-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path28_ambiguity_res.cpp
154 lines (153 loc) · 2.64 KB
/
28_ambiguity_res.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
//ambiguity-->if there are more than one functions of same name in class and
//then we inherit into other class then we have to specify compiler that
// which class function we want to use in derived class
//CASE 1-> when there is no function with same name in derived class as base
//class
/*
#include <iostream>
using namespace std;
class base1
{
public :
void greet()
{
cout<<"how r u ?"<<endl;
}
};
class base2
{
public :
void greet()
{
cout<<"kaise ho ?"<<endl;
}
};
class derived : public base1, public base2
{
};
int main ()
{
base1 b1;
b1.greet();
base2 b2;
b2.greet();
derived der;
// der.greet();//error :'greet' is ambiguous ,bec derived class has 2 greet()now
//so we have to tell compiler which greet functn to use
return 0;
}
*/
// solving above program ambiguity
/*
#include <iostream>
using namespace std;
class base1
{
public :
void greet()
{
cout<<"how r u ?"<<endl;
}
};
class base2
{
public :
void greet()
{
cout<<"kaise ho ?"<<endl;
}
};
class derived : public base1, public base2
{
public :
void greet()
{
base1:: greet();// ambiguity resolve ,now it will use greet of base1 class
}
};
int main ()
{
base1 b1;
b1.greet();
base2 b2;
b2.greet();
derived der;
der.greet();// greet of base1 class now
return 0;
}
*/
//CASE 2-> if there is same name function in derived class then
//ambiguity will resolve itself and compiler will use functions
// of derived class
/*
#include <iostream>
using namespace std;
class base1
{
public :
void greet()
{
cout<<"how r u ?"<<endl;
}
};
class base2
{
public :
void greet()
{
cout<<"kaise ho ?"<<endl;
}
};
class derived : public base1, public base2
{
public :
void greet()
{
cout<<"hello world"<<endl;
}
};
int main ()
{
base1 b1;
b1.greet();
base2 b2;
b2.greet();
derived der;
der.greet();
return 0;
}
*/
//ex-> 2 if class has its own function of same name as inherited
//by other class then compiler will run its own function
#include <iostream>
using namespace std;
class base1
{
public :
void greet(){
cout<<"how are u?"<<endl;
}
};
class base2: public base1
{
public :
void greet(){
cout<<"kaise ho aap?"<<endl;
}
};
class derived : public base2
{
public:
void greet(){
cout<<"hi"<<endl;
}
} ;
int main() {
base1 b1;
b1.greet();
base2 b2;
b2.greet();//base2 will run its own greet() , not of base1 (inherited)
derived der;
der.greet();//derived class will run its greet()
return 0;
}