如下程序声明了一个二维图形类TwoDShape,从其派生出矩形类Rec。
#include<iostream>
#include<string>
using namespace std;
class TwoDShape //二维图形类
char name[20];
public:
TwoDSha TwoDShape pe(char*n="unknown")strepy(name,n);
char*getName( )return name;
______=0;
;
class Rec:public TwoDShape
double width,height;
public:
Rec(double w=0.0,doubleh=0.0):TwoDShape("rectangle")width=w;height=h;
double getWidth( )return width;
double getHeight( )return height;
double area( )return width*height;
;
int main( )
TwoDShape*shape;
shape=new Rec(2.1,3.0);
cout<<"object is"<<shape->getName( )<<"\n";
cout<<"Area is"<<shape->area( )<<"\n";
return 0;
请将程序补充完整,使程序在运行时输出:
object is triangle
Area is 6.3
参考答案:virtual double area( )
解析: 定义纯虚函数的一般格式为:
virtual函数类型函数名(参数表)=0在基类Two[)Shape声明,而在Rec中进行纯虚函数的定义。