欢迎来到天天文库
浏览记录
ID:54765244
大小:79.50 KB
页数:8页
时间:2020-04-21
《c++继承与派生实验报告.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验4继承与派生班级学号(最后两位)姓名成绩一、实验目的1.熟练掌握类的继承,能够定义和使用类的继承关系2.掌握派生类的声明与实现方法3.掌握类构造函数的初始化列表与作用域分辨率的使用方法4.理解虚基类在解决二义性问题中的作用.二、实验内容1.定义一个基类有姓名、性别、年龄,再由基类派生出教师类和学生类,教师类增加工号、职称和工资,学生类增加学号、班级、专业和入学成绩。2.声明一个哺乳动物Mammal类,再由此派生出狗Dog类,声明一个Dog类的对象,观察基类与派生类的构造函数与析构函数的调用顺序。3.定义一个Point类,派生出矩形类Rectangle和圆类Circle
2、,计算各派生类对象的面积Area()。4.设计一个圆类circle和一个桌子类table,另设计一个圆桌类roundtable,它是从前两个类派生的,要求输出一个圆桌的高度、面积和颜色等数据。5.定义一个大学生类student,函数私有数据成员:姓名、学号、校名,并为它定义带参数的构造函数,参数带缺省值的构造函数和输出数据成员值的print()公有成员函数,另定义研究生类,它以公有继承方式派生于类student,新增加“研究方向、导师名”两个私有数据成员,并定义带参数的构造函数和输出研究生数据的print()公有成员函数。在main()函数中定义基类和派生类对象,对类进行
3、测试。三、实验源程序、测试与结论1.#include#includeclassperson{public:person(char*a,char*b,ints){name=a;sex=b;score=s;}voiddisplay(){cout<<"姓名:"<4、(char*a,char*b,ints,char*c,intc2,floats1):person(a,b,s){pro=c;cla=c2;score=s1;}voiddisplay1(){cout<<"学生:"<5、,char*b,ints,char*p,intn,intsa):person(a,b,s){post=p;num=n;salary=sa;}voiddisplay2(){cout<<"教师:"<6、lay2();students1("小李","男",19,"学生",04,85);s1.display1();}}2.#include"iostream"usingnamespacestd;classMammal{public:Mammal(){cout<<"setMammalclass"<7、voidmain(){Dogb;}3.#includeusingnamespacestd;#definePI3.14classPoint{public:Point():m_x(0),m_y(0){}Point(doublex,doubley):m_x(x),m_y(y){}~Point(){}protected:doublem_x;doublem_y;};classRectangle:publicPoint{public:Rectangle():Point(){}Rectangle(doublex,doub
4、(char*a,char*b,ints,char*c,intc2,floats1):person(a,b,s){pro=c;cla=c2;score=s1;}voiddisplay1(){cout<<"学生:"<5、,char*b,ints,char*p,intn,intsa):person(a,b,s){post=p;num=n;salary=sa;}voiddisplay2(){cout<<"教师:"<6、lay2();students1("小李","男",19,"学生",04,85);s1.display1();}}2.#include"iostream"usingnamespacestd;classMammal{public:Mammal(){cout<<"setMammalclass"<7、voidmain(){Dogb;}3.#includeusingnamespacestd;#definePI3.14classPoint{public:Point():m_x(0),m_y(0){}Point(doublex,doubley):m_x(x),m_y(y){}~Point(){}protected:doublem_x;doublem_y;};classRectangle:publicPoint{public:Rectangle():Point(){}Rectangle(doublex,doub
5、,char*b,ints,char*p,intn,intsa):person(a,b,s){post=p;num=n;salary=sa;}voiddisplay2(){cout<<"教师:"<6、lay2();students1("小李","男",19,"学生",04,85);s1.display1();}}2.#include"iostream"usingnamespacestd;classMammal{public:Mammal(){cout<<"setMammalclass"<7、voidmain(){Dogb;}3.#includeusingnamespacestd;#definePI3.14classPoint{public:Point():m_x(0),m_y(0){}Point(doublex,doubley):m_x(x),m_y(y){}~Point(){}protected:doublem_x;doublem_y;};classRectangle:publicPoint{public:Rectangle():Point(){}Rectangle(doublex,doub
6、lay2();students1("小李","男",19,"学生",04,85);s1.display1();}}2.#include"iostream"usingnamespacestd;classMammal{public:Mammal(){cout<<"setMammalclass"<7、voidmain(){Dogb;}3.#includeusingnamespacestd;#definePI3.14classPoint{public:Point():m_x(0),m_y(0){}Point(doublex,doubley):m_x(x),m_y(y){}~Point(){}protected:doublem_x;doublem_y;};classRectangle:publicPoint{public:Rectangle():Point(){}Rectangle(doublex,doub
7、voidmain(){Dogb;}3.#includeusingnamespacestd;#definePI3.14classPoint{public:Point():m_x(0),m_y(0){}Point(doublex,doubley):m_x(x),m_y(y){}~Point(){}protected:doublem_x;doublem_y;};classRectangle:publicPoint{public:Rectangle():Point(){}Rectangle(doublex,doub
此文档下载收益归作者所有