-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay-025.cpp
180 lines (148 loc) · 2.35 KB
/
Day-025.cpp
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
// types of inheritance
// 1. Single inheritance
/*#include<iostream>
using namespace std;
class Animal{
public:
int age;
int weight;
public:
void speak(){
cout << "speaking " << endl;
}
};
class Dog: public Animal{
};
int main (){
Dog d;
d.speak();
return 0;
} */
// multilevel inheritance
/*#include<iostream>
using namespace std;
class Animal{
public:
int age;
int weight;
public:
void speak(){
cout << "speaking " << endl;
}
};
class Dog: public Animal{
};
class GermanShepherd: public Dog{
};
int main (){
GermanShepherd g;
g.speak();
return 0;
} */
// multiple inheritance
/*#include<iostream>
using namespace std;
class Animal{
public:
int age;
int weight;
public:
void bark(){
cout << "barking " << endl;
}
};
class Human{
public:
void speak(){
cout << "speaking" << endl;
}
};
class Hybrid: public Animal,public Human{
};
int main (){
Hybrid obj1;
obj1.speak();
obj1.bark();
return 0;
} */
// Hierarchical Inheritance
/*#include<iostream>
using namespace std;
class Animal{
public:
int age;
int weight;
public:
void speak(){
cout << "speaking " << endl;
}
};
class Dog: public Animal{
};
class Cat: public Animal{
};
int main (){
Dog d;
d.speak();
Cat c;
c.speak();
return 0;
} */
// Hybrid Inheritance - Combination of more than 1 type of inheritance
/*#include<iostream>
using namespace std;
class Animal{
public:
int age;
int weight;
public:
void speak(){
cout << "speaking " << endl;
}
};
class Dog: public Animal{
public:
void dog (){
cout << "Dog has been called"<< endl;
}
};
class Cat: public Animal{
public:
void cat (){
cout << "cat has been called"<< endl;
}
};
class Creatures: public Dog, public Cat{
} ;
int main (){
Cat c;
c.speak();
Creatures obj1;
obj1.cat();
obj1.dog();
return 0;
} */
// inheritance Ambiguity
#include<iostream>
using namespace std;
class A{
public:
void func(){
cout << "i AM A" << endl;
}
};
class B{
public:
void func(){
cout << "i AM B" << endl;
}
};
class c:public A, public B{
};
int main (){
c obj;
// use scope operator to specify which func you want to call
obj.A::func();
obj.B::func();
return 0;
}