是这样么????
#include <iostream>
using namespace std;
//声明抽象基类Shape
class Shape
{
public:
virtual double area() const =0;
//纯虚函数
};
//声明Circle(圆形)类
class Circle:public Shape
{
public:
Circle(double r=0):radius(r){}
virtual double area() const{return 3.14159*radius*radius;}
protected:
double radius;
};
//声明Rectangle(矩形)类
class Rectangle:public Shape
{
public:
Rectangle(double a=0,double b=0):x(a),y(b){}
virtual double area() const{return x*y;}
protected:
double x;
double y;
};
//声明Square(正方形)类
class Square:public Shape
{
public:
Square(double a=0):x(a){}
virtual double area()const{return x*x;}
protected:
double x;
};
int main()
{
Shape *pt;
//定义基类指针
Circle circle(5.6); //建立Circle类对象circle
Rectangle rectangle(5,4);
Square square(6);
pt=&circle;
cout<<"The circle's area is "<<circle.area()<<endl;
pt=&rectangle;
cout<<"The rectangle's area is "<<rectangle.area()<<endl;
pt=□
cout<<"The square's area is "<<square.area()<<endl;
return 0;
}