Repository navigation
Expand file tree
/
Copy pathinheritance.cpp
More file actions
102 lines (82 loc) · 3.49 KB
/
Copy pathinheritance.cpp
File metadata and controls
102 lines (82 loc) · 3.49 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Inheritance: build a new class from an existing one, reusing and extending it.
// Shows single, multilevel, hierarchical and multiple inheritance, the
// protected access level, and the order constructors and destructors run in.
#include <iostream>
#include <string>
class Person {
protected: // visible to derived classes, hidden from everyone else
std::string name;
int age;
public:
Person(const std::string& name, int age) : name(name), age(age) { std::cout << " Person() " << name << '\n'; }
~Person() { std::cout << " ~Person() " << name << '\n'; }
void introduce() const { std::cout << " Hi, I'm " << name << ", " << age << " years old.\n"; }
};
// Single inheritance: a Student IS-A Person
class Student : public Person {
protected:
std::string program;
double cgpa;
public:
Student(const std::string& name, int age, const std::string& program, double cgpa)
: Person(name, age), program(program), cgpa(cgpa) { // the base part is built first
std::cout << " Student() " << name << '\n';
}
~Student() { std::cout << " ~Student() " << name << '\n'; }
void study() const { std::cout << " " << name << " studies " << program << " (CGPA " << cgpa << ")\n"; }
};
// Multilevel inheritance: GraduateStudent -> Student -> Person
class GraduateStudent : public Student {
std::string thesis;
public:
GraduateStudent(const std::string& name, int age, const std::string& program, double cgpa,
const std::string& thesis)
: Student(name, age, program, cgpa), thesis(thesis) {
std::cout << " GraduateStudent() " << name << '\n';
}
~GraduateStudent() { std::cout << " ~GraduateStudent() " << name << '\n'; }
// 'name' comes from Person, two levels up, and is accessible because it is protected
void defendThesis() const { std::cout << " " << name << " defends: \"" << thesis << "\"\n"; }
};
// Hierarchical inheritance: Teacher is a second class derived from Person
class Teacher : public Person {
std::string subject;
public:
Teacher(const std::string& name, int age, const std::string& subject) : Person(name, age), subject(subject) {}
void teach() const { std::cout << " " << name << " is teaching " << subject << '\n'; }
};
// Multiple inheritance: one class with two base classes
class Camera {
public:
void takePhoto() const { std::cout << " *click* photo saved\n"; }
};
class Phone {
public:
void call(const std::string& number) const { std::cout << " calling " << number << "...\n"; }
};
class SmartPhone : public Camera, public Phone {
public:
void browse(const std::string& site) const { std::cout << " opening " << site << '\n'; }
};
int main() {
std::cout << "Constructing a GraduateStudent (base classes are built first):\n";
{
GraduateStudent g("Maryam", 25, "MS Computer Science", 3.8, "Federated Learning on Edge Devices");
std::cout << "\nUsing members from every level:\n";
g.introduce(); // from Person
g.study(); // from Student
g.defendThesis(); // its own
std::cout << "\nLeaving scope (destructors run in reverse order):\n";
}
std::cout << "\nHierarchical inheritance:\n";
Teacher t("Dr. Kamran", 45, "Operating Systems");
t.introduce();
t.teach();
std::cout << "\nMultiple inheritance:\n";
SmartPhone phone;
phone.takePhoto();
phone.call("555-0199");
phone.browse("github.com");
std::cout << "\nEnd of main:\n";
return 0;
}