欢迎来到天天文库
浏览记录
ID:6136072
大小:44.50 KB
页数:4页
时间:2018-01-04
《实验六 多态和虚函数的应用》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、实验六多态与虚函数的应用班级:B12512学号:20124051202姓名:曹伟成绩:一.实验目的1.理解多态性的相关概念。2.理解静态关联和动态关联的相关概念。3.掌握虚函数的使用方法。4.掌握纯虚函数和抽象类的使用方法。二.使用的设备和仪器计算机+WindowsXP+VisualC++6.0三.实验内容及要求1、定义抽象类Shape,在此基础上派生出圆类Circle和正方形类Square,两个派生类都有计算面积的函数Area()、计算周长的函数Perim()和显示面积周长的函数Show()。程序中通过基类的指针调用派生类中的函数。(必做)2、设计一个Person类,包含n
2、ame和age两个数据成员;由Person类派生出学生类Student,添加学号Number;由Student类派生出研究生类Graduate,添加导师姓名instructor属性;每个类均有带参数构造函数、析构函数和显示信息的函数。程序中动态创建三类对象,通过基类的指针指向派生类对象,并调用派生类对象的函数。(选做)四.实验步骤1、程序代码:#definePI3.14159#includeusingnamespacestd;classShape{public:virtualvoidarea()=0;virtualvoidPerim()=0;};class
3、Circle:publicShape{public:Circle(doubler){radius=r;}voidarea(){cout<<"A1="<4、im(){cout<<"V2="<<2*(length+wide)<area();p->Perim();deletep;p=newSquare(3,4);p->area();p->Perim();deletep;return0;}运行结果:A1=28.2743V1=18.8495A2=12V2=14Pressanykeytocontinue1、程序代码:#include#include5、usingnamespacestd;classPerson{public:Person(inta=0,stringna=""){age=a;name=na;}virtualvoidinput(){cout<<"age:";cin>>age;cout<<"name:";cin>>name;}virtualvoiddisplay(){cout<<"age:"<6、licPerson{public:Student(inta=0,stringna="",intNum=0):Person(a,na){Number=Num;}voidinput(){Person::input();cout<<"Number:";cin>>Number;}voiddisplay(){Person::display();cout<<"Number:"<7、te(inta=0,stringna="",intNum=0,stringins=""):Student(a,na,Num){instructor=ins;}voidinput(){Student::input();cout<<"instructor:";cin>>instructor;}voiddisplay(){Student::display();cout<<"instructor:"<
4、im(){cout<<"V2="<<2*(length+wide)<area();p->Perim();deletep;p=newSquare(3,4);p->area();p->Perim();deletep;return0;}运行结果:A1=28.2743V1=18.8495A2=12V2=14Pressanykeytocontinue1、程序代码:#include#include
5、usingnamespacestd;classPerson{public:Person(inta=0,stringna=""){age=a;name=na;}virtualvoidinput(){cout<<"age:";cin>>age;cout<<"name:";cin>>name;}virtualvoiddisplay(){cout<<"age:"<6、licPerson{public:Student(inta=0,stringna="",intNum=0):Person(a,na){Number=Num;}voidinput(){Person::input();cout<<"Number:";cin>>Number;}voiddisplay(){Person::display();cout<<"Number:"<7、te(inta=0,stringna="",intNum=0,stringins=""):Student(a,na,Num){instructor=ins;}voidinput(){Student::input();cout<<"instructor:";cin>>instructor;}voiddisplay(){Student::display();cout<<"instructor:"<
6、licPerson{public:Student(inta=0,stringna="",intNum=0):Person(a,na){Number=Num;}voidinput(){Person::input();cout<<"Number:";cin>>Number;}voiddisplay(){Person::display();cout<<"Number:"<7、te(inta=0,stringna="",intNum=0,stringins=""):Student(a,na,Num){instructor=ins;}voidinput(){Student::input();cout<<"instructor:";cin>>instructor;}voiddisplay(){Student::display();cout<<"instructor:"<
7、te(inta=0,stringna="",intNum=0,stringins=""):Student(a,na,Num){instructor=ins;}voidinput(){Student::input();cout<<"instructor:";cin>>instructor;}voiddisplay(){Student::display();cout<<"instructor:"<
此文档下载收益归作者所有