-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigQ2.txt
117 lines (104 loc) · 3.61 KB
/
BigQ2.txt
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
115
116
117
#include <iostream>
#include <string>
using namespace std;
// base class called student
class Student {
private:// private members to store the value of student name and Enrollment year
string name;
int enrollmentYear;
public:
// default constructor
Student() {
}
// using this keyword to tell compiler that u have to assign the value to this member
//peramatric constructor
Student(string name, int enrollmentYear) {
this->name = name;
this->enrollmentYear = enrollmentYear;
}
// get function returning name
string getName() {
return name;
}
// get function returning Enrollment year
int getEnrollmentYear() {
return enrollmentYear;
}
/************set funtions***************/
//set function for assigning name value
void setName(string name) {
this->name = name;
}
//set() for assigning year value to the private member
void setEnrollmentYear(int enrollmentYear) {
this->enrollmentYear = enrollmentYear;
}
/*****************************************/
// virtual print function for printing massage
virtual void print() {
cout << "StudentName: " << name << endl;
cout << "Enrollment Year: " << enrollmentYear << endl;
}
};
//child or subclass inherited from the base class called student
class KpuStudent : public Student {
private:
string campusLocation;//private member for store the location value of campus
public:
//KpuStudent peramtric constructor with student constructor
KpuStudent(string name, int enrollmentYear, string campusLocation) : Student(name, enrollmentYear) {
this->campusLocation = campusLocation;
}
// using get function for getting campus location
string getCampusLocation() {
return campusLocation;
}
void setCampusLocation(string campusLocation) {
this->campusLocation = campusLocation;
}
//print function from the base class with override massage
void print() {
cout << "StudentName: " << getName() << endl;
cout << "Campus Location: " << campusLocation << endl;
}
};
/************hogwartsStudent subclass*****************/
//inherited class from the base student class
class HogwartsStudent : public Student {
private:
string houseAffiliation;//private member
public://local peramatric constructor
HogwartsStudent(string name, int enrollmentYear, string houseAffiliation) : Student(name, enrollmentYear) {
this->houseAffiliation = houseAffiliation;
}
//get function for get houseAffiliation
string getHouseAffiliation() {
return houseAffiliation;
}
//set function
void setHouseAffiliation(string houseAffiliation) {
this->houseAffiliation = houseAffiliation;
}
// print function from the base class with override massage
void print() {
cout << "StudentName: " << this->getName() << endl;
cout << "House Affiliation: " << houseAffiliation << endl;
}
};
int main() {
//initiating base class peramatric constructor and passing its arguments value
Student student/*Base class object*/ = Student("Pritpal Singh", 2021);
KpuStudent kpuStudent/*sub class object*/ = KpuStudent("Pritpal Singh", 2021, "Surrey");// initiating KpuStudent sub class constructor and passing its arguments value
HogwartsStudent hogwartsStudent/*object*/ = HogwartsStudent("Pritpal Singh", 2021, "Ravenclaw");//intitiating hogwartsStudent sub class Constructor and passing its argumets value
Student *students[3];//creating pointer array of base class
/*passing refrence values of class objects to the students array elements*/
students[0] = &student;//
students[1] = &kpuStudent;
students[2] = &hogwartsStudent;
//printing only virtual print funtion from every class using for loop a
for(int i = 0; i < 3; i++) {
students[i]->print();
cout<<"_________________________________"<<endl;//adding line and space
}
return 0;
}