0% found this document useful (0 votes)
10 views2 pages

Class Public: Return

The document is a C++ code implementation showcasing class inheritance and constructors. It defines classes for Person, Student, HighSchoolStudent, Employee, and Engineer, demonstrating default and custom constructors, as well as destructor behavior. The main function creates instances of these classes to illustrate the order of constructor calls and access to member functions.

Uploaded by

Xbit Smarty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Class Public: Return

The document is a C++ code implementation showcasing class inheritance and constructors. It defines classes for Person, Student, HighSchoolStudent, Employee, and Engineer, demonstrating default and custom constructors, as well as destructor behavior. The main function creates instances of these classes to illustrate the order of constructor calls and access to member functions.

Uploaded by

Xbit Smarty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include <iostream>

2 #include <string>
3
4 class Person
5 {
6 public:
7 Person() : m_name{""}, m_age{0}
8 {
9 std::cout << "Person default constructor: name " << m_name << " age " << m_age
<< std::endl;
10 }
11
12 Person(const std::string& name, int age = 18) : m_name{name}, m_age{age}
13 {
14 std::cout << "Person custom constructor: name " << m_name << " age " << m_age
<< std::endl;
15 }
16 virtual ~Person()
17 {
18 std::cout << "Person destructor" << std::endl;
19 }
20
21 std::string getName() const
22 {
23 return m_name;
24 }
25 protected:
26 std::string m_name;
27 int m_age;
28 };
29
30 class Student : private Person
31 {
32 public:
33 Student()
34 {
35 std::cout << "Student default constructor: name " << m_name << " age " <<
m_age << std::endl;
36 }
37 Student(const std::string& name) : Person(name) // , m_age{18} can't call it here
38 {
39 std::cout << "Student custom constructor: name " << m_name << " age " << m_age
<< std::endl;
40 m_age = 20; // allowed to be called here
41 }
42 virtual ~Student()
43 {
44 std::cout << "Student destructor" << std::endl;
45 }
46 };
47
48 class HighSchoolStudent : public Student
49 {
50 public:
51 HighSchoolStudent()
52 {
53 // m_name for example is not accesible here
54 std::cout << "HighSchoolStudent default constructor" << std::endl;
55 }
56 virtual ~HighSchoolStudent()
57 {
58 std::cout << "HighSchoolStudent destructor" << std::endl;
59 }
60 };
61
62 class Employee
63 {
64 public:
65 Employee(const std::string& name, int id) : m_name{name}, m_id{id}
66 {
67 std::cout << "Employee custom constructor: name " << m_name << " id " << m_id
<< std::endl;
68 }
69 virtual ~Employee()
70 {
71 std::cout << "Employee destructor" << std::endl;
72 }
73 std::string getName() const
74 {
75 return m_name;
76 }
77 protected:
78 std::string m_name;
79 int m_id;
80 };
81
82 class Engineer : public Person, public Employee
83 {
84 public:
85 // declaring default constructor is mandatory since Person requires a
86 // default constructor if we don't call initialization of base constructors
"Person(name, age), Employee(name, id)"
87 Engineer(const std::string& name, int id, int age) : Person(name, age), Employee(
name, id)
88 {
89 // if called m_name barely, it will be ambiguous, which m_name to take
90 std::cout << "Engineer custom constructor: person name " << Person::m_name
91 << " Employee name " << Employee::m_name << " id " << m_id << " age " << m_age
<< std::endl;
92 }
93 virtual ~Engineer()
94 {
95 std::cout << "Engineer destructor" << std::endl;
96 }
97 };
98
99 int main()
100 {
101 {
102 // check order of call
103 Student student("Joe");
104 }
105 {
106 Engineer engineer("Alex", 1, 28);
107 std::cout << "Get engineer name from Employee " << engineer.Employee::getName
()
108 << " from person " << engineer.Person::getName() << std::endl;
109 }
110 HighSchoolStudent st;
111
112 return 0;
113 }

You might also like