-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42ambiguityres.cpp
More file actions
64 lines (60 loc) · 1.49 KB
/
Copy path42ambiguityres.cpp
File metadata and controls
64 lines (60 loc) · 1.49 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
#include <iostream>
using namespace std;
class base1
{
public:
void greet()
{
cout << "Good Morning, my greetings!" << endl;
}
};
class base2
{
public:
void greet() // Here we created a function with similar name as a previous base class
{
cout << "Good Evening, my greetings to you sir!" << endl;
}
};
class derived : public base1, public base2
{
// This derived class doesn't know whose greet function to fetch
// To tell it we need to mention or address whose greet it needs to favour
public:
void greet()
{
base1 ::greet();
}
};
class derived_base1 : public base1
{
/*
IN CASE OF AMBIGUITY BETWEEN A BASE AND DERIVED CLASS :-
The compiler favours the derived class function with same name as it has higher priority
so no need for ambiguity resolution here
*/
public:
void greet()
{
cout << "Good Afternoon, this is the favoured greeting !!" << endl;
}
};
int main()
{
base1 obj1;
base2 obj2;
derived obj3;
obj1.greet();
obj2.greet();
obj3.greet();
// This causes Ambiguity, where the derived class object can't determine whose greet function to call between base1 and base2
/*
TO RESOLVE AMBIGUITY :-
1) CREATE A FUNCTION WITH SIMILAR NAME IN THE DERIVED CLASS
2) USE THIS SYNTAX AS THE AMBIGUITY RESOLUTION
{{base class}} :: {{function call/ name()}};
*/
derived_base1 obj4;
obj4.greet(); // this runs without any problem
return 0;
}