Repository navigation
Expand file tree
/
Copy pathOPPs_2_Inheritance.cpp
More file actions
168 lines (110 loc) · 2.55 KB
/
Copy pathOPPs_2_Inheritance.cpp
File metadata and controls
168 lines (110 loc) · 2.55 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
#include <bits/stdc++.h>
#define co cout
#define e endl
using namespace std;
// Ecapsulation : simple class me bas private kr do data ko
// Polymorphism : exists in multiple forms
// types :
// 1. Compile Time Polymorphism
// a. function overloading
// b. operator overloading
// 2. Run Time Polymorphism
// a. function over-riding;
// Abstraction : Implementation hiding
// Key Difference in Abstraction and Encapsulation in Code:
// Abstraction is achieved through defining abstract classes or interfaces
// that hide the internal details of how specific methods are implemented.
// The code focuses on what methods an object will provide.
// Encapsulation is about hiding the internal data
// (like balance in BankAccount) and providing public
// methods to control how that data is accessed and modified.
// Inheritance :
class parent_class
{
public:
int height;
int weight;
int age;
// private:
int cycle;
public:
int getAge(){
return this->age;
}
void setWeight(int weight)
{
this->weight = weight;
}
int getCycle(){
return this->cycle;
}
};
class child_class : public parent_class {
public:
string color;
void sleep()
{
co<<"Male sleeping"<<e;
}
};
// for polymorphism
class Animal{
public:
int val1;
int val2;
void setVal1(int val1){
this->val1 = val1;
}
void setVal2(int val1)
{
this->val2 = val2;
}
void operator + (Animal &obj)
{
int val1 = this->val1;
int val2 = obj.val1;
cout<<"Output : "<<val2-val1<<endl;
}
void speak()
{
co<<"speaking"<<e;
}
};
class Dog : public Animal{
public:
void speak()
{
co<<"barking"<<e;
}
};
// public to protected ke case man lo agr me koi child class
// banata hun to usko access sirf usi class me kr skte hai
// for ex : valid in child_class
// int getHeight()
// {
// return this->height;
// }
// Types of inheritance :
// 1. Single Inheritance : B inherit A
// 2. Multilevel Inheritance : D inherit C inherit B inherit A
// 3. Multiple Inheritance : C inherit B and A both
// 4. Hierarchical Inheritance : One clas serve as parent class for more than one class
int main(){
child_class obj1;
co<<obj1.height<<e; // garbage
co<<obj1.age<<e; // garbage
co<<obj1.weight<<e; // 1
obj1.sleep(); // Male sleeping
co<<obj1.cycle<<e; // can't be accessed
co<<"cycle : "<<obj1.getCycle()<<e; // gives 1
obj1.setWeight(84);
co<<obj1.weight<<e; // 84
// Polymorphism
Animal newA, newB;
newA.setVal1(6);
newB.setVal1(4);
newB + newA; // Output : 2
// Function over-riding
Dog D;
D.speak(); // barking
}