抽象类+基类指针访问派生类的练习
比较简单,锻炼一下熟练度和速度。
题目一:请设计一个描述杭电学校师生的抽象类 HDUPerson,要求有属性姓名、性别、年龄,有相应的构造函数与 Set、Get 方法,有成员函数 IntroduceSelf 实现自我介绍、纯虚函数GetCurrentNum,通过继承派生杭电学生类 Student和杭电教师类 Teacher, 学生类中含在学的课程数,在教师类中含每周授课时数,在派生类中必须实现GetCurrentNum 获取学生的课程数或教师每周授课时数。在主程序中通过HDUPerson 类的指针动态构造学生对象(属性为 Tom、男、18岁,目前在学课程6门)与教师对象(属性为 Mary、女、31,目前每周授课8学时),通过指针调用基类方法IntroduceSelf(自我介绍时能表明是教师或学生) 与 GetCurrentNum,并写出你测试程序的输出结果。
代码:
#include<iostream>
using namespace std;
class HDUPerson{
public:
HDUPerson(string n,string s,int a):name(n),sex(s),age(a){}
virtual void IntroduceSelf(){
cout<<"姓名:"<<name<<",性别:";
if(sex=="m"){
cout<<"男,";
}
else{
cout<<"女,";
}
cout<<"年龄"<<age<<endl;
}
virtual void GetCurrentNum() = 0;//纯虚函数
void set_name(string n){
name=n;
}
void set_sex(string s){
sex=s;
}
void set_age(int a){
age=a;
}
string get_name(){
return name;
}
string get_sex(){
return sex;
}
int get_age(){
return age;
}
private:
string name;
string sex;
int age;
};
class Student:public HDUPerson{
public:
Student(string n,string s,int a,int courses):HDUPerson(n,s,a),num_courses(courses){}
void IntroduceSelf(){
cout<<"我是学生,";
HDUPerson::IntroduceSelf();
}
void GetCurrentNum(){
cout<<"本学期我的在学课程有"<<num_courses<<"门"<<endl;
}
private:
int num_courses;
};
class Teacher:public HDUPerson{
public:
Teacher(string n,string s,int a,int courses):HDUPerson(n,s,a),teach_courses(courses){}
void GetCurrentNum(){
cout<<"本学期我的在授课程有"<<teach_courses<<"门";
}
void IntroduceSelf(){
cout<<"我是教师,";
HDUPerson::IntroduceSelf();
}
private:
int teach_courses;
};
int main(){
HDUPerson *p = new Student("Tom","m",18,6);
p->IntroduceSelf();
p->GetCurrentNum();
HDUPerson *q = new Teacher("Mary","w",31,8);
q->IntroduceSelf();
q->GetCurrentNum();
return 0;
}
运行结果:
题目二:设计一个抽象类Shape,派生两个类Circle和Rectangle,分别实现虚函数
calArea和calCirc,计算圆形和矩形的面积及周长。并写出测试代码,测试代码用基类指针访问派生类,并调用计算面积和周长的函数。
代码:
#include<iostream>
using namespace std;
const float PI=3.1415;
class Shape{
public:
virtual float calArea() const = 0;
virtual float calCirc() const = 0;
};
class Circle:public Shape{
private:
float r;
public:
Circle(float rr):r(rr){}
float calArea() const{
return PI*r*r;
}
float calCirc() const{
return 2*PI*r;
}
};
class Rectangle:public Shape{
private:
float a,b;
public:
Rectangle(float aa,float bb):a(aa),b(bb){}
float calArea() const{
return a*b;
}
float calCirc() const{
return 2*(a+b);
}
};
int main(){
//float rr;
//cin>>rr;
Shape *p = new Circle(5);
cout<<"圆的面积为:"<<p->calArea()<<endl;
cout<<"圆的周长为:"<<p->calCirc()<<endl;
//float a,b;
//cin>>a>>b;
Shape *q=new Rectangle(2,3);
cout<<"矩形的面积为:"<<q->calArea()<<endl;
cout<<"矩形的周长为:"<<q->calCirc()<<endl;
return 0;
}
运行结果:
题目三:编程实现小型公司的工资管理。该公司主要有4类人员:经理(manager)、兼职技术人员(technician)、销售员(salesman)和销售经理(salesmanager)。要求存储这些人员的编号、姓名和月工资,计算月工资并显示全部信息。月工资计算办法是:经理拿固定月薪8000元,兼职技术人员按每小时100元领取月薪,销售员按当月销售额的4%提成,销售经理既拿固定月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售总额的千分之五。兼职技术人员一个月工作小时数、销售员一个月销售额、销售经理所管辖部门一个月销售总额由各个类的成员函数完成设置。(要求用抽象类和类继承)
代码:
#include<iostream>
using namespace std;
class Person{
public:
Person(int n,string nam):num(n),name(nam){}
virtual void calculate_wage() = 0;
void show(){
calculate_wage();
cout<<"编号:"<<num<<",姓名:"<<name<<",工资:"<<wage<<endl;
}
protected:
int num;
string name;
double wage;
};
class manager:public Person{
public:
manager(int n,string nam):Person(n,nam){}
void calculate_wage(){
wage=8000;
}
};
class technician:public Person{
public:
technician(int n,string nam):Person(n,nam){}
void set_hour(int h){
hour=h;
}
void calculate_wage(){
wage = 100*hour;
}
private:
int hour;
};
class salesman:public Person{
public:
salesman(int n,string nam):Person(n,nam){}
void calculate_wage(){
wage=0.04*month_sale;
}
void set_month_sale(int m_s){
month_sale = m_s;
}
private:
int month_sale;
};
class salesmanager:public Person{
public:
salesmanager(int n,string nam):Person(n,nam){}
void calculate_wage(){
wage=5000+total_sales*0.005;
}
void set_total_sales(int t){
total_sales = t;
}
private:
int total_sales;
};
int main(){
manager zhang (1000, "张三");
technician li (1000, "李四");
salesman wang (1000, "王五");
salesmanager zhao (1000, "赵六");
li.set_hour(50);
wang.set_month_sale(175000);
zhao.set_total_sales(1500000);
Person *p[4]={&zhang,&li,&wang,&zhao};
for(int i=0;i<4;i++){
p[i]->show();
}
return 0;
}
运行结果: