-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritance.cpp
More file actions
387 lines (360 loc) · 11 KB
/
Copy pathInheritance.cpp
File metadata and controls
387 lines (360 loc) · 11 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/***
* Inheritance
* ===========
* - Class is used to describe properties and behavior of an object.
* - It is a process of inheriting properties and behavior of existing class into a new class.
* - Existing Class is called as Parint Class, Old Class, Super Class, Main Class and Base Class.
* - New Class is called as Child Class, Derived Class, Sub Class.
* syntax:-
* ======
* Class Parant_Class
* {
*
* };
* Class Child_Class:Visiblity_Class
* {
* };
* - Inheritance is the process by which new classes called derived classes are created from existing classes called base classes.
* - The derived classes have all the features of the base classes.
* - The programmer can chose to add new features specific to the newly created derived class.
* - Inheritance provides the idea of reusability i.e., code one written can be used again and again in number of classes.
* - The above concept of reusability achieved by inheritance save the programmer's time and effort.Since the main code written can be reused in various situation as needed.
* - Any change made to the base class code automatically get propagated to the inherited code. This will provides that it is possible to change one of code and all current object will automatically be updated.
*
* Q. What are different types of inheritance?
* Ans:- types of inheritance are :->
* - Single Inheritance
* - Multiple Inheritance
* - Multilevel Inheritance
* - Hierarchical Inheritance
* - Hybrid Inheritance
* types of Inheritance in terms of Visiblity:->
* - Private
* - Public
* - Protacted
*
* Single Inheritance
* ==================
* syntax:-
* ======
* Class Parant_Class
* {
*
* };
* Class Child_Class:Visiblity_Class
* {
* };
* Multiple Inheritance
* ====================
* syntax:-
* ======
* Class Parant_Class1
* {
*
* };
* Class Parant_Class2
* {
*
* };
* Class Child_Class:Visiblity_Class1,Visiblity_Class2,.....,Visiblity_ClassN
* {
* };
* Multilevel Inheritance
* ======================
* syntax:-
* ======
* Class Parant_Class1
* {
*
* };
* Class Child_Class2:Visiblity_Class1
* {
*
* };
* Class Child_Class3:Visiblity_Class2
* {
* };
* .
* .
* .
* Class Child_Class(N+1):Visiblity_ClassN
* {
* };
* Hierarchical Inheritance
* ========================
* syntax:-
* ======
* Class Parant_Class
* {
*
* };
* Class Child_Class1:Visiblity_Class
* {
*
* };
* Class Child_Class2:Visiblity_Class
* {
* };
* Hybrid Inheritance
* ==================
* syntax:-
* ======
* Class Parant_Class
* {
*
* };
* Class Child_Class1:Visiblity_Class
* {
* };
* Class Child_Class2:Visiblity_Class
* {
* };
* Class Child_Class3:Visiblity_Class1,Visiblity_Class2
* {
* };
*
* Visibility Mode Inheritance:->
* =============================
*
* Private Inheritance
* ===================
*
*
* Protacted Inheritance
* =====================
*
*
* Public Inheritance
* ==================
*
*
*
***/
// //Single Inheritance Program
#include<iostream>
using namespace std;
//Parant Class
class Car
{
/** for Variation 1 and 2**/
private:
int color_code;
char Fuel_type[10];
// **/
// //For Variation 3
// protected:
// int color_code;
// char Fuel_type[10];
public:
void inputdata()
{
cout<<"Color of car as \n 1. Silver \n 2. White \n 3. Black \n Enter the Color of your car => ";
cin>>color_code;
cout<<"Enter your car fuel type => ";
cin>>Fuel_type;
cout<<"---------------*************************------------------------"<<endl;
}
void showdata()
{
if(color_code == 1)
{
cout<<"Color of your car is Silver "<<endl;
}
else if(color_code == 2)
{
cout<<"Color of your car is White "<<endl;
}
else if(color_code == 3)
{
cout<<"Color of your car is Black "<<endl;
}
cout<<"Fuel type of your car is "<<Fuel_type<<endl;
cout<<"---------------*************************------------------------"<<endl;
}
};
//Child Class
class SportsCar:public Car
{
private:
int max_speed;
int alarm;
int airbags;
public:
void getdata()
{
cout<<"Enter the maximum speed of your car => ";
cin>>max_speed;
cout<<"Enter the number of alarm in your car => ";
cin>>alarm;
cout<<"Enter the number of airbags in your car => ";
cin>>airbags;
cout<<"---------------*************************------------------------"<<endl;
}
void viewdata()
{
cout<<"Maximum speed of your car => "<<max_speed<<endl;
cout<<"Number of alarm in your car => "<<alarm<<endl;
cout<<"Number of airbags in your car => "<<airbags<<endl;
// //for Variation 3
if(color_code == 1)
{
cout<<"Color of your car is Silver "<<endl;
}
else if(color_code == 2)
{
cout<<"Color of your car is White "<<endl;
}
else if(color_code == 3)
{
cout<<"Color of your car is Black "<<endl;
}
cout<<"Fuel type of your car is "<<Fuel_type<<endl;
cout<<"---------------*************************------------------------"<<endl;
}
};
int main()
{
Car Swift;
SportsCar Tesla;
// /** variation 1
Swift.inputdata();
Swift.showdata();
Tesla.getdata();
Tesla.viewdata();
// */
// /** variation 2
// Tesla.inputdata();
// Tesla.showdata();
// Tesla.getdata();
// Tesla.viewdata();
// */
// /**Variation 3 to accessing the instant variable of Parant class in Child Class
// Tesla.inputdata();
// Tesla.getdata();
// Tesla.viewdata();
// **/
return 0;
// }
// // Multiple Inheritance
// #include<iostream>
// #include<math.h>
// using namespace std;
// class A
// {
// protected:
// int a, b, c;
// public:
// void get()
// {
// cout<<"Enter the value of a, b : ";
// cin>>a>> b;
// c = a+b;
// }
// };
// class B
// { protected:
// int s;
// public:
// void sumOfSquare()
// {
// int a = 10, b = 20;
// s = pow(a,2) + pow(b,3);
// }
// };
// class C:public A, public B
// {
// public:
// void SumOfCube()
// {
// int d;
// d = pow(a,3) + pow(b,3);
// cout<<"Sum of Cubes : "<<d<<endl;
// cout<<"Added Value are : "<<c<<endl;
// cout<<"Sum of square : "<<s<<endl;
// }
// };
// //Parant Class
// class Car
// {
// protected:
// int color_code;
// char Fuel_type[10];
// public:
// void inputdata()
// {
// cout<<"Color of car as \n 1. Silver \n 2. White \n 3. Black \n Enter the Color of your car => ";
// cin>>color_code;
// cout<<"Enter your car fuel type => ";
// cin>>Fuel_type;
// cout<<"---------------*************************------------------------"<<endl;
// }
// };
// class SportsCar
// {
// protected:
// int max_speed;
// int alarm;
// int airbags;
// public:
// void getdata()
// {
// cout<<"Enter the maximum speed of your car => ";
// cin>>max_speed;
// cout<<"Enter the number of alarm in your car => ";
// cin>>alarm;
// cout<<"Enter the number of airbags in your car => ";
// cin>>airbags;
// cout<<"---------------*************************------------------------"<<endl;
// }
// };
// class luxurycar:public Car, public SportsCar
// {
// private:
// int sunroof;
// int qled;
// public:
// void data()
// {
// cout<<"Enter the number of sunroof : ";
// cin>>sunroof;
// cout<<"Enter the number of qled : ";
// cin>>qled;
// cout<<"---------------*************************------------------------"<<endl;
// }
// void viewdata()
// {
// if(color_code == 1)
// {
// cout<<"Color of your car is Silver "<<endl;
// }
// else if(color_code == 2)
// {
// cout<<"Color of your car is White "<<endl;
// }
// else if(color_code == 3)
// {
// cout<<"Color of your car is Black "<<endl;
// }
// cout<<"Fuel type of your car is "<<Fuel_type<<endl;
// cout<<"Maximum speed of your car => "<<max_speed<<endl;
// cout<<"Number of alarm in your car => "<<alarm<<endl;
// cout<<"Number of airbags in your car => "<<airbags<<endl;
// cout<<"Number of sunroof in your car => "<<sunroof<<endl;
// cout<<"Number of qled in your car => "<<qled<<endl;
// cout<<"---------------*************************------------------------"<<endl;
// }
// };
// int main()
// {
// C obj;
// obj.get();
// obj.sumOfSquare();
// obj.SumOfCube();
// cout<<"****************************************"<<endl;
// luxurycar rollroyce;
// rollroyce.inputdata();
// rollroyce.getdata();
// rollroyce.data();
// rollroyce.viewdata();
// return 0;
// }