继承与派生(二)实验报告
- 格式:doc
- 大小:94.50 KB
- 文档页数:9
c 继承与派生实验报告
C 继承与派生实验报告
实验目的:通过实验,掌握C语言中继承与派生的概念和使用方法,加深对面向对象编程的理解。
实验内容:在C语言中,继承与派生是面向对象编程中非常重要的概念。
在本次实验中,我们将通过一个简单的例子来演示C语言中的继承与派生的用法。
首先,我们定义一个基类(父类)Person,包括姓名和年龄两个成员变量,以及一个显示信息的成员函数。
然后,我们定义一个派生类(子类)Student,继承自Person类,新增一个成员变量学号,并重写显示信息的成员函数。
在实验中,我们首先创建一个Person对象,设置姓名和年龄,然后调用显示信息函数,观察结果。
接着,我们创建一个Student对象,设置姓名、年龄和学号,再次调用显示信息函数,观察结果。
实验结果:通过实验,我们成功实现了C语言中的继承与派生。
我们发现,通过继承,子类Student可以直接使用父类Person中的成员变量和成员函数,同时可以新增自己的成员变量和函数。
这样的设计使得代码更加简洁和灵活,提高了代码的复用性和可维护性。
结论:C语言中的继承与派生是面向对象编程中非常重要的概念,通过本次实验,我们深入理解了这一概念的用法和意义。
掌握了继承与派生的方法后,我们可以更加灵活地设计和编写程序,提高代码的质量和效率。
总结:通过本次实验,我们对C语言中的继承与派生有了更深入的理解,加深了对面向对象编程的认识。
在今后的学习和工作中,我们将更加灵活地运用继承与派生的方法,提高代码的质量和效率。
C 继承与派生实验报告C 继承与派生实验报告引言:在面向对象的编程中,继承与派生是重要的概念。
通过继承,我们可以从已有的类中派生出新的类,并且可以在新的类中添加额外的属性和方法。
本实验旨在通过实际的编程实践,深入理解C语言中的继承与派生。
实验过程:首先,我们创建了一个基类Animal,其中包含了一个成员变量name和一个成员函数eat。
然后,我们创建了两个派生类Dog和Cat,它们分别继承了Animal类,并且在其中添加了各自特有的成员函数bark和meow。
接着,我们创建了一个对象dog和一个对象cat,并分别调用了它们的成员函数eat、bark 和meow。
实验结果:通过运行程序,我们可以看到dog对象调用了eat和bark函数,而cat对象调用了eat和meow函数。
这说明继承与派生的机制正常工作,派生类可以继承基类的属性和方法,并且可以在派生类中添加新的属性和方法。
实验分析:继承与派生是面向对象编程的重要概念,它可以使得代码的复用性更高,同时也增加了代码的灵活性。
通过继承,派生类可以继承基类的属性和方法,这样可以减少代码的冗余,并且可以在派生类中添加新的功能。
在本实验中,Dog 和Cat类分别继承了Animal类,这样它们就拥有了相同的属性name和方法eat。
然后,通过在派生类中添加新的方法bark和meow,我们可以实现不同的行为。
继承与派生的应用:继承与派生在实际的软件开发中有着广泛的应用。
例如,在一个图形界面程序中,可以定义一个基类Widget,它包含了一些基本的属性和方法,然后可以通过派生类Button和TextBox来创建具体的按钮和文本框。
这样,我们可以通过继承和派生的方式,实现不同的界面元素,并且可以在派生类中添加新的功能,如按钮的点击事件和文本框的输入验证。
继承与派生的注意事项:在使用继承与派生的过程中,我们需要注意一些问题。
首先,派生类可以访问基类的公有成员,但不能访问基类的私有成员。
西安财经学院信息学院《面向对象方法及程序设计》 实验报告实验名称 继承与派生 实验室 519 实验日期 12.23继承与派生一、实验目的与要求1. 进一步巩固C++语言中类和对象的概念和应用。
2. 掌握继承和派生的概念和实现。
3. 进一步熟练掌握类和对象的概念,使用的方法,访问的规则。
4. 掌握单继承的概念和应用。
5. 掌握多继承和虚基类的概念,并熟练应用。
二、实验内容1.根据如图所示编辑程序,计算教师的课时,计算学生的平均成绩,假定每个学生3门课程,并输出每个类的信息(例如教师的职称,学生的专业等,程序实现要求使用到虚基类的知识)。
2. 编写一个程序实现员工的工资管理。
该公司主要有4类人员,经理(manager ),销售经理(salesmanager),技术人员(technician),销售员(salesman)。
这些人员都是职员(employee ),有编号,姓名,月工资,工龄等信息。
月工资的计算方法为:经理固定月薪8000元,技术人员每小时工资100元,销售人员底薪为1000,然后加上每月的销售额的4%,销售经理底薪5000,然后加上本部门当月销售总额的千分之五。
要求编写程序计算该公司职员的月工资并输出到屏幕上。
(假定该公司1个经理,1个销售经理,3个技术人员,3个销售人员)三、实验环境 硬件环境:PC 一台姓名 学号 班级 年级 指导教师 李翠软件环境:WIN7操作系统、Microsoft visual c++ 2010 四、实验步骤五、实验结果六、小结通过本次实验,使我对继承与派生有了更深入的了解。
包括,虚基类以及虚基类与派生类的构造函数与析构函数的调用等等。
七、源程序清单内容1:#include"iostream"using namespace std;class person{};class teacher:virtual public person{public:int b;};class teacher1:virtual public teacher{public:teacher1(int B){b=B;cout<<"教授"<<endl;cout<<"课时:"<<b<<endl;}};class teacher2:virtual public teacher{public:teacher2(int D){b=D;cout<<"讲师"<<endl;cout<<"课时:"<<b<<endl;}};class teacher3:virtual public teacher{public:teacher3(int F){b=F;cout<<"研究生助教"<<endl;cout<<"课时:"<<b<<endl;}};class student:public person{public:float a1,a2,a3;};class student1:public student{public:student1(float b1,float b2,float b3){a1=b1;a2=b2;a3=b3;cout<<"英语:"<<a1<<endl;cout<<"数据库:"<<a2<<endl;cout<<"java:"<<a3<<endl;}};class student2:public student{public:student2(float b1,float b2,float b3){a1=b1;a2=b2;a3=b3;cout<<"本科生:"<<endl;cout<<"英语:"<<a1<<endl;cout<<"组成原理:"<<a2<<endl;cout<<"c++:"<<a3<<endl;}};class student3:public student{public:student3(float b1,float b2,float b3){a1=b1;a2=b2;a3=b3;cout<<"专科生:"<<endl;cout<<"英语:"<<a1<<endl;cout<<"c语言:"<<a2<<endl;cout<<"数字电路:"<<a3<<endl;}};class zhuyan:public teacher3,public student1{public:zhuyan(int x,float a,float b,float c):teacher3(x),student1(a,b,c) {}};int main(){teacher1 q(50);teacher2 w(70);student2 e(75,82,100);student3 r(78,79,87);zhuyan t(80,70,78,81);system("pause");return 0;}内容2:#include <iostream>using namespace std;class employee{public:employee(){cout<<"编号:";cin>>number;cout<<"姓名:";cin>>name;salary=0;}protected:char number[5];char name[10];double salary;};class manager:public employee{public:manager(){monthlypay=8000;salary=monthlypay;}void print(){cout<<"经理:"<<name<<"编号:"<<number<<"本月工资:"<<salary<<endl;} protected:int monthlypay;};class technician:public employee{public:technician(){weekpay=100;}void pay(){cout<<name<<"工作时间:";cin>>workhour;salary=workhour*100;}void print(){cout<<"技术人员:"<<name<<"编号:"<<number<<"本月工资:"<<salary<<endl;} protected:int weekpay;int workhour;};class salesman:public employee{public:salesman(){basicsalary=1000;commrate=0.04;}void pay(){cout<<name<<"本月销售额:";cin>>sales;salary=basicsalary+sales* commrate;}void print(){cout<<"销售员:"<<name<<"编号:"<<number<<"本月工资:"<<salary<<endl;} protected:int basicsalary;double commrate;double sales;};class salesmanager:public salesman{public:salesmanager(){monthlypay=5000;commrate=0.005;}void pay(){cout<<name<<"本月部门销售额:";cin>>sales;salary=monthlypay+sales* commrate;}void print(){cout<<"销售经理:"<<name<<"编号:"<<number<<"本月工资:"<<salary<<endl;} private:double monthlypay;};int main(){manager obj1;obj1.print();technician obj2,obj3,obj4;obj2.pay(); obj2.print();obj3.pay(); obj3.print();obj4.pay(); obj4.print();salesman obj5,obj6,obj7;obj5.pay(); obj5.print();obj6.pay(); obj6.print();obj7.pay(); obj7.print();salesmanager obj8;obj8.pay(); obj8.print();system("pause"); }。
实验2 派生类与继承实验课程名:面向对象程序设计(C++)专业班级:学号::实验时间:实验地点:指导教师:二、实验内容一、构造一个类Geometry 及其派生类,该类主要实现关于几何图形的基本操作。
对于基类“几何图形”,有求面积、求体积的函数(纯虚函数),其派生类圆和矩形主要有初始化(构造函数),求面积,求周长操作,类圆的派生类圆球和圆柱有求表面积、体积操作。
试在主函数中分别定义圆、圆球、圆柱以及矩形的对象,并调用其成员函数实现其相应操作。
实验代码如下:#include<iostream>using namespace std;class Geometry{public:CircleradiumsCircle()~Circle() BallBall()~Ball() GeometryGeometry()~Geometry()GetArea()GetPerimeter()Getcolume()show()Column Column()~Column()Rectangle Rectangle() ~Rectangle()Column column(1,2,3);column.show();return 0;}运行结果:代码分析:1)首先定义基类Geometry,在定义基类的派生类Circle,Rectangle再定义以Circle,Rectangle为基类的派生类Column,以及以Circle为基类的派生类Ball;2)在定义派生类时用构造函数初始化私有成员;3)最后用类的对象来调用类函数;二、设计如下类:(1)建立一个Point类,表示平面中的一个点;建立一个Line类,表示平面中的一条线端,内含两个Point类的对象;建立Triangle类,表示一个三角形,内含三个Line类的对象构成一个三角形。
(2)设计三个类的相应的构造函数、复制构造函数,完成初始化和对象复制(3)设计Triangle类的成员函数完成三条边是否能构成三角形的检验和三角形面积计算,面积显示。
实验目的与要求:1.掌握类的继承与派生关系以及实验方法,理解类的层次结构。
2.掌握派生类构造函数初始化基类成员和对象成员的方法。
3.掌握内联函数和默认函数。
4.掌握赋值兼容原则,掌握派生类的复制构造函数和赋值运算符的定义。
实验过程及内容:1.实践教程实验二十二P81范例:定义一个继承与派生关系的类体系,在派生类中访问基类成员。
①先定义一个点类,包含x,y坐标数据成员,显示函数和计算面积的函数成员;②以点为基类派生一个圆类,增加表示半径的数据成员,重载显示和计算面积的函数;③定义一个线段类,以两个点类对象作数据成员,定义显示、求面积及长度函数,线段类采用聚合方式,因为有两个端点,不能用派生。
编程测试所定义的类体系。
本实验教程中有源码,请自行运行,体会和熟悉继承与派生的基本概念及实现方法,掌握派生类构造函数初始化基类成员和对象成员的方法等。
2. 实践教程P83编程:多层派生练习,由上题Point类和Circle类继续派生出Cylinder类。
要求计算圆柱的底面积、侧面积、全面积和体积。
请编写所有完整的成员函数,并编写主函数进行验证。
数据处理1.(1)(2)j结果报错,原因是派生类中的成员函数不能访问基类中的私有成员。
(3)在Line类中添加两个数据成员。
2. #include <iostream>#include <cmath>using namespace std;#define PI 3.14159class Point{friend class Line;protected:double x, y ;public:Point(){x = 0 ; y = 0 ; }Point(double xv,double yv){ x = xv; y = yv; }double Area(){return 0;}void Show() {cout<<"x="<<x<<' '<<"y="<<y<<endl;}};class Circle :public Point{protected:double radius;public:Circle(){ x = 0; y = 0; radius = 0; }Circle(double xv,double yv,double vv):Point(xv,yv){ //调用基类构造函数radius = vv;}Circle(Circle & cir):Point(cir){ //按赋值兼容规则cir可为Point实参radius=cir.radius;}Circle & operator=(Circle & cir){this->Point::operator=(cir); //在派生类中定义重载的拷贝赋值操作符有固定的标准格式radius=cir.radius;return *this;}double Area(){return PI*radius*radius;}void Show()cout<<"x="<<x<<' '<<"y="<<y<<" radius="<<radius<<endl; //访问基类的数据成员}};class Cylinder:public Circle {double high;public:Cylinder(){ x = 0; y = 0; radius = 0;high=0; }Cylinder(double xv,double yv,double vv,double kv):Circle(xv,yv,vv){ //调用基类构造函数high=kv;}Cylinder(Cylinder & cyl):Circle(cyl){ //按赋值兼容规则cyl可为Cylinder实参high=cyl.high;}Cylinder & operator=(Cylinder & cyl){this->Circle :: operator=(cyl); //在派生类中定义重载的拷贝赋值操作符有固定的标准格式high=cyl.high;return *this;}double ceArea(){return 2*PI*radius*high;}double quArea(){return ceArea()+2* Area();}double volume(){return Area()*high;}void Show(){cout<<"x="<<x<<' '<<"y="<<y<<' '<<"radius="<<radius<<' '<<"high="<<high<<endl; //访问基类的数据成员};class Line{Point start,end; //对象成员public:Line(){} //对象成员初始化Line(double xv1,double yv1,double xv2,double yv2) :start(xv1,yv1),end(xv2,yv2){ }double GetLength() {return sqrt((start.x-end.x)*(start.x-end.x)+(start.y-end.y)*(start.y-end.y));}double Area(){return 0;}void Show(){cout<<"start point:\n";start.Show();cout<<"end point:\n";end.Show();}};int main(){Point pt(0,0);Circle cl1(100,100,10),cl2(cl1),cl3;Cylinder h1(50,50,20,30),h2(h1),h3;Line ln1(0,0,100,100),ln2;cout<<"点面积:"<<pt.Area()<<endl;pt.Show();cout<<"cl1圆面积:"<<cl1.Area()<<endl;cl1.Show();cout<<"cl2圆面积:"<<cl2.Area()<<endl;cl2.Show();cl3=cl1;cout<<"cl3圆面积:"<<cl3.Area()<<endl;cl3.Show();cout<<"h1底面积:"<<h1.Area()<<endl;cout<<"h1侧面积:"<<h1.ceArea()<<endl;cout<<"h1全面积:"<<h1.quArea()<<endl;cout<<"h1体积:"<<h1.volume()<<endl;h1.Show();cout<<"h2底面积:"<<h2.Area()<<endl;cout<<"h2侧面积:"<<h2.ceArea()<<endl;cout<<"h2全面积:"<<h2.quArea()<<endl;cout<<"h2体积:"<<h2.volume()<<endl;h2.Show();h3=h1;cout<<"h3底面积:"<<h3.Area()<<endl;cout<<"h3侧面积:"<<h3.ceArea()<<endl;cout<<"h3全面积:"<<h3.quArea()<<endl;cout<<"h3体积:"<<h3.volume()<<endl;h3.Show();cout<<"线面积:"<<ln1. Area()<<'\t'<<"线长度:"<<ln1. GetLength()<<endl;ln1.Show();ln2.Show();return 0;}实验结论:通过这次实验,我对类的继承和派生,派生类构造函数初始化基类成员和对象成员的方法,以及赋值兼容原则有了更深的理解。
一、实验目的1. 理解继承的概念及其在面向对象编程中的重要性。
2. 掌握不同继承方式(公有继承、私有继承、保护继承)下的基类成员在派生类中的访问权限。
3. 熟悉构造函数和析构函数在继承过程中的调用顺序和时机。
4. 培养动手实践能力,提高编程水平。
二、实验内容1. 创建一个基类`Base`,包含三个成员变量:`int`类型的`a`,`float`类型的`b`和`char`类型的`c`。
2. 创建三个派生类`DerivedPublic`,`DerivedPrivate`和`DerivedProtected`,分别采用公有继承、私有继承和保护继承的方式继承自基类`Base`。
3. 在每个派生类中添加一个构造函数,用于初始化基类成员和派生类特有的成员变量。
4. 在每个派生类中添加一个成员函数`display`,用于打印基类成员和派生类成员变量的值。
5. 在主函数中创建基类对象和派生类对象,并调用`display`函数打印成员变量的值。
三、实验步骤1. 创建基类`Base`,包含三个成员变量和相应的构造函数。
```cppclass Base {public:int a;float b;char c;Base(int a, float b, char c) : a(a), b(b), c(c) {}};```2. 创建三个派生类,分别采用公有继承、私有继承和保护继承的方式继承自基类`Base`。
```cppclass DerivedPublic : public Base {public:int d;DerivedPublic(int a, float b, char c, int d) : Base(a, b, c), d(d) {}void display() {cout << "DerivedPublic: a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;}};class DerivedPrivate : private Base {public:int d;DerivedPrivate(int a, float b, char c, int d) : Base(a, b, c), d(d) {}void display() {cout << "DerivedPrivate: a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;}};class DerivedProtected : protected Base {public:int d;DerivedProtected(int a, float b, char c, int d) : Base(a, b, c), d(d) {}void display() {cout << "DerivedProtected: a = " << a << ", b = " << b << ", c = " << c << ", d = " << d << endl;}};```3. 在主函数中创建基类对象和派生类对象,并调用`display`函数打印成员变量的值。
实验6 继承与派生一、实验目的1.理解继承与派生、单继承与多继承的概念;2.理解基类与派生类的定义及使用方法,派生类对象的定义与初始化方法;3.理解继承与派生过程中,把派生类作为基类构成类族的概念及虚基类的概念。
二、实验环境一台PC机,Windows XP操作系统,Visual C++ 6.0开发环境。
三、实验内容1、由在校人员类(Person)作为基类派生出学生类(Student):实验步骤:#include <iostream>#include <string>using namespace std;class Person{public:Person(int i,char *n, char s, int a){ID=i;name=n;sex=s;age=a;};int getID(){return ID;}void show(){cout<<"ID: "<<ID<<endl;cout<<"name : "<<name<<endl;cout<<"sex: "<<sex<<endl;cout<<"age: "<<age<<endl;}private:int ID;string name;char sex;int age;};class Student:public Person{public:Student(int i,char *n,char s,int a,float m,float p,float e,float c):Person(i,n,s,a){math=m;physical=p;english=e;cpp=c;total=math+physical+english+cpp;}void show(){Person::show();cout<<"math: "<<math<<endl;cout<<"physical: "<<physical<<endl;cout<<"english: "<<english<<endl;cout<<"C++: "<<cpp<<endl;cout<<"total: "<<total<<endl;}private:float math,physical,english,cpp,total;};void main(){Person p1(1,"张帅",'M',22);p1.show();cout<<endl;Student s1(9901,"林维",'S',21,65,70,75,88);s1.show();}实验结果:2、由学生类、课程类作为基类,共同派生出选课类。
《C++程序设计》实验报告Exercise7继承与派生1实验目的(1)学习定义和使用类的继承关系,定义派生类。
(2)熟悉不同继承方式下对基类成员的访问控制。
(3)学习利用虚基类解决二义性问题。
2实验要求(1)定义一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还回有问题吗?编程试之。
源程序代码:#include<iostream>using namespace std;class Animal{public:Animal(){age=0;cout<<"构造了Animal对象"<<endl;}~Animal(){cout<<"析构了Animal对象"<<endl;}void display(){cout<<"Its age is"<<age<<"years old"<<endl;}int age;};class Dog:public Animal{public:Dog(){cout<<"构造了Dog对象"<<endl;}~Dog(){cout<<"析构了Animal对象"<<endl;}int setAge(int n){age=n;return age;}};int main(){Dog mimi;mimi.setAge(3);mimi.display();return0;}运行结果:(2)定义一个基类BaseClass,有整型变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。
实验二类的继承与派生实验二类的继承与派生一、实验目的1. 掌握类的声明和使用。
2. 掌握对象的声明和使用。
3. 掌握具有不同访问属性的成员的访问方式。
4. 观察构造函数和析构函数的执行过程。
5. 学习声明和使用类的继承关系,声明派生类;6. 熟悉不同继承方式下对基类成员的访问控制;二.实验内容1. 设计一个用于人事管理的People(人员)类。
考虑到通用性,这里只抽象出所有类型人员都具有的属性:number(编号)、sex (性别)、birthday(出生日期)、id(身份证号)等等。
具有的属性如下:姓名char name[11]、编号char number[7]、性别char sex[3]、生日birthday、身份证号charid[20]。
其中“出生日期”声明为一个“日期”类内嵌子对象。
用成员函数实现对人员信息的录入和显示。
要求包括:构造函数和析构函数、拷贝构造函数、内联成员函数、组合。
在测试程序中声明people 类的对象数组,录入数据并显示。
2. 从people(人员)类派生出student(学生)类,添加属性:班号char classNO[7];从people 类派生出teacher(教师)类,添加属性:职务char pship[11]、部门char departt[21]。
从student 类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacher adviser;从graduate 类和teacher 类派生出TA(助教博士生)类,重载相应的成员函数,测试这些类。
三 . 实验步骤1.程序代码第一题#include#includeusing namespace std;class Date //日期类{private:int year;int month;int day;public:Date(){} //默认构造Date(int y,int m,int d) //带参构造{year=y;month=m;day=d;}void set() //设置数据函数{cin>>year>>month>>day;}void display() //显示函数{cout<<year<<"年"<<month<<"月"<<day<<"日";< bdsfid="103" p=""></year<<"年"<<month<<"月"<<day<<"日";<>}};class Person //人员类{private:string name;int num;char sex;Date birthday;char ID[18];public:Person(){} //默认构造Person(int n,int y,int m,int d,char id[18],char s='m'):birthday(y,m,d) {num=n;sex=s;strcpy(ID,id);} //有默认值的带参构造Person(Person& p) //拷贝构造{ name=;num=p.num;sex=p.sex;birthday=p.birthday;strcpy(ID,p.ID);}void input() //输入函数{cout<<"录入数据:"<<endl;< bdsfid="131" p=""></endl;<> cout<<"姓名:";cin>>name;cout<<"编号:";cin>>num;cout<<"性别(m/f):";cin>>sex;cout<<"生日:";birthday.set();cout<<"身份证号:";cin>>ID;ID[18]='\0';cout<<endl;< bdsfid="144" p=""></endl;<>}void output() //输出函数{cout<<"编号:"<<num<<endl;< bdsfid="149" p=""></num<<endl;<>cout<<"姓名:"<<name<<endl;< bdsfid="151" p=""></name<<endl;<>cout<<"性别:"<<sex<<endl;< bdsfid="153" p=""></sex<<endl;<>cout<<"生日:";birthday.display();cout<<endl;< bdsfid="157" p=""></endl;<>cout<<"身份证号:"<<id<<endl;< bdsfid="159" p=""></id<<endl;<>}~Person() //析构函数{cout<<" "<<num<<"号人员已经录入"<<=""></num<<"号人员已经录入"<};int main(){Person p1;p1.input();p1.output();return 0;}第二题#include#includeusing namespace std;class Date //日期类{private:int year;int month;int day;public:Date(){} //默认构造Date(int y,int m,int d) //带参构造{year=y;month=m;day=d;}void set() //设置数据函数{cin>>year>>month>>day;}void display() //显示函数{cout<<year<<"年"<<month<<"月"<<day<<"日";< bdsfid="200" p=""></year<<"年"<<month<<"月"<<day<<"日";<>}};class Person //人员类{private:string name;int num;char sex[10];Date birthday;char ID[18];public:Person(){} //默认构造Person(int n,int y,int m,int d,char id[18],char sex[10]):birthday(y,m,d) {num=n;strcpy(ID,id);} //有默认值的带参构造Person(Person& p) //拷贝构造{ name=;num=p.num;birthday=p.birthday;strcpy(ID,p.ID);}void input() //输入函数{cout<<"姓名:";cin>>name;cout<<"编号:";cin>>num;cout<<"性别(男/女):";cin>>sex;cout<<"生日:";birthday.set();cout<<"身份证号:";cin>>ID;ID[18]='\0';cout<<endl;< bdsfid="237" p=""></endl;<>}void output() //输出函数{cout<<"编号:"<<num<<endl;< bdsfid="242" p=""></num<<endl;<>cout<<"姓名:"<<name<<endl;< bdsfid="244" p=""></name<<endl;<>cout<<"性别:"<<sex<<endl;< bdsfid="246" p=""></sex<<endl;<>cout<<"生日:";birthday.display();cout<<endl;< bdsfid="250" p=""></endl;<>cout<<"身份证号:"<<id<<endl;< bdsfid="252" p=""></id<<endl;<>}~Person() //析构函数{//cout<<" "<<num<<"号人员已经录入"<<=""></num<<"号人员已经录入"<};class stduent:public Person{char classno[7];public: student(){cout<<"*************"<<="">void input(){Person::input();cout<<"输入学号"<<endl;< bdsfid="269" p=""></endl;<>cin>>classno;}void getno(){Person::output();cout<<"学号为:"<<classno<<endl;< bdsfid="275" p=""></classno<<endl;<>}};class teacher:public Person{char pship[11],departt[21];public :teacher(){cout<<"***********"<<endl;}< bdsfid="283" p=""></endl;}<> void input(){Person::input();cout<<"输入职务"<<endl;< bdsfid="288" p=""></endl;<>cin>>pship;cout<<"输入部门"<<endl;< bdsfid="291" p=""></endl;<>cin>>departt;}void inputt(){cout<<"输入职务"<<endl;< bdsfid="297" p=""></endl;<>cin>>pship;cout<<"输入部门"<<endl;< bdsfid="300" p=""></endl;<>cin>>departt;}void getno(){Person::output();cout<<"职务为:"<<pship<<endl;< bdsfid="306" p=""></pship<<endl;<>cout<<"部门为:"<<departt<<endl;< bdsfid="308" p=""></departt<<endl;<>}void output (){cout<<"职务为:"<<pship<<endl;< bdsfid="313" p=""></pship<<endl;<>cout<<"部门为:"<<departt<<endl;< bdsfid="315" p=""></departt<<endl;<>}};class graduate:public stduent{char subject[21], adviser[21];public :graduate(){cout<<""<<endl;< bdsfid="323" p=""></endl;<>}void input(){stduent::input();cout<<"输入专业:"<<endl;< bdsfid="329" p=""></endl;<> cin>>subject;cout<<"输入导师:"<<endl;< bdsfid="332" p=""></endl;<>cin>>adviser;}void getno(){ stduent::getno();cout<<"专业为:"<<subject<<endl;< bdsfid="338" p=""></subject<<endl;<>cout<<"导师为:"<<adviser<<endl;< bdsfid="340" p=""></adviser<<endl;<>}};class TA :public graduate,teacher{public :TA(){}void input(){graduate::input();teacher::inputt();}void getno(){graduate::getno();teacher::output();}};int main(){Person p1;stduent s;teacher t;graduate g;TA T;cout<<"请依次输入人员数据信息"<<endl;< bdsfid="366" p=""></endl;<>p1.input();cout<<"请输入学生数据信息";s.input();cout<<"请输入老师数据信息";t.input();cout<<"请输入研究生数据信息";g.input();cout<<"请输入助教博士数据信息";T.input();cout<<"人员数据信息为:";p1.output();cout<<"学生数据信息为:";s.getno();cout<<"老师信息为:";t.getno();cout<<"研究生信息为:";g.getno();cout<<"助教博士信息为:"T.getno();}2.调试程序第一次调试,发现没有名字的显示。
学号:姓名:班级:实验四继承与派生(二)【实验目的】1、理解多重继承的概念;2、理解为了避免同同一基类出现多个重复的副本而采用的虚基类概念和虚拟继承;3、学习利用虚基类解决二义性问题。
【实验内容】题目:2、设计一个用于人事管理的“people(人员)”基类。
考虑到通用性,仅只抽象出所有类型人员都有的属性:编号、姓名、性别、出生日期、身份证号等;从people(人员)类派生出student(学生)类,并添加属性:班号classNO;从people类派生出teacher(教师)类,并添加属性:职务principalship、部门Department;从student类派生出graduate (研究生)类,并添加属性:专业subject、导师teacher adviser(teacher 类);从graduate类和teacher类派生出TA(助教生)类。
设计时注意虚基类的使用,注意重载相应的成员函数。
测试这些类。
UML图:Date-year: int-month: int-day: int<<create>>-Date(y: int, m: int, d: int) <<create>>-Date(D: Date)+init(y: int, m: int, d: int): void+show(): void people#m_date: Date#m_no: long#m_ident_no: string#m_name: string#m_sex: string<<create>>-people(no: long, name: string, sex: string, ident_no: string, year: int, month: int, day: int) <<create>>-people(no: long, name: string, sex: string, ident_no: string, date: Date)<<create>>-people(p: people)+init(no: long, name: string, sex: string, ident_no: string, year: int, month: int, day: int): void+init(no: long, name: string, sex: string, ident_no: string, date: Date): void+init(p: people): void+show(): voidstudent#m_classno: string<<create>>-student(person: people, classno: string)<<create>>-student(stu: student)+show(): voidteacher#m_principalship: string#m_department: string<<create>>-teacher(p: people, principalship: string, department: string)<<create>>-teacher(stu: teacher)+show(): voidgraduate#m_subject: string#m_adviser: teacher<<create>>-graduate(s: student, subject: string, t: teacher)<<create>>-graduate(g: graduate)+show(): voidTA<<create>>-TA(g: graduate, t: teacher)<<create>>-TA(t: TA)+show(): void源程序代码:#include<iostream>#include<string>using namespace std;class Date{private:int year;int month;int day;public:Date(int y,int m,int d){year=y;month=m;day=d;}Date(const Date &D){year=D.year;month=D.month;day=D.day;}void init(int y,int m,int d){year=y;month=m;day=d;}void show()const{cout<<"出生日期:"<<year<<','<<month<<','<<day<<endl;}};class people{public:people(long no,string name,string sex,string ident_no,int year,int month,int day);people(long no,string name,string sex,string ident_no,Date date);people(const people &p);void init(long no,string name,string sex,string ident_no,int year,int month,int day);void init(long no,string name,string sex,string ident_no,Date date);void init(const people &p);virtual void show()const;protected:Date m_date;long m_no;string m_ident_no;string m_name;string m_sex;};people::people(long no,string name,string sex,string ident_no,int year,int month,int day):m_date(year,month,day){m_no=no;m_name=name;m_sex=sex;m_ident_no=ident_no;}people::people(long no,string name,string sex,string ident_no,Date date):m_date(date) {m_no=no;m_name=name;m_sex=sex;m_ident_no=ident_no;}people::people(const people &p):m_date(p.m_date){m_no=p.m_no;m_name=p.m_name;m_sex=p.m_sex;m_ident_no=p.m_ident_no;}void people::init(long no,string name,string sex,string ident_no,int year,int month,int day){m_date.init(year,month,day);m_no=no;m_name=name;m_sex=sex;m_ident_no=ident_no;}void people::init(long no,string name,string sex,string ident_no,Date date){m_date=date;m_no=no;m_name=name;m_sex=sex;m_ident_no=ident_no;}void people::init(const people &p){m_date=p.m_date;m_no=p.m_no;m_name=p.m_name;m_sex=p.m_sex;m_ident_no=p.m_ident_no;}void people::show()const{cout<<"编号:"<<m_no<<endl;cout<<"姓名:"<<m_name<<endl;cout<<"性别:"<<m_sex<<endl;cout<<"身份证号:"<<m_ident_no<<endl;m_date.show();}class student:public people{protected:string m_classno;public:student(people & person,string classno);student(const student &stu);void show()const;};student::student(people & person,string classno):people(person) {m_classno=classno;}student::student(const student &stu):people(stu){m_classno=stu.m_classno;}void student::show()const{people::show();cout<<"班号:"<<m_classno<<endl;}class teacher:public people{protected:string m_principalship;string m_department;public:teacher(people & p,string principalship,string department);teacher(const teacher &stu);void show()const;};teacher::teacher(people & p,string principalship,string department):people(p) {m_principalship=principalship;m_department=department;}teacher::teacher(const teacher &t):people(t){m_principalship=t.m_principalship;m_department=t.m_department;}void teacher::show()const{people::show();cout<<"部门:"<<m_department<<endl;cout<<"职务:"<<m_principalship<<endl;}class graduate:public student{protected:string m_subject;teacher m_adviser;public:graduate(student & s,string subject,teacher & t);graduate(const graduate & g);void show()const;};graduate::graduate(student & s,string subject,teacher & t):student(s),m_adviser(t) {m_subject=subject;}graduate::graduate(const graduate & g):student(g),m_adviser(g.m_adviser){m_subject=g.m_subject;}void graduate::show()const{student::show();cout<<"专业:"<<m_subject<<endl;cout<<"他的老师信息:"<<endl;m_adviser.show();}class TA:public graduate,public teacher{public:TA(graduate & g,teacher & t):graduate(g),teacher(t){}TA(const TA &t):graduate(t),teacher(t){}void show()const{graduate::show();cout<<"部门:"<<m_department<<endl;cout<<"职务:"<<m_principalship<<endl;}};void main(){Date date(1993,3,13);people person1(13137131,"彭伟华","男","42102319930104",date);people person2(13137132,"胡威","男","42012319931039",date);student student1(person1,"计科1103");teacher teacher2(person2,"教授","学工处");teacher teacher1(person1,"主任","教务处");graduate graduate1(student1,"计算机",teacher2);TA TA1(graduate1,teacher1);people *P[]={&person1,&student1,&teacher1,&graduate1};for(int i=0;i<4;i++){P[i]->show();cout<<"*****************************"<<endl;}TA1.show();}程序运行结果截图:【实验体会】1、在编写源程序代码之前,要画好UML图,通过UML图了解各个类之间的关系;2、要理解多重继承的概念;3、虚函数的声明只能出现在类定义中的函数原型声明中,不能在成员函数实现的的时候;4、可以通过指向基类的指针来访问派生类虚函数,实现程序运行过程中的动态绑定。