forked from portfoliocourses/cplusplus-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.cpp
More file actions
101 lines (89 loc) · 3.34 KB
/
inheritance.cpp
File metadata and controls
101 lines (89 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
*
* Program: Inheritance Demonstration
*
* Description: A basic demonstration of how to use inheritance in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=rJlJ8qqVm3k
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
using namespace std;
// MenuItem is a "Base Class" or what we could also call a "Parent Class", it
// contains a few simple member variables and a member function for printing
// them out.
class MenuItem
{
public:
string name;
double calories;
void print()
{
cout << name << " (" << calories << " cal)" << endl;
}
};
// We want our Drink class to "have and do" everything that a MenuItem class
// can do... we could copy and paste the code from MenuItem into the Drink
// class, but then we would have duplicate code (a "code clone" we can call
// it). The problem with duplicate code is that it becomes harder to maintain,
// if we need to make a change we would need to make it in two places!
//
// So instead we us : public MenuItem to make Drink a "Derived Class" off the
// the MenuItem class (also called a "Child Class"). The Drink class will be
// given the member variables and member functions of its base class MenuItem,
// i.e. our Drink objects will have public member variables name, calories,
// and the print function too!
//
// We can still define in the Drink class new member variables, like ounces,
// and new member functions, like cal_per_ounce.
//
class Drink : public MenuItem
{
public:
double ounces;
double cal_per_ounce()
{
return calories / ounces;
}
};
int main()
{
// instantiate and use a MenuItem object
MenuItem french_fries;
french_fries.name = "French Fries";
french_fries.calories = 400;
french_fries.print();
// We can also instantiate and use a Drink object, using the same name
// and calories member variables, and the same print member function, in
// addition to the new member variable calories and the new member function
// cal_per_ounce().
//
Drink hot_chocolate;
hot_chocolate.name = "Hot Chocolate";
hot_chocolate.calories = 300;
hot_chocolate.ounces = 8;
hot_chocolate.print();
cout << "cal/ounce: " << hot_chocolate.cal_per_ounce()
<< endl;
// We can use a derived class anywhere a base class can be used and it will
// work! This is a feature of polymorphism, another concept that allows
// inheritance to help us write better code. We describe the relationship
// between Drink and MenuItem as an "is a" relationship, in that a Drink
// object "is a" MenuItem object. A Drink object can be used anywhere that a
// Menu item object can, in the sense that a Drink object has the same member
// variables and same member functions available.
//
// Here we make a pointer to an MenuItem variable, and we assign it the
// memory address of the hot_chocolate Drink object instance. This seems odd
// but polymorphism will allow for this, and we can even call the drink
// member function via the pointer, and it's OK. What makes this work is that
// we *know* a Drink object will have a print object by virtue of it being
// a MenuItem too.
//
MenuItem *ptr;
ptr = &hot_chocolate;
ptr->print();
return 0;
}