Вот пример задания:
Построить три класса (базовый и 2 потомка), описывающих некоторых работников с
почасовой оплатой (один из потомков) и фиксированной оплатой (второй потомок).
Описать в базовом классе абстрактный метод для расчета среднемесячной заработной
платы.
Упорядочить всю последовательность работников по убыванию
среднемесячного заработка.
C++ |
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
118
119
120
121
122
123
| #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <set>
using namespace std;
//Base class
class Employee
{
private:
string name;
int id;
protected:
double total_salary;
public:
Employee(const string& n, int i){
this->name = n;
this->id = i;
}
virtual void calc_salary() = 0;
void show(){cout << this->name <<" "<< this->id <<" "<< this->total_salary << endl;}
virtual void SetN(){
cout << "Name: ";
cin >> this->name;
cout << "id: ";
cin >> this->id;
}
virtual ~Employee(){}
friend bool operator < (const Employee& e, const Employee& e1){ // <---возможна ошибка здесь
if (e.total_salary == e1.total_salary) return e.name < e1.name;
return e.total_salary < e.total_salary;
}
friend ostream&operator<<(ostream& os, const Employee& e){
os << e.id << " " << e.name << " " << e.total_salary << endl;
return os;
}
};
//Monthly salary
class Employee_monthly : public Employee
{
private:
double m_salary;
public:
Employee_monthly(const string& n, int i, double m_sal) : Employee(n, i)
{
this->m_salary = m_sal;
};
virtual void calc_salary(){
this->total_salary = m_salary;
}
void SetN(){
Employee::SetN();
cout << "Salary: ";
cin >> m_salary;
}
};
//Hourly salary
class Employee_hourly : public Employee
{
private:
double h_salary;
public:
Employee_hourly(const string& n, int i, double h_sal) : Employee(n, i)
{
this->h_salary = h_sal;
}
virtual void calc_salary(){ this->total_salary = 20.8 * 8 * h_salary; }
void SetN(){
Employee::SetN();
cout << "Salary: ";
cin >> h_salary;
}
};
int main()
{
srand(time(NULL));
vector<Employee*>employees;
for (int i = 0; i < 8; i++){
int key = rand() % 2 + 1;
cout << key;
switch (key){
case 1:{
Employee_hourly* h = new Employee_hourly("Worker with horly salery", rand()%100+1, rand() % 20+8);
h->calc_salary();
employees.push_back(h);
break;
}
case 2:{
Employee_monthly* h = new Employee_monthly("Worker with monthly salery", rand()%100+1, rand() % 4000 + 2000);
h->calc_salary();
employees.push_back(h);
break;
}
}
}
cout << endl;
vector<Employee*>::iterator iter;
iter = employees.begin();
while (iter != employees.end()){
cout << *(*iter++);
}
cout << endl;
sort(employees.begin(), employees.end()); // <---возможна ошибка здесь
cout << endl;
cout << "Sorted:\n"<<endl;
iter = employees.begin();
while (iter != employees.end()){
cout << *(*iter++);
}
system("PAUSE");
return 0;
} |
|