-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtual Functions.cpp
More file actions
83 lines (62 loc) · 1.58 KB
/
Virtual Functions.cpp
File metadata and controls
83 lines (62 loc) · 1.58 KB
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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class Person {
public:
virtual void getdata() {
};
virtual void putdata() {
};
};
static int s_count = 1;
static int p_count = 1;
class Professor : public Person {
private:
int publications;
int cur_id;
string name;
int age;
public:
void getdata() {
cur_id = p_count++;
cin >> name >> age >> publications;
};
void putdata(){
cout << name << " " << age << " " << publications << " " << cur_id << endl;
};
};
class Student : public Person {
private:
int marks[6];
int cur_id;
string name;
int age;
public:
void getdata() {
cur_id = s_count++;
cin >> name >> age >> marks[0] >> marks[1] >> marks[2] >> marks[3] >> marks[4] >> marks[5];
};
void putdata() {
cout << name << " " << age << " " << marks[0]+ marks[1]+ marks[2]+ marks[3]+ marks[4]+ marks[5] << " " << 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;
}