#include<iostream.h>
class shape
{
public:
virtual void printArea() =0;
};
class Rectangle:public shape //矩形
{
private:
double l,k,h;
public:
virtual void printArea()
{
cout<<"矩形面积="<<l*k*h<<endl;
}
Rectangle(double a =0,double b=0,double c=0)
{
l=a;
k=b;
h=c;
}
};
class Circle:public shape
{
private:
double r;
public:
virtual void printArea()
{
cout<<"圆面积="<<3.1415926*r*r<<endl;
}
Circle(double a=0)
{
r=a;
}
};
class Triangle:public shape
{
private:
double d,h;
public:
virtual void printArea()
{
cout<<"三角面积="<<d*h*0.5<<endl;
}
Triangle(double a=0,double b=0)
{
d=a;
h=b;
}
};
int main()
{
Circle c(5);
Triangle t(5,5);
Rectangle r(5,5,5);
shape *pt=&c;
pt->printArea();
pt=&t;
pt->printArea();
pt=&r;
pt->printArea();
return 0;
}
c++纯虚函数
最新推荐文章于 2020-09-09 16:19:02 发布