-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathhr7.cpp
108 lines (82 loc) · 1.71 KB
/
hr7.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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person {
public:
static int i_prof;
static int i_student;
int age, cur_id;
string name;
virtual void putdata() {
cout << "hello" << endl;
};
virtual void getdata() {}
};
int Person::i_prof = 1;
int Person::i_student = 1;
int sum(int marks[6]) {
int total = 0;
for (int i = 0; i < 6; i++) total += marks[i];
return total;
}
class Professor: public Person {
public:
int publications;
void getdata();
void putdata();
};
void Professor::getdata() {
string n;
int a, p;
cin >> n;
cin >> a;
cin >> p;
name = n;
age = a;
publications = p;
cur_id = Person::i_prof;
Person::i_prof++;
}
void Professor::putdata() {
cout << name << " " << age << " " << publications << " " << cur_id << endl;
}
class Student: public Person {
public:
int marks[6];
void getdata();
void putdata();
};
void Student::getdata() {
string n;
int a;
cin >> n;
cin >> a;
for (int i=0; i < 6; i++) {
cin >> marks[i];
}
name = n;
age = a;
cur_id = Person::i_student;
Person::i_student++;
}
void Student::putdata() {
cout << name << " " << age << " " << sum(marks) << " " << cur_id << endl;
}
int main(){
int n, val;
cin>>n; //The number of objects that is going to be created.
Person *per[n];
for(int i = 0;i < n;i++){
cin>>val;
if(val == 1){
// If val is 1 current object is of type Professor
per[i] = new Professor;
}
else per[i] = new Student; // Else the current object is of type Student
per[i]->getdata(); // Get the data from the user.
}
for(int i=0;i<n;i++)
per[i]->putdata(); // Print the required output for each object.
return 0;
}