-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance
More file actions
198 lines (146 loc) · 5.09 KB
/
Inheritance
File metadata and controls
198 lines (146 loc) · 5.09 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
Suppose, we want to write a class ‘RateJungle’ in which visitor can provide ‘rating’ based on their visiting-experience.
If we write the class from the starting, then we need define attribute ‘visitorName’ again; which will make the code repetitive and unorganizable,
as the visitor entry will be at multiple places and such code is more prone to error. With the help of inheritance, we can avoid such duplication as shown in Listing 14.5;
where class Jungle is inherited at Line 33 by the class ‘RateJungle’.
Now, when the object ‘r’ of class ‘RateJungle’ is created at Line 62 of Listing 14.5, then this object ‘r’ will have the access to ‘visitorName’ as well
(which is in the parent class).
// InheritanceEx.cpp
#include <iostream>
using namespace std;
class Jungle{
private: // not accessible outside the class
string visitorName;
public: // to allow access to function 'welcomeMessage' outside the class
// constructor : automatically called at the time of object-creation
Jungle(string name){
setVisitorName(name);
}
// setVisitorName is accessible outside the class, which will set the visitor name
void setVisitorName(string name){
visitorName = name;
}
// function to retrieve the visitorName as it is not accessible directly
string getVisitorName(){
return visitorName;
}
void welcomeMessage(){
cout << "Welcome to Jungle " << getVisitorName();
}
};
class RateJungle : public Jungle{
private:
int feedback;
string name;
public:
// constructor 1
RateJungle(string name) : Jungle(name){
feedback = 0; // set feedback to zero by default
}
// constructor 2
RateJungle(string name, int value) : Jungle(name){
feedback = value; // set feedback to zero by default
}
void setFeedback(int value){
feedback = value;
}
void printRating(){
cout << "Thanks " << getVisitorName() << endl;
cout << "Your feedback is set as : " << feedback << endl;
}
};
int main(){
string name;
RateJungle r("pranit"); // constructor 1 will be initialized
RateJungle s("shantanu", 3); // constructor 2 will be initialized
// Feedback is set to 0 by constructor
r.printRating(); // print the feedback value
r.setFeedback(2); // provide feedback value
r.printRating(); // print the feedback value
s.printRating(); // print the feedback value
return 0;
}
/* Outputs
thanks pranit
Your feedback is set as : 0
Thanks pranit
Your feedback is set as : 2
Thanks shantanu
Your feedback is set as : 3
*/
an empty constructor is added at Line 54, for setting the value of the object ‘p’ (Line 74) through Line 83. Note that,
since we added empty constructor in child class, therefore we need to add in parent class as well as shown in Line 18.
// InheritanceEx2.cpp
#include <iostream>
using namespace std;
class Jungle{
private: // not accessible outside the class
string visitorName;
public: // to allow access to function 'welcomeMessage' outside the class
// constructor : automatically called at the time of object-creation
Jungle(string name){
setVisitorName(name);
}
// empty constructor
Jungle(){ // it is required as child-class is using empty constructor
}
// setVisitorName is accessible outside the class, which will set the visitor name
void setVisitorName(string name){
visitorName = name;
}
// function to retrieve the visitorName as it is not accessible directly
string getVisitorName(){
return visitorName;
}
void welcomeMessage(){
cout << "Welcome to Jungle " << getVisitorName();
}
};
class RateJungle : public Jungle{
private:
int feedback;
string name;
public:
// constructor 1
RateJungle(string name) : Jungle(name){
feedback = 0; // set feedback to zero by default
}
// constructor 2
RateJungle(string name, int value) : Jungle(name){
feedback = value; // set feedback to zero by default
}
// constructor 3 : empty constructor
RateJungle(){ // empty constructor in parent class is compulsory
}
void setFeedback(int value){
feedback = value;
}
void printRating(){
cout << "Thanks " << getVisitorName() << endl;
cout << "Your feedback is set as : " << feedback << endl;
}
};
int main(){
string name;
RateJungle r("pranit"); // constructor 1 will be initialized
RateJungle s("shantanu", 3); // constructor 2 will be initialized
RateJungle p;
// Feedback is set to 0 by constructor
r.printRating(); // print the feedback value
r.setFeedback(2); // provide feedback value
r.printRating(); // print the feedback value
s.printRating(); // print the feedback value
p.setVisitorName("kharat"); // constructor 3 is used
p.setFeedback(5);
p.printRating();
return 0;
}
/* Outputs
Thanks pranit
Your feedback is set as : 0
Thanks pranit
Your feedback is set as : 2
Thanks shantanu
Your feedback is set as : 3
Thanks kharat
Your feedback is set as : 5
*/