class Base
{
protected:
float square;
public:
Base(): square(0.0) {}
virtual void calSquare() = 0;
};
class Square: public Base
{
private:
float length;
public:
Square(float len): length(len)
{}
virtual void calSquare()
{
square = length * length;
cout << "正方形面积是 " << square << endl;
}
};
class Triangle: public Base
{
private:
float bottom, height;
public:
Triangle(float bot, float hei): bottom(bot), height(hei)
{}
virtual void calSquare()
{
square = bottom * height / 2;
cout << "三角形面积是 " << square << endl;
}
};
class Circle: public Base
{
private:
float radius;
public:
Circle(float r): radius(r)
{}
virtual void calSquare()
{
square = radius * radius * 3.1415926;
cout << "圆形面积是 " << square << endl;
}
};
void main()
{
Square s(4);
Triangle t(2, 3);
Circle c(2.5);
s.calSquare();
t.calSquare();
c.calSquare();
}
#include
using namespace std;
class graph
{
protected:
float high,wide;
public:
graph();
graph(float h,float w)
{
high=h;wide=w;cout<<"高为:"<
class retangle:public graph
{
public:
retangle(float h,float w):graph(h,w){}
void area()
{ cout<<"矩形的面积是:"<
class triangle:public graph
{
public:
triangle(float h,float w):graph(h,w){}
void area()
{ cout<<"等腰三角形的面积是:"<
void main()
{ retangle g(2,3);
g.area();
triangle h(2,3);
h.area();
}