设计一
#include<iostream>
using namespace std;
class Teacher{
protected:
int fixsalary;
int classhour;
public:
Teacher(int a,int b):fixsalary(a),classhour(b){}
void worktime(int a);
virtual int Mouthsalary()=0;
};
void Teacher::worktime(int a){
classhour+= a;
}
class Professor:public Teacher{
public:
Professor():Teacher(5000,0){}
int Mouthsalary();
};
int Professor::Mouthsalary(){
return classhour*50+fixsalary;
}
class Aprofessor:public Teacher{
public:
Aprofessor():Teacher(3000,0){}
int Mouthsalary();
};
int Aprofessor::Mouthsalary(){
return classhour*30+fixsalary;
}
class Lecturer:public Teacher{
public:
Lecturer():Teacher(2000,0){}
int Mouthsalary();
};
int Lecturer::Mouthsalary(){
return classhour*20+fixsalary;
}
int main(){
Teacher *p;
Professor s1;
p=&s1;
s1.worktime(10);
cout<<p->Mouthsalary()<<endl;
Aprofessor s2;
p = &s2;
s2.worktime(20);
cout<<p->Mouthsalary()<<endl;
Lecturer s3;
p=&s3;
s3.worktime(30);
cout<<p->Mouthsalary()<<endl;
return 0;
}
设计二
#include<iostream>
using namespace std;
class Teacher{
protected:
int fixsalary;//固定工资
int classhour;//课时
public:
Teacher(int a):fixsalary(a){}
void Classhour(int a);
virtual int Mouthsalary()=0;
};
void Teacher::Classhour(int a){
classhour= a;
}
class Professor:public Teacher{ //教授类
public:
Professor():Teacher(5000){}
int Mouthsalary();
};
int Professor::Mouthsalary(){
return classhour*50+fixsalary;
}
class Aprofessor:public Teacher{ //副教授类
public:
Aprofessor():Teacher(3000){}
int Mouthsalary();
};
int Aprofessor::Mouthsalary(){
return classhour*30+fixsalary;
}
class Lecturer:public Teacher{ //讲师类
public:
Lecturer():Teacher(2000){}
int Mouthsalary();
};
int Lecturer::Mouthsalary(){
return classhour*20+fixsalary;
}
int main(){
Teacher *p;
Professor s1;
p=&s1;
s1.Classhour(15);
cout<<p->Mouthsalary()<<endl;
Aprofessor s2;
p = &s2;
s2.Classhour(35);
cout<<p->Mouthsalary()<<endl;
Lecturer s3;
p=&s3;
s3.Classhour(50);
cout<<p->Mouthsalary()<<endl;
return 0;
}
设计三
#include<iostream>
#include<string.h>
using namespace std;
class Teacher{
protected:
int fixsalary;//固定工资
int classhour;//课时
char name[15];
int id;
public:
Teacher(int a,char *name,int id):fixsalary(a),id(id){
strcpy(name,name);
}
void Classhour(int a);
void print();
virtual int Mouthsalary()=0;
};
void Teacher::Classhour(int a){
classhour= a;
}
void Teacher::print(){
cout<<"编号:"<<id<<endl;
cout<<"姓名:"<<name<<endl;
}
class Professor:public Teacher{ //教授类
public:
Professor():Teacher(5000,"教授",1){
strcpy(name,"教授");
}
int Mouthsalary();
};
int Professor::Mouthsalary(){
return classhour*50+fixsalary;
}
class Aprofessor:public Teacher{ //副教授类
public:
Aprofessor():Teacher(3000,"副教授",2){
strcpy(name,"副教授");
}
int Mouthsalary();
};
int Aprofessor::Mouthsalary(){
return classhour*30+fixsalary;
}
class Lecturer:public Teacher{ //讲师类
public:
Lecturer():Teacher(2000,"讲师",3){
strcpy(name,"讲师");
}
int Mouthsalary();
};
int Lecturer::Mouthsalary(){
return classhour*20+fixsalary;
}
int main(){
Teacher *p;
Professor s1;
p=&s1;
s1.Classhour(15);
s1.print();
cout<<"月收入:"<<p->Mouthsalary()<<endl;
Aprofessor s2;
p = &s2;
s2.Classhour(35);
s2.print();
cout<<"月收入:"<<p->Mouthsalary()<<endl;
Lecturer s3;
p=&s3;
s3.Classhour(50);
s3.print();
cout<<"月收入:"<<p->Mouthsalary()<<endl;
return 0;
}