一、实验目的和要求
1. 2. 3. 4.
了解多态的概念;
了解虚函数的作用及使用方法;
了解静态关联和动态关联的概念和用法; 了解纯虚函数和抽象类的概念和用法
二、实验内容和结果
1. 声明一个车(vehicle)基类,具有MaxSpeed、weight等成员变量有Run、Stop等成员
函数,由此派生出自行车(bicycle)类、汽车(motorcar)类,自行车(bicycle)类有高度(Height)等属性,汽车(motorcar)有座位数(SeatNum)等属性,类从bicycle和motorcar派生出摩托车(motorcycle)类,它们都有Run、Stop等成员函数,使用虚函数完成。(各个类都必须定义构造函数和析构函数)
#include protected: float MaxSpeed; float weight; public: ~vehicle(){} vehicle(float m,float w):MaxSpeed(m),weight(w){} virtualvoid Run () { cout<<\"车已经启动啦!!!\"< virtualvoid Stop () { cout<<\"vehicle已经停啦!!!\"< protected: float Height; public: ~bicycle(){} bicycle(float m,float w,float h):vehicle( m,w),Height(h){} void Run () { cout<<\"The height of bicycle is:\"< cout<<\"bicycle已经停啦!!!\"< class motorcar: virtualpublic vehicle { protected: int SeatNum; public: ~motorcar(){} motorcar(float m,float w,float s):vehicle( m,w),SeatNum(s){} void Run () { cout<<\"The number of motorcar's seat is:\"< cout<<\"motorcar已经停啦!!!\"< { public: ~motorcycle(){} motorcycle(float m,float w,float h,float s):vehicle( m,w),bicycle( m,w,h), motorcar(m,w,s){} void Run () { cout<<\"MaxSpeed:\"< cout<<\"motorcycle已经停啦!!!\"< vehicle a(108,53); bicycle b(180,50,2); motorcar c(180,80,5); motorcycle d(120,90,1.2,3); vehicle *p[4]={&a,&b,&c,&d }; for(int i=0;i<4;i++) { p[i]->Stop(); p[i]->Run(); } return 0; } 2. 定义抽象基类Shape,由它派生出3个派生类:Circle,Square,Rectangle,Triangle, Trapezoid(梯形)。用一个函数printArea分别输出以上五者的面积,5个图形的数据在定义对象时给出。 #include { public: ~Shape(){} virtualvoid Setdata(float=0 ,float=0 ,float=0) =0; virtualfloat Area() =0;//求面积、纯虚函数 }; class Triangle:public Shape //三角形类 { protected: float W,H; public: ~Triangle(){} Triangle(float w=0,float h=0):W(w),H(h){} void Setdata(float w=0,float h=0,float j=0) { W=w; H=h; } float Area() { return W*H/2; } }; class Square:public Shape //正方形类 { protected: float S; public: ~Square(){} Square(float s=0):S(s){} void Setdata(float s=0,float h=0,float { S=s; } float Area() { return S*S; } }; class Rectangle:public Shape //矩形类 { protected: float L; float W; =0) jpublic: ~Rectangle(){} void Setdata(float l=0,float w=0,float j=0) { L=l; W=w; } Rectangle(float l=0,float w=0):L(l),W(w){} float Area() { return L*W; } }; class Circle:public Shape //圆类 { protected: float R; public: ~Circle(){} Circle(float r=0):R(r){} void Setdata(float r=0,float h=0,float j=0) { R=r; } float Area() { return 3.1415926*R*R; } }; class Trapezoid:public Shape //梯形类 { protected: float SD; float XD; float H; public: ~Trapezoid(){} Trapezoid(float sd=0,float xd=0,floath=0):SD(sd),XD(xd),H(h){} void Setdata(float sd=0,float xd=0,float { SD=sd; XD=xd; H=h; =0) h } float Area() { return 0.5*(SD+XD)*H; } }; int main() { cout< cout<<\"总面积为:\"<3. 实现计算器程序,要求输入两个数和运算符,得到结果,用简单工厂模式完成。 #include protected: int a,b,result; char op; public: Operater(int A,char OP,int B):a(A),b(B),op(OP) {} Operater() { a=1,b=1,op=' '; } virtualint getResult()=0; }; class OperaterAdd:public Operater { public: OperaterAdd(int A,char OP,int B):Operater(A,OP,B) OperaterAdd():Operater() {} int getResult() {return result=a+b;} }; class OperaterSub:public Operater {public: OperaterSub(int A,char OP,int B):Operater(A,OP,B) {} OperaterSub():Operater() {} int getResult() { return result=a-b; } }; class OperaterMul:public Operater {} { public: OperaterMul(int A,char OP,int B):Operater(A,OP,B) {} OperaterMul():Operater() {} int getResult() {return result=a*b;}}; class OperaterDiv:public Operater { public: OperaterDiv(int A,char OP,int B):Operater(A,OP,B) {} OperaterDiv():Operater() {} int getResult() { if(b!=0) { result=a/b; return result; } else cout<<\"除数不能为0!\"; } }; class OperaterFactory { public: Operater *createOperate(char op) { Operater *oper; switch(op) {case'+': oper=new OperaterAdd();break; case'-': oper=new OperaterSub();break; case'*': oper=new OperaterMul();break; case'/': oper=new OperaterDiv();break; } return oper; } }; int main() { OperaterFactory OF; Operater *oper=OF.createOperate('+'); cout< 因篇幅问题不能全部显示,请点此查看更多更全内容