-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inheritance
53 lines (44 loc) · 1.15 KB
/
Inheritance
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
#include <iostream>
using namespace std;
class Person{
protected:
string name;
private:
int age;
public:
void setName(string iname){ name = iname;}
void setAge(int iage){ age = iage;}
Person(){
cout << "Person constructor called!" << endl;
}
~Person(){
cout << "Person deconstructor called!"<< endl;
}
};
class Student : public Person{
protected:
int ID;
public:
void setId(int iID) {ID = iID;}
void introduce (){
name = "John"; // derived class has access to protected stuffs from base class
//age = 21; // but cannot access private stuffs from base class
cout << "Hi, my name is "<< name << " and I'm " << age << endl
<< "My student ID is " << ID<<endl;
}
Student(){
cout << "Student constructor called!" <<endl; // Person's constructor is executed first then Student's constructor
}
~Student(){
cout << "Student deconstructor called!" <<endl; // Student's deconstructor is executed first then Person's deconstructor
}
};
int main()
{
Student MK;
MK.setName("Ken");
MK.setAge(20);
MK.setId(3072);
MK.introduce(); // John
return 0;
}