实验4 继承与派生
实验步骤:
第一步:调试基类Person
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Person{
public:
void input();
void output();
protected:
int number;
string name;
string sex;
double salary;
};
void Person::input()
{
cout<<"工号:";cin>>number;
cout<<"姓名:";cin>>name;
cout<<"性别:";cin>>sex;
}
void Person::output()
{
cout<<"+--------------------------------+"<<endl;
cout<<"工号:";cout<<number<<endl;
cout<<"名字:";cout<<name<<endl;
cout<<"性别:";cout<<sex<<endl;
cout<<"+--------------------------------+"<<endl;
}
int main()
{
Person p1;
p1.input();
p1.output();
}
第二步:调试派生类Teacher
在上一步的基础上增加下面的代码:
class Teacher:public virtual Person{
public:
void input();
void output();
void pay();
protected:
int teaching_hours;
int title;
};
void Teacher::input()
{
Person::input();
cout<<"课时:";cin>>teaching_hours;
cout<<"1------教授"<<endl;
cout<<"2------副教授"<<endl;
cout<<"3------讲师"<<endl;
cout<<"4------助教"<<endl;
cout<<"请选择职称:";cin>>title;
}
void Teacher::output()
{
Person::output();
cout<<"课时:";cout<<teaching_hours;
cout<<"职称:";
if(title==1)
cout<<"教授"<<endl;
else if(title==2)
cout<<"副教授"<<endl;
else if(title==3)
cout<<"讲师"<<endl;
else if(title==4)
cout<<"助教"<<endl;
}
void Teacher::pay()
{
if(title==1)//教授
salary=teaching_hours*150+5000;
else if(title==2)//副教授
salary=teaching_hours*120+4000;
else if(title==3)//讲师
salary=teaching_hours*100+3000;
else if(title==4) //助教
salary=teaching_hours*80+2000;
cout<<"工资:"<<salary<<endl;
}
并将主函数修改为:
int main()
{
Teacher t1;
t1.input();
t1.output();
t1.pay();
}
第三步:调试派生类Temporary_workers
在上一步的基础上增加下面的代码:
class Temporary_worker:public virtual Person{
public:
Temporary_worker():hourlyPay(30){};
void input();
void pay();
private:
double hourlyPay;
int workingHours;
};
void Temporary_worker::input()
{
Person::input();
cout<<"当月工作时数:";cin>>workingHours;
}
void Temporary_worker::pay()
{
salary=workingHours*hourlyPay;
cout<<"当月薪水:"<<salary<<endl;
}
并将主函数修改为:
int main()
{
Temporary_worker tw1;
tw1.input();
tw1.output();
tw1.pay();
}
第四步:调试派生类Staff
在上一步的基础上增加下面的代码:
class Staff: public virtual Person{
public:
Staff(){};
void input();
void output();
void pay();
protected:
double position;//值位
};
void Staff::input()
{
Person::input();
cout<<"1------院级"<<endl;
cout<<"2------处级"<<endl;
cout<<"3------科级"<<endl;
cout<<"4------一般工作人员"<<endl;
cout<<"请选择值位:";cin>>position;
}
void Staff::output()
{
Person::output();
if(position==1)
cout<<"职位:院级"<<endl;
else if(position==2)
cout<<"职位:处级"<<endl;
else if(position==3)
cout<<"职位:科级"<<endl;
else if(position==4)
cout<<"职位:一般工作人员"<<endl;
}
void Staff::pay()
{
if(position==1)
salary=8000;
else if(position==2)
salary=6800;
else if(position==3)
salary=6600;
else if(position==4)
salary=5000;
cout<<"当月薪水:"<<salary<<endl;
}
第五步:调试Staff_Teacher类
class Staff_Teacher:public Staff,public Teacher{
public :
Staff_Teacher(){};
void input();
void output();
void pay();
};
void Staff_Teacher::input()
{
Teacher::input();
cout<<"1------院级"<<endl;
cout<<"2------处级"<<endl;
cout<<"3------科级"<<endl;
cout<<"4------一般工作人员"<<endl;
cout<<"请选择职位:";cin>>position;
}
void Staff_Teacher::output()
{
Teacher::output();
if(position==1)
cout<<"职位:院级"<<endl;
else if(position==2)
cout<<"职位:处级"<<endl;
else if(position==3)
cout<<"职位:科级"<<endl;
else if(position==4)
cout<<"职位:一般工作人员"<<endl;
}
void Staff_Teacher::pay()
{
Staff::pay();
if(title==1)//教授
salary=teaching_hours*1.5+5000+salary;
else if(title==2)//副教授
salary=teaching_hours*1.2+4000+salary;
else if(title==3)//讲师
salary=teaching_hours*1.2+3000+salary;
else //助教
salary=teaching_hours*1.2+2000+salary;
cout<<"工资:"<<salary<<endl;
}
主函数修改为:
int main()
{
Staff_Teacher st1;
st1.input();
st1.output();
st1.pay();
}
完整代码:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Person{
public:
void input();
void output();
protected:
int number;
string name;
string sex;
double salary;
};
void Person::input()
{
cout<<"工号:";cin>>number;
cout<<"姓名:";cin>>name;
cout<<"性别:";cin>>sex;
}
void Person::output()
{
cout<<"+--------------------------------+"<<endl;
cout<<"工号:";cout<<number<<endl;
cout<<"名字:";cout<<name<<endl;
cout<<"性别:";cout<<sex<<endl;
cout<<"+--------------------------------+"<<endl;
}
class Teacher:public virtual Person{
public:
void input();
void output();
void pay();
protected:
int teaching_hours;
int title;
};
void Teacher::input()
{
Person::input();
cout<<"课时:";cin>>teaching_hours;
cout<<"1------教授"<<endl;
cout<<"2------副教授"<<endl;
cout<<"3------讲师"<<endl;
cout<<"4------助教"<<endl;
cout<<"请选择职称:";cin>>title;
}
void Teacher::output()
{
Person::output();
cout<<"课时:";cout<<teaching_hours;
cout<<"职称:";
if(title==1)
cout<<"教授"<<endl;
else if(title==2)
cout<<"副教授"<<endl;
else if(title==3)
cout<<"讲师"<<endl;
else if(title==4)
cout<<"助教"<<endl;
}
void Teacher::pay()
{
if(title==1)//教授
salary=teaching_hours*150+5000;
else if(title==2)//副教授
salary=teaching_hours*120+4000;
else if(title==3)//讲师
salary=teaching_hours*100+3000;
else if(title==4) //助教
salary=teaching_hours*80+2000;
cout<<"工资:"<<salary<<endl;
}
class Temporary_worker:public virtual Person{
public:
Temporary_worker():hourlyPay(30){};
void input();
void pay();
private:
double hourlyPay;
int workingHours;
};
void Temporary_worker::input()
{
Person::input();
cout<<"当月工作时数:";cin>>workingHours;
}
void Temporary_worker::pay()
{
salary=workingHours*hourlyPay;
cout<<"当月薪水:"<<salary<<endl;
}
class Staff: public virtual Person{
public:
Staff(){};
void input();
void output();
void pay();
protected:
double position;//值位
};
void Staff::input()
{
Person::input();
cout<<"1------院级"<<endl;
cout<<"2------处级"<<endl;
cout<<"3------科级"<<endl;
cout<<"4------一般工作人员"<<endl;
cout<<"请选择值位:";cin>>position;
}
void Staff::output()
{
Person::output();
if(position==1)
cout<<"职位:院级"<<endl;
else if(position==2)
cout<<"职位:处级"<<endl;
else if(position==3)
cout<<"职位:科级"<<endl;
else if(position==4)
cout<<"职位:一般工作人员"<<endl;
}
void Staff::pay()
{
if(position==1)
salary=8000;
else if(position==2)
salary=6800;
else if(position==3)
salary=6600;
else if(position==4)
salary=5000;
cout<<"当月薪水:"<<salary<<endl;
}
class Staff_Teacher:public Staff,public Teacher{
public :
Staff_Teacher(){};
void input();
void output();
void pay();
};
void Staff_Teacher::input()
{
Teacher::input();
cout<<"1------院级"<<endl;
cout<<"2------处级"<<endl;
cout<<"3------科级"<<endl;
cout<<"4------一般工作人员"<<endl;
cout<<"请选择职位:";cin>>position;
}
void Staff_Teacher::output()
{
Teacher::output();
if(position==1)
cout<<"职位:院级"<<endl;
else if(position==2)
cout<<"职位:处级"<<endl;
else if(position==3)
cout<<"职位:科级"<<endl;
else if(position==4)
cout<<"职位:一般工作人员"<<endl;
}
void Staff_Teacher::pay()
{
Staff::pay();
if(title==1)//教授
salary=teaching_hours*1.5+5000+salary;
else if(title==2)//副教授
salary=teaching_hours*1.2+4000+salary;
else if(title==3)//讲师
salary=teaching_hours*1.2+3000+salary;
else //助教
salary=teaching_hours*1.2+2000+salary;
cout<<"工资:"<<salary<<endl;
}
int main()
{
Staff_Teacher st1;
st1.input();
st1.output();
st1.pay();
}
日期:2020年3月19日