forked from Dashark/hello-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi-inheritance.cpp
95 lines (90 loc) · 1.88 KB
/
multi-inheritance.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
using namespace std;
template<class T>
class Person {
// Data members of person
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
void learn() {
static_cast<T const&>(*this).learn();
}
void teach() {
static_cast<T const&>(*this).teach();
}
};
template<class T>
class Faculty : public Person<Faculty> {
// data members of Faculty
public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
void learn() {
cout << "Faculty learn sth." << endl;
}
void teach() {
cout << "Faculty teach sth." << endl;
}
};
template<class T>
class Student : public Person<Student> {
// data members of Student
public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
void learn() {
cout << "Student learn sth." << endl;
}
void teach() {
cout << "Student can't teach." << endl;
}
};
/*
template<class T1, class T2>
class TA : public T1 {
TA(int x):T1(x) {
cout << "TA::TA(int ) called" << endl;
}
void learn() {
T1::learn();
}
void teach() {
static_cast<T2 const&>(*this).teach();
}
// TA constraints
};
*/
class TA : public Person<TA> {
public:
TA(int x):st(x), fa(x) {
cout << "TA::TA(int ) called" << endl;
}
void learn() {
st.learn();
}
void teach() {
fa.teach();
}
private:
Student st;
Faculty fa;
};
class TA : public Student, public Faculty {
public:
TA(int x):Student(x), Faculty(x) {
cout << "TA::TA(int ) called" << endl;
}
void learn() {
Student::learn();
}
void teach() {
Faculty::teach();
}
};
int main() {
TA ta1(30);
//TA<Student, Faculty> ta1(30);
ta1.learn(); // Student's learn
ta1.teach(); // Faculty's teach
}