-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance_lesson10.cpp
116 lines (94 loc) · 2.54 KB
/
inheritance_lesson10.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//Note-1:
/*
In C++ we can derive a class from another class, called a base.
The derived class can access members and functions of the base class.
We say the derived class has inherited from the base class. */
//main.cpp
#include"main.hpp"
int main()
{
//Create an instance of the derived class,
//GradStudent
GradStudent gs1;
//Set the id of the grad student
//using the function in the base class
gs1.setId(123456789);
//Set the degree of the grad student
//using the function in the derived class
gs1.setDegree("BSEE");
cout<<gs1.getId()<<" "<<gs1.getDegree();
return 0;
}
//main.hpp
#include<iostream>
#include<string>
using namespace std;
//The base class
class Student
{
private:
int id;
public:
void setId(int idIn);
int getId();
Student();
};
Student::Student()
{
id = 000000000;
}
void Student::setId(int idIn)
{
id = idIn;
}
int Student::getId()
{
return id;
}
//The derived class with Student as base class
class GradStudent : public Student
{
private:
string degree;
public:
GradStudent();
void setDegree(string degreeIn);
string getDegree();
};
GradStudent::GradStudent()
{
degree = "undelcared";
}
void GradStudent::setDegree(string degreeIn)
{
degree = degreeIn;
}
string GradStudent::getDegree()
{
return degree;
}
//Look at the line: class GradStudent: public Student
/*
The access control before the base class (in this case 'public') determines the access of the inherited class.
There are three types of access control: public, private, and protected.
Public Inheritance means all public members of the base class are accessible to the derived class
Private Inheritance means all members of the base class are private to the derived class
Protected Inheritance means all members of the base class are protected to the derived class.
It is very rare to have a protected or private inheritance,
the vast majority of the time inheritance is public. */
//for private inheritance:
//The derived class with Student as base class
class GradStudent : private Student
{
private:
string degree;
public:
GradStudent();
void setDegree(string degreeIn);
string getDegree();
void setStudentId(int idIn); //need this to access Student::setId()
int getStudentId(); //need this to access Student::getId()
};
//Note-2:
//Multiple Inheritance: C++ classes can inherit from more than one class.
// class TeachingAssociate: public Staff, public GradStudent