构造子类,实现虚函数就可以了啊
class Shape//抽象类
{
public:
Shape(){}
~Shape(){}
virtual float GetPerim()=0;
};
class Rectangle : public Shape
{
public:
Rectangle() {}
Rectangle(float l,float w) : L(l),W(w) {}
~Rectangle() {}
float GetPerim() {return 2*(L+W);}
private:
float L,W;
};
class Circle : public Shape
{
public:
Circle() {}
Circle(float r) : R(r) {}
~Circle() {}
float GetPerim() {return 2*3.1415926*R;}
private:
float R;
};
void main( )
{
Shape * sp;
sp=new Circle(4);
cout<<sp->GetPerim()<<endl;
sp = new Rectangle(5,3);
cout<<sp->GetPerim()<<endl;
return 0;
}