欢迎来到天天文库
浏览记录
ID:52688849
大小:126.00 KB
页数:14页
时间:2020-03-29
《声明一个车(vehicle)基类-具有MaxSpeed、weight等成员变量有Run、Stop等成员函数.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验九多态性与虚函数(2)一、实验目的和要求1.了解多态的概念;2.了解虚函数的作用及使用方法;3.了解静态关联和动态关联的概念和用法;4.了解纯虚函数和抽象类的概念和用法二、实验内容和结果1.声明一个车(vehicle)基类,具有MaxSpeed、weight等成员变量有Run、Stop等成员函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)有座位数(SeatNum)等属性,类从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数,使用虚
2、函数完成。(各个类都必须定义构造函数和析构函数)#includeusingnamespacestd;classvehicle{protected:floatMaxSpeed;floatweight;public:~vehicle(){}vehicle(floatm,floatw):MaxSpeed(m),weight(w){}virtualvoidRun(){cout<<"车已经启动啦!!!"<3、hicle{protected:floatHeight;public:~bicycle(){}bicycle(floatm,floatw,floath):vehicle(m,w),Height(h){}voidRun(){cout<<"Theheightofbicycleis:"<4、atw,floats):vehicle(m,w),SeatNum(s){}voidRun(){cout<<"Thenumberofmotorcar'sseatis:"<5、{}voidRun(){cout<<"MaxSpeed:"<6、p[4]={&a,&b,&c,&d};for(inti=0;i<4;i++){p[i]->Stop();p[i]->Run();}return0;}1.定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。#include#includeusingnamespacestd;classShape{public:~Shape(){}virtualvoidSetdata(float=0,floa7、t=0,float=0)=0;virtualfloatArea()=0;//求面积、纯虚函数};classTriangle:publicShape//三角形类{protected:floatW,H;public:~Triangle(){}Triangle(floatw=0,floath=0):W(w),H(h){}voidSetdata(floatw=0,floath=0,floatj=0){W=
3、hicle{protected:floatHeight;public:~bicycle(){}bicycle(floatm,floatw,floath):vehicle(m,w),Height(h){}voidRun(){cout<<"Theheightofbicycleis:"<4、atw,floats):vehicle(m,w),SeatNum(s){}voidRun(){cout<<"Thenumberofmotorcar'sseatis:"<5、{}voidRun(){cout<<"MaxSpeed:"<6、p[4]={&a,&b,&c,&d};for(inti=0;i<4;i++){p[i]->Stop();p[i]->Run();}return0;}1.定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。#include#includeusingnamespacestd;classShape{public:~Shape(){}virtualvoidSetdata(float=0,floa7、t=0,float=0)=0;virtualfloatArea()=0;//求面积、纯虚函数};classTriangle:publicShape//三角形类{protected:floatW,H;public:~Triangle(){}Triangle(floatw=0,floath=0):W(w),H(h){}voidSetdata(floatw=0,floath=0,floatj=0){W=
4、atw,floats):vehicle(m,w),SeatNum(s){}voidRun(){cout<<"Thenumberofmotorcar'sseatis:"<5、{}voidRun(){cout<<"MaxSpeed:"<6、p[4]={&a,&b,&c,&d};for(inti=0;i<4;i++){p[i]->Stop();p[i]->Run();}return0;}1.定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。#include#includeusingnamespacestd;classShape{public:~Shape(){}virtualvoidSetdata(float=0,floa7、t=0,float=0)=0;virtualfloatArea()=0;//求面积、纯虚函数};classTriangle:publicShape//三角形类{protected:floatW,H;public:~Triangle(){}Triangle(floatw=0,floath=0):W(w),H(h){}voidSetdata(floatw=0,floath=0,floatj=0){W=
5、{}voidRun(){cout<<"MaxSpeed:"<6、p[4]={&a,&b,&c,&d};for(inti=0;i<4;i++){p[i]->Stop();p[i]->Run();}return0;}1.定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。#include#includeusingnamespacestd;classShape{public:~Shape(){}virtualvoidSetdata(float=0,floa7、t=0,float=0)=0;virtualfloatArea()=0;//求面积、纯虚函数};classTriangle:publicShape//三角形类{protected:floatW,H;public:~Triangle(){}Triangle(floatw=0,floath=0):W(w),H(h){}voidSetdata(floatw=0,floath=0,floatj=0){W=
6、p[4]={&a,&b,&c,&d};for(inti=0;i<4;i++){p[i]->Stop();p[i]->Run();}return0;}1.定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle,Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。#include#includeusingnamespacestd;classShape{public:~Shape(){}virtualvoidSetdata(float=0,floa
7、t=0,float=0)=0;virtualfloatArea()=0;//求面积、纯虚函数};classTriangle:publicShape//三角形类{protected:floatW,H;public:~Triangle(){}Triangle(floatw=0,floath=0):W(w),H(h){}voidSetdata(floatw=0,floath=0,floatj=0){W=
此文档下载收益归作者所有