đây là code của mình
Mình muốn nhập từng thông tin cho lớp con khi dùng vector cho lớp cha( lớp cha trừu tượng) để tối ưu bộ nhớ
Nhưng không biết code như nào
Mong mọi người tư vấn giúp em
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Employee
{
protected:
string name;
double salary;
double commission;
public:
virtual double calculate_pay() = 0;
double total_pay()
{
return salary + commission;
}
virtual void input()
{
cin.ignore();
cout << " Enter name :";
getline(cin, name);
cout << " Enter salary :";
cin >> salary;
}
virtual void output()
{
cout << " Name :" << name << endl;
cout << " Salary :" << salary << endl;
cout << " Wage Pay :" << calculate_pay() << endl;
}
};
class Laborer : public Employee
{
private:
int hours_Work;
public:
double calculate_pay()
{
if (hours_Work > 50)
commission = 0.25*salary;
else commission = 0.1*salary;
return total_pay();
}
void input()
{
Employee::input();
cout << " Enter hours worked :";
cin >> hours_Work;
}
void output()
{
Employee::output();
cout << " Hours Worked :" << hours_Work << endl;
cout << " Wage Pay :" << calculate_pay() << endl;
}
};
class Scientist :public Employee
{
private:
int publication;
public:
double calculate_pay()
{
if (publication > 5)
commission = 0.25* salary;
else commission = 0.5*salary;
return total_pay();
}
void input()
{
Employee::input();
cout << " Enter publication :";
cin >> publication;
}
void output()
{
Employee::output();
cout << " Number of publication :" << publication << endl;
cout << " Wage Pay :" << total_pay() << endl;
}
};
class Manager :public Employee
{
private:
double business_amount;
public:
double calculate_pay()
{
if (business_amount > 100000)
commission = 0.15* business_amount;
else commission = business_amount;
return total_pay();
}
void input()
{
Employee::input();
cout << " Enter Business Amount :";
cin >> business_amount;
}
void output()
{
Employee::output();
cout << " Bussiness Amount : " << business_amount << endl;
cout << " Wage Pay :" << total_pay() << endl;
}
};
int main()
{
vector <Employee*>E;
Laborer L;
E.push_back(new Laborer() );
E.push_back(new Scientist());
E.push_back(new Manager());
int luachon, n1, n2, n3;
do
{
system("cls");
cout << " ____________MENU___________" << endl;
cout << " 1.Add new Laborer " << endl;
cout << " 2.Add new Scientist " << endl;
cout << " 3.Add new Manager " << endl;
cout << " 4.Print All " << endl;
cout << " 5.Search by Name " << endl;
cout << " 6.Delete " << endl;
cout << " 7.Exit" << endl;
cout << " Enter your choise :";
cin >> luachon;
switch (luachon)
{
case 1:
{
system("cls");
cout << " Number of Laborer :";
cin >> n1;
for (int i = 0; i < n1; i++)
{
cout << " Enter infomation of Laborer [" << i + 1 << "]" << endl;
E[i]->input();
cout << endl;
}
break;
}
case 2:
{
system("cls");
cout << " Number of Scientist :";
cin >> n2;
for (int i = 0; i < n2; i++)
{
cout << " Enter information of Scientist [" << i + 1 << "]" << endl;
E[i]->input();
cout << endl;
}
break;
}
case 3:
{
system("cls");
cout << " Number of Manager :";
cin >> n3;
for (int i = 0; i <n3; i++)
{
cout << " Enter information of Manager [" << i + 1 << "]" << endl;
E[i]->input();
cout << endl;
}
break;
}
case 4:
{
system("cls");
cout << " Laborer " << endl;
for (int i = 0; i < n1; i++)
{
E[i]->output();
}
cout << " Scientist " << endl;
for (int i = 0; i < n2; i++)
{
E[i]->output();
}
for (int i = 0; i < n3; i++)
{
E[i]->output();
}
system("pause");
break;
}
/*
case 5:break;
case 6:break;
case 7:break;
*/
}
} while (luachon > 0 || luachon < 8);
system("pause");
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?