C++编程求面积

编写一个C++程序,求三角形,矩形,圆形的面积,使用输入输出流类对象cin和cout实现数据的输入输出,使用常变量表示圆周率,使用函数实现面积的求解,求面积的函数使用内联函数,函数的重载,带默认参数的函数等几种不同的形式
求大神,速度啊

#include "stdafx.h"
#include <iostream>
using namespace std;

const double PI = 3.1415926;

class Shape
{
public:
virtual double GetArea() = 0;
};

// 三角形
class Triangle
: public Shape
{
public:

// 含参构造
Triangle(double a, double b, double c)
: _a(a),
_b(b),
_c(c)
{

};

// 默认构造
Triangle()
: _a(0.0),
_b(0.0),
_c(0.0)
{

}

// 析构
~Triangle(){};

// 获取面积
virtual double GetArea()
{
double p=(_a+_b+_c)/2;
return sqrt(p*(p-_a)*(p-_b)*(p-_c));
}

// 重载操作符 <<
friend ostream& operator << (ostream& out, Triangle& triangle)
{
out<<"The three sides of the triangle is:"<<endl
<<"a="<<triangle._a<<endl
<<"b="<<triangle._b<<endl
<<"c="<<triangle._c<<endl;

return out;
};

// 重载操作符 >>
friend istream& operator >> (istream& in, Triangle& triangle)
{

cout<<"please input the three sides of the triangle:"<<endl;

in>>triangle._a
>> triangle._b
>> triangle._c;

return in;
};

private:
// 私有变量,三条边
double _a;
double _b;
double _c;
};

// 长方形
class Rectangle
: public Shape
{
public:

// 含参构造
Rectangle(double a, double b)
: _a(a),
_b(b)
{

};

// 默认构造
Rectangle()
: _a(0.0),
_b(0.0)
{

}

// 析构
~Rectangle(){};

// 获取面积
virtual double GetArea()
{
return _a*_b;
}

// 重载操作符 <<
friend ostream& operator << (ostream& out, Rectangle& rect)
{
out<<"The length and width of the rectangle is:"<<endl
<<"a="<<rect._a<<endl
<<"b="<<rect._b<<endl;

return out;
};

// 重载操作符 >>
friend istream& operator >> (istream& in, Rectangle& rect)
{

cout<<"please input the length and width of the rectangle:"<<endl;

in>>rect._a
>> rect._b;

return in;
};


private:
// 私有变量,两条边
double _a;
double _b;
};

// 圆形
class Circle
: public Shape
{
public:

// 含参构造
Circle(double r)
: _r(r)
{

};

// 默认构造
Circle()
: _r(0.0)
{

}

// 析构
~Circle(){};

// 获取面积
virtual double GetArea()
{
return PI*_r*_r;
}

// 重载操作符 <<
friend ostream& operator << (ostream& out, Circle& circle)
{
out<<"The radius of the circle is:"<<endl
<<"r="<<circle._r<<endl;

return out;
};

// 重载操作符 >>
friend istream& operator >> (istream& in, Circle& circle)
{

cout<<"please input the radius of the circle:"<<endl;
in>>circle._r;

return in;
};


private:
// 私有变量,半径
double _r;
};

int main()
{
Shape* pShape = new Triangle();
  cin>>*(static_cast<Triangle*>(pShape));
  cout<<*(static_cast<Triangle*>(pShape));
cout<<"The area is:"<<pShape->GetArea()<<endl;
delete pShape;
pShape = NULL;

pShape = new Rectangle();
cin>>*(static_cast<Rectangle*>(pShape));
cout<<*(static_cast<Rectangle*>(pShape));
cout<<"The area is:"<<pShape->GetArea()<<endl;
delete pShape;
pShape = NULL;

pShape = new Circle();
cin>>*(static_cast<Circle*>(pShape));
cout<<*(static_cast<Circle*>(pShape));
cout<<"The area is:"<<pShape->GetArea()<<endl;
delete pShape;
pShape = NULL;

return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-10-04
这里面最复杂的是求圆的面积,给你写个,你参考着自己写三角形和矩形的,自己动手才有收获
#include<iostream>
using namespace std;
const float PI=3.1415;//常量
class Circle
{
public:
Circle(float r);//构造函数
float area();//求圆的面积
private:
float radius;

};
//构造函数初始化数据成员radius
Circle::Circle(float r)
{
radius=r;

}
//面积函数定义
float Circle:;area()
{
return PI*radius*radius;

}
int main()
{
float radius;

cout<<"请输入半径:";

cin>>radius;

Circle cir(radius);//定义对象

float S=cir.area();

cout<<S;

return 0;

}本回答被提问者和网友采纳
第2个回答  2014-02-25
自己做吧,有错误帮你看。
相似回答