例172分析下列程序的运行结果 include“ iostream. h” class bo i public virtual void display()=0 class B1: public BO p ublic virtual void display) {cout<<”Bl: dislay()”<≤endl
例17.2分析下列程序的运行结果
class D1: public B1 void maino) i public {B0*p, virtual void display() B1b1; D1 d1 i cout<<"D1: dislay()"<<endl; y p=&b1 fun(p) void fun(B0 *ptr) p=&d1 i ptr->display(); 3 fun(p)
例173定义一个 shape抽象类,在此基础上派生出 rectangle类 和 circle类,二者都有 getarea(函数计算对象的面积, getperim()函数计算对象的周长 #include iostream h" class circle public shape class shape i public i public circle(float r)i radius=, 1 shape( 3 wcircleoix float getareao) wshape([3 I return 3. 14159*radius*radius, 1 virtual float getarea(0 float getperim( virtual float getperim(F0 i return 2*3. 14159*radi private float radius
例17.3 定义一个shape抽象类,在此基础上派生出rectangle类 和circle类,二者都有getarea( )函数计算对象的面积, getperim( )函数计算对象的周长
class rectangle public shape float length, width i public rectangle(float 1, float w) void maint i length= i shape *sp width=w sp=new circle() cout<<"The area of the circle is l rectangle()( y <<sp->getarea()<<endl float getareao) cout<<The perimeter of the circle is i return length*width, 3 <<sp->getperim()<<endl float getperim() delete sp i return 2*length+2*width private sp=new rectangle(4, 6) cout<< The area of the rectangle is <<sp->getarea()<<endl; cout<< The perimeter of the rectangle is <<sp->getperim()<<endl delete sp