-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathambiguity.cpp
More file actions
74 lines (62 loc) · 1.3 KB
/
ambiguity.cpp
File metadata and controls
74 lines (62 loc) · 1.3 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
/* What is ambiguity problem?
The most obvious problem with multiple inheritance occurs during function overriding.
Suppose, two base classes have a same function which is not overridden in derived class.
If you try to call the function using the object of the derived class, compiler shows error. It's because compiler doesn't know which function to call. For example,
class base1
{
public:
void someFunction( )
{ .... ... .... }
};
class base2
{
void someFunction( )
{ .... ... .... }
};
class derived : public base1, public base2
{
};
int main()
{
derived obj;
obj.someFunction() // Error!
}
This problem can be solved using scope resolution function to specify which function to class either base1 or base2
int main()
{
obj.base1::someFunction( ); // Function of base1 class is called
obj.base2::someFunction(); // Function of base2 class is called.
}
#include<iostream>
using namespace std;
class a
{
public:
void display()
{
cout<<"in class a"<<endl;
}
};
class b
{
public:
void display()
{
cout<<"in class b"<<endl;
}
};
class c: public a , public b
{
public:
void output()
{
cout<<"in class c"<<endl;
}
};
int main()
{
c c2;
c2.a::display(); //syntax obj.class name::func();
c2.b::display();
c2.output();
}