用c++编写一个程序计算三角形正方形圆形三种图形的面积,要求 1.三种图形要有一个共同的基类Bas

用c++编写一个程序计算三角形正方形圆形三种图形的面积,要求
1.三种图形要有一个共同的基类Base
2.在其中声明一个虚函数用来求面积
3.利用派生类定义三角形正方形圆形
4.并用数据进行测试

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();
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-12-01
#include<iostream>
using namespace std;
class graph
{
protected:
float high,wide;
public:
graph();
graph(float h,float w)
{
high=h;wide=w;cout<<"高为:"<<h<<"\t宽为:"<<w<<endl;} };

class retangle:public graph
{
public:
retangle(float h,float w):graph(h,w){}
void area()
{ cout<<"矩形的面积是:"<<high*wide<<endl;}
};

class triangle:public graph
{
public:
triangle(float h,float w):graph(h,w){}
void area()
{ cout<<"等腰三角形的面积是:"<<high*wide/2<<endl;}
};

void main()
{ retangle g(2,3);
g.area();
triangle h(2,3);
h.area();
}