-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhr_virtual_functions.cpp
More file actions
69 lines (65 loc) · 1.7 KB
/
Copy pathhr_virtual_functions.cpp
File metadata and controls
69 lines (65 loc) · 1.7 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
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
class Person {
std::string name;
int age;
public:
Person() : name(), age() { }
virtual void getdata() { std::cin >> name >> age; }
virtual void putdata() { std::cout << name << " " << std::to_string(age); }
};
class Professor : public Person {
static int g_id;
int cur_id, publications;
public:
Professor() : Person(), cur_id(g_id), publications() { g_id += 1; }
void getdata() override {
Person::getdata();
std::cin >> publications;
}
void putdata() override {
Person::putdata();
std::cout << " " << std::to_string(publications)
<< " " << std::to_string(cur_id) << "\n";
}
};
int Professor::g_id = 1;
class Student : public Person
{
static int g_id;
int cur_id, marks[6];
public:
Student() : Person(), cur_id(g_id), marks() { g_id += 1; }
void getdata() override {
Person::getdata();
std::for_each(marks, marks+6, [](int& n) { std::cin >> n; });
}
void putdata() override {
int sum = 0;
for (auto n : marks)
sum += n;
Person::putdata();
std::cout << " " << std::to_string(sum)
<< " " << std::to_string(cur_id) << "\n";
}
};
int Student::g_id = 1;
int main() {
int nop; // number of persons
std::cin >> nop;
std::vector<Person *> persons(nop);
for (auto& p : persons) {
int person_type = 0;
std::cin >> person_type;
if (person_type == 1)
p = new Professor();
else if (person_type == 2)
p = new Student();
p->getdata();
}
for (auto p : persons)
p->putdata();
return 0;
}