-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinheritance7.cpp
More file actions
61 lines (55 loc) · 1.5 KB
/
Copy pathinheritance7.cpp
File metadata and controls
61 lines (55 loc) · 1.5 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
/*
Granting Access
1) General form:- base-class::Member;
2) By coding "Using" statement
3) By an access declaration within derived class
*/
// 3) By an access declaration within derived class(Virtual Base Class)
// 1)"Virtual" keywoard create a single copy of a class(here it is base class)
// 2)Using the derived keyword "ob.derived1::i"
#include<iostream>
using namespace std;
class Base
{
public:int i;
};
class derived1:virtual public Base
// class derived1:public Base
{
public:int j;
};
class derived2:virtual public Base
// class derived2:public Base
{
public:int k;
};
class derived3:public derived1,public derived2
{
public:int sum;
};
int main()
{
derived3 ob;
<<<<<<< HEAD
ob.i=10;//Ambigous Statement(compiler doesnt know which copy of base class)
=======
//ob.i=10;//Ambigous Statement(compiler doesnt know which copy of base class)
>>>>>>> 94b19d334770d2774d4974a2f4bd59094cf8205b
ob.derived1::i=10;
ob.j=20;
ob.k=30;
ob.sum=ob.derived1::i;
<<<<<<< HEAD
ob.sum=ob.j+ob.k;
=======
//ob.sum=ob.j+ob.k;
>>>>>>> 94b19d334770d2774d4974a2f4bd59094cf8205b
cout<<ob.sum<<endl;
}
/*both derived1 and derived2 inherites the base class
and derived3 inherites both derived1 and derived2 classes
hence 2 copies of base class are present in a object of type derived3
To remove the ambiguity
1) Use "::" to the data member 'i' and manually select either derived1 or derived2
ob.derived1::i=10;
2) Use Virtual keyword while inheriting the base class to derived1 and derived2*/