实验二 类的继承与派生
- 格式:doc
- 大小:218.50 KB
- 文档页数:11
实验2 继承与派生2.1 实验目的1.熟练掌握类的继承,能够定义和使用类的继承关系。
2.掌握派生类的声明与实现方法。
3.掌握类构造函数的初始化列表与作用域分辨符的使用方法。
4.理解虚基类在解决二义性问题中的作用。
2.2 实验工具与准备工作在开始实验前,应回顾或复习相关内容。
需要一台主算机,其中安装有Visual C++ 6.0等集成开发环境软件。
2.3 实验内容1.先阅读下列程序,写出执行结果。
然后输入程序,调试程序,比较结果的正确性。
// 文件名: main.cpp#include <iostream> // 预处理命令using namespace std; // 使用标准命名空间stdclass A{public:// 公有函数:A(){ cout << "构造A" << endl; } // 构造函数~A(){ cout << "析构A" << endl; } // 析构函数};class B: public A{public:// 公有函数:B(){ cout << "构造B" << endl; } // 构造函数~B(){ cout << "析构B" << endl; } // 析构函数};class C: public B{public:// 公有函数:C(){ cout << "构造C" << endl; } // 构造函数~C(){ cout << "析构C" << endl; } // 析构函数};int main(void) // 主函数main(void){C obj; // 定义对象system("PAUSE"); // 调用库函数system( ),输出系统提示信息return 0; // 返回值0, 返回操作系统}2.先阅读下列程序,写出执行结果。
实验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.实验目的(1)掌握利用单继承和多继承的方式定义派生类的方法。
(2)深刻理解在各种继承方式下构造函数和析构函数的执行顺序。
(3)理解和掌握公有继承、私有继承和保护继承对基类成员的访问机制。
(4)理解虚基类的目的和作用。
2.实验内容(1)声明一个圆类作为基类,含成员数据半径R;有成员函数:构造函数实现对基类成员数据的初始化、计算圆面积的成员函数、输出的成员函数,要求输出圆半径R。
把圆类作为基类,通过公有继承,派生圆柱体类,派生类新增成员数据有高(H);新增成员函数有构造函数、计算圆柱体体积的函数、输出所有成员的函数。
main()完成派生类对象的定义和相关函数的测试。
(2)声明一个学生类,有成员函数:学号、姓名、性别、年龄,要求有如下成员函数:构造函数,输出所有成员的函数。
声明一个课程类,有成员数据:课程编号、课程名称、学时数,要求有如下成员函数:构造函数,输出所有成员的函数。
将学生类和课程类作为基类,通过公有继承,派生选课类,派生类新增成员数据有:成绩;新增成员函数有:构造函数,输出所有成员的函数。
main()完成派生类对象的定义和相关函数的测试。
(3)设计一个汽车类Vehicle,包含数据成员车轮和重量,由它派生出类Car 和类Truck,前者包含载客数,后者包含载重量。
编写程序实现。
3.实验要求(1)独立完成实验。
(2)实验前需先预先浏览实验指导书,编好源程序。
(3)实验过程中尽量独立思考,培养分析问题和解决问题的能力。
(4)实验完毕后书写实验报告,实验报告内容包括:实验内容、结果分析以及本次实验的心得体会。
要求将程序源代码和运行时的输入输出数据情况书写在实验内容部分,并对程序的输出结果进行分析,说明为什么能得出相应的结果(如果不能输出预期结果,需要分析说明其原因)。
实验二类的继承与派生实验二类的继承与派生一、实验目的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. 熟悉继承与派生的应用场景。
实验步骤:1. 创建父类:首先,我们创建一个名为"Animal"的父类,该类具有属性和方法,例如"age"和"eat()"。
2. 创建子类:接下来,我们创建一个名为"Cat"的子类,该类继承自"Animal"类。
在子类中,我们可以重写父类的方法或添加新的方法。
3. 实例化对象:通过实例化父类和子类的对象,我们可以调用它们的方法和访问它们的属性。
4. 测试继承与派生:我们可以通过调用父类和子类的方法,观察它们的行为是否符合预期。
实验结果:在创建父类"Animal"时,我们定义了一个"age"属性和一个"eat()"方法。
在创建子类"Cat"时,我们继承了父类的属性和方法,并添加了一个新的"meow()"方法。
在实例化父类对象时,我们可以通过调用"eat()"方法来模拟动物进食的行为。
而在实例化子类对象时,我们既可以调用从父类继承而来的"eat()"方法,也可以调用子类特有的"meow()"方法来模拟猫咪的叫声。
通过实验,我们发现继承与派生的优势在于代码的复用和扩展。
我们只需在父类中定义一次通用的属性和方法,然后让不同的子类继承父类,即可实现代码的复用。
同时,子类还可以通过重写父类的方法或添加新的方法,实现代码的扩展和个性化。
讨论与应用:继承与派生不仅仅局限于上述的父类和子类关系,它还可以在多层次的继承结构中发挥作用。
C++继承与派生实验报告应用数学学院信息与计算科学(信息计算)专业1 班、学号3111008106姓名吴伟栓教师评定_________________实验题目继承与派生一、实验目的与任务实验目的:(1)学习声明和使用类的继承关系,声明派生类。
(2)熟悉不同继承方式下对基类成员的访问控制。
(3)学习利用虚基类解决二义性问题。
实验任务:(1)声明一个基类Animal,有私有整型成员变量age,构造其派生类dog,在其成员函数SetAge(int n)中直接给age赋值,看看会有什么问题,把age改为公有成员变量,还会有问题吗?编程试试看。
(2)声明一个基类BaseClass,有整形成员变量Number,构造其派生类DerivedClass,观察构造函数和析构函数的执行情况。
(3)声明一个车(vehicle)基类,具有MaxSpeed,Weight等成员变量,Run,Stop 等成员函数,由此派生出自行车(bicycle)类,汽车(motorcar)类。
自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)类有座位数(SeatNum)等属性。
从bicycle 和motorcar派生出摩托车(motocycle)类,在继承过程中,注意把vehicle设置为虚基类。
如果不把vehicle设置为虚基类,会有什么问题?编程试试看。
(4)(选做)从实验六中的people(人员)类派生出student(学生)类,添加属性:班号char classNo[7];从people类派生出teacher(教师)类,添加属性:职务char principalship[11]、部门char department[21]。
从student类中派生出graduate(研究生)类,添加属性:专业char subject[21]、导师teacherradviser;从graduate类和teacher 类派生出TA(助教生)类,注意虚基类的使用。
C++与数据结构实验指导书实验二类及类的继承一、实验目的1、熟悉并掌握类的概念及定义类的格方法;2、掌握类对象的定义;3、掌握构造函数和析构函数的含义与作用以及定义方式和实现方法;4、理解继承的含义,掌握派生类的定义方法和实现;5、理解多继承的概念及多继承中构造与析构的应用。
二、实验内容1、定义一个学生类,数据成员有学号、姓名、年龄,并使用成员函数实现如下功能:(1)使用成员函数实现输入、输出;(2)使用构造函数和析构函数实现对数据的输入、输出;编写主函数使用这个类,实现对学生数据的赋值和输出。
#include<iostream>#include<string>using namespace std;class student{private:int number;string name;int age;public:student(int a,string b,int c){number=a;name=b;age=c;cout<<"学号:"<<number<<" 姓名:"<<name<<" 年龄:"<<age<<endl;}~student(){cout<<"学号:"<<number<<" 姓名:"<<name<<" 年龄:"<<age<<" Destructed."<<endl;}};int main(){student s1(1412010708,"雷宇",20);student s2(1412010709,"李嘉锡",18);student s3(1412010710,"刘奇瑞",19);return 0;}2、定义日期类Date。
实验二类的继承与派生一、实验目的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 <iostream>#include <string>using 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<<"日";}};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;cout<<"姓名:";cin>>name;cout<<"编号:";cin>>num;cout<<"性别(m/f):";cin>>sex;cout<<"生日:";birthday.set();cout<<"身份证号:";cin>>ID;ID[18]='\0';cout<<endl;}void output() //输出函数{cout<<"编号:"<<num<<endl;cout<<"姓名:"<<name<<endl;cout<<"性别:"<<sex<<endl;cout<<"生日:";birthday.display();cout<<endl;cout<<"身份证号:"<<ID<<endl;}~Person() //析构函数{cout<<" "<<num<<"号人员已经录入"<<endl; }};int main(){Person p1;p1.input();p1.output();return 0;}第二题#include <iostream>#include <string>using 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<<"日";}};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;}void output() //输出函数{cout<<"编号:"<<num<<endl;cout<<"姓名:"<<name<<endl;cout<<"性别:"<<sex<<endl;cout<<"生日:";birthday.display();cout<<endl;cout<<"身份证号:"<<ID<<endl;}~Person() //析构函数{//cout<<" "<<num<<"号人员已经录入"<<endl; }};class stduent:public Person{char classno[7];public: student(){cout<<"*************"<<endl; }void input(){Person::input();cout<<"输入学号"<<endl;cin>>classno;}void getno(){Person::output();cout<<"学号为:"<<classno<<endl;}};class teacher:public Person{char pship[11],departt[21];public :teacher(){cout<<"***********"<<endl;}void input(){Person::input();cout<<"输入职务"<<endl;cin>>pship;cout<<"输入部门"<<endl;cin>>departt;}void inputt(){cout<<"输入职务"<<endl;cin>>pship;cout<<"输入部门"<<endl;cin>>departt;}void getno(){Person::output();cout<<"职务为:"<<pship<<endl;cout<<"部门为:"<<departt<<endl;}void output (){cout<<"职务为:"<<pship<<endl;cout<<"部门为:"<<departt<<endl;}};class graduate:public stduent{char subject[21], adviser[21];public :graduate(){cout<<""<<endl;}void input(){stduent::input();cout<<"输入专业:"<<endl;cin>>subject;cout<<"输入导师:"<<endl;cin>>adviser;}void getno(){ stduent::getno();cout<<"专业为:"<<subject<<endl;cout<<"导师为:"<<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;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.调试程序第一次调试,发现没有名字的显示。