-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtual Base Class.cpp
More file actions
42 lines (37 loc) · 907 Bytes
/
Copy pathVirtual Base Class.cpp
File metadata and controls
42 lines (37 loc) · 907 Bytes
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
#include <iostream>
using namespace std;
// Base class
class Person {
public:
void display() {
cout << "Person class called." << endl;
}
};
// Derived classes from Person using virtual inheritance
class Student : virtual public Person {
public:
void showStudent() {
cout << "Student class called." << endl;
}
};
class Teacher : virtual public Person {
public:
void showTeacher() {
cout << "Teacher class called." << endl;
}
};
// Derived class from both Student and Teacher
class TA : public Student, public Teacher {
public:
void showTA() {
cout << "TA (Teaching Assistant) class called." << endl;
}
};
int main() {
TA ta;
ta.display(); // Only one copy of Person's display() due to virtual inheritance
ta.showStudent();
ta.showTeacher();
ta.showTA();
return 0;
}