-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultilevelinheri.cpp
50 lines (40 loc) · 1.11 KB
/
multilevelinheri.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
#include <iostream>
// Base class
class Base {
public:
void publicMethod() {
std::cout << "Public method in Base class\n";
}
private:
void privateMethod() {
std::cout << "Private method in Base class\n";
}
};
// Derived class 1 publicly inherits from Base
class Derived1 : public Base {
public:
void derived1Method() {
std::cout << "Derived1 method\n";
}
};
// Derived class 2 privately inherits from Derived1
class Derived2 : private Derived1 {
public:
void derived2Method() {
// Access public methods of the Base class through Derived1
publicMethod(); // Accessible
// Access public methods of Derived1 directly
derived1Method(); // Accessible
std::cout << "Derived2 method\n";
}
};
int main() {
Derived2 obj;
// Access public methods of the Base class
obj.publicMethod(); // Accessible
// Access public methods of Derived1 through Derived2
// obj.derived1Method(); // Not accessible (private inheritance)
// Access methods of Derived2
obj.derived2Method(); // Accessible
return 0;
}