类的重用

实验、类的重用

【实验目的】

1、理解组合和继承两种类的重用方法的概念和各自适用的情况;

2、掌握C++中以组合方式定义新类及构造对象的方法;

3、掌握C++中以继承方式定义新类及构造对象的方法;

4、掌握多重继承和虚基类。

【实验内容】

设计一个用于人事管理的“People(人员)”基类。考虑到通用性,仅抽象出各类人员都具有的属性 : 编号、姓名、性别、出生日期、身份证号等; 从People(人员)类派生出学生类Student,并添加属性: 班号classNO; 从People类派生出教师)类Teacher, 并添加属性: 职务principalship、部门department;从Student 类中派生出Graduate(研究生)类,添加属性:专业subject、导师adviser (Teacher 类对象); 从Graduate 类和Teacher类派生出助教生类TA。设计时注意虚基类的使用,注意重载相应的成员函数。编写main函数测试这些类。在main 函数中设计测试用例时,注意考虑如何体现成员函数的覆盖。老师将根据设计封装的合理性、测试用例周全性进行评分。

【UML图】

 

【程序清单】

#include<iostream>

#include<string>

usingnamespace std;

//Date

classDate

{

public:

  Date(int year,int month,int day);

  void Show();

private:

  intyear,month,day;

};

 

Date::Date(int year,int month,int day):year(year),month(month),day(day)

{

}

void Date::Show()

{

  cout<<year<<"-"<<month<<"-"<<day<<endl;

}

 

//People

classPeople

{

public:

   People(string id,string name,string gender,Date birthday,string ID_num);

   virtualvoid Show();

   virtual ~People();

private:

   stringid,name,gender,ID_num;

   Datebirthday;

};

 

People::People(string id,string name,string gender,Date birthday,string ID_num):id(id),name(name),gender(gender),ID_num(ID_num),birthday(birthday)

{

}

People::~People()

{

}

void People::Show()

{

   cout<<"编号:"<<id<<endl;

   cout<<"姓名:"<<name<<endl;

   cout<<"性别:"<<gender<<endl;

   cout<<"出生日期:";

   birthday.Show();

   cout<<"身份证号:"<<ID_num<<endl;

}

 

//Student

classStudent:virtualpublicPeople

{

public:

   Student(People& p,string classNo);

   void Show();

private:

   stringclassNo;

};

 

Student::Student(People& p,string classNo):People(p),classNo(classNo)

{

}

void Student::Show()

{

   People::Show();

   cout<<"班号:"<<classNo<<endl;

}

 

//Teacher

classTeacher:virtualpublicPeople

{

public:

   Teacher(People& p,string principalship,string department);

   string& GetPrincipalship();

   string& GetDepartment();

   void Show();

private:

   stringprincipalship,department;

};

 

Teacher::Teacher(People& p,string principalship,string department):People(p),principalship(principalship),department(department)

{

}

string& Teacher::GetPrincipalship()

{

  returnprincipalship;

}

string& Teacher::GetDepartment()

{

  returndepartment;

}

void Teacher::Show()

{

   People::Show();

   cout<<"职务:"<<principalship<<endl;

   cout<<"部门:"<<department<<endl;

}

 

//Graduate

classGraduate:publicStudent

{

public:

   Graduate(People& p,Student& s,string subject,Teacher& adviser);

   void Show();

private:

   stringsubject;

   Teacheradviser;

};

 

Graduate::Graduate(People& p,Student& s,string subject,Teacher& adviser):People(p),Student(s),subject(subject),adviser(adviser)

{

}

void Graduate::Show()

{

   Student::Show();

   cout<<"专业:"<<subject<<endl;

   cout<<"导师:"<<endl;

   adviser.Show();

}

 

//TA

classTA:publicGraduate,publicTeacher

{

public:

   TA(People& p,Graduate& g,Teacher& t);

   void Show();

};

 

TA::TA(People& p,Graduate& g,Teacher& t):People(p),Graduate(g),Teacher(t)

{

}

void TA::Show()

{

   cout<<"助教信息:"<<endl;

   Graduate::Show();

   cout<<endl;

   cout<<"助教职务:"<<Teacher::GetPrincipalship()<<endl;

   cout<<"助教部门:"<<Teacher::GetDepartment()<<endl;;

}

 

 

int main()

{

   People p1("201308002001","张三","",Date(1994,1,1),"420001199401012342");

   People p2("07005","王五","",Date(1980,12,12),"126174198012121255");

   Student s(p1,"1");

   Teacher t1(p1,"助教","计算机");

   Teacher t2(p2,"教授","计算机");

   Graduate g(p1,s,"计算机软件与理论",t2);

   TA ta(p1,g,t1);

   ta.Show();

   cout<<"____________________________________________"<<endl<<endl;

 

   Teacher* pt=&ta;//向上转型

   pt->Show();//虚函数,函数覆盖,调用taShow函数

 

   return 0;

}

 

【运行结果截图】

 

【实验小结】

1、充分利用类的重用,用一个类做另一个类的成员;

2、学会类的多重继承;

3、注意充分运用基类的函数,实现代码的重用,而非代码的重复;

4、注意虚基类的运用,特别关注其初始化方式,采用虚基类可以避免二义性的产生;

5、学会使用虚函数,从而实现函数的覆盖,但要注意它的前提是向上转型;

6、注意类封装的完整性;


猜你喜欢

转载自blog.csdn.net/qq_40333952/article/details/80412834