设计一个三角形类Triangle,包含三角形三条边长的私有数据成员,要求使用重载运算符”+”,以实现求两个三角形对象的面积之和。
#include <iostream>
#include<cstring>
#include<math.h>
using namespace std;
class Triangle
{
private:
double a,b,c,s;
public:
Triangle();
Triangle(double a1,double b1,double c1);
friend double operator + (Triangle &s1,Triangle &s2);
};
Triangle::Triangle(double a1,double b1,double c1)
{
a=a1;
b=b1;
c=c1;
double p=(double)(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
}
double operator + (Triangle &s1,Triangle &s2)
{
return s1.s+s2.s;
}
int main()
{
Triangle s1(3,4,5),s2(3,4,5);
cout<<s1+s2;
return 0;
}