用C++设计一个三角形类
此三角形类为Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。其中,在此类中设计一个友元函数operater...
此三角形类为Triangle,包含三角形三条边长的私有数据成员,另有一个重载运算符“+”,以实现求两个三角形对象的面积之和。其中,在此类中设计一个友元函数operater+(Triangle t1,Triangle t2),它重载运算符“+”,返回t1和t2两个三角形的面积之和
展开
2013-07-25
展开全部
写了个简单的类,请参考一下:#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<math.h>
class Triangle{
public :
double area;//面积
friend double operator+(Triangle & ,Triangle &);//用友元函数重载运算符+
Triangle(int a,int b,int c){
double p;
side1=a;
side2=b;
side1=c;
if( (a+b<=c) ||(a+c<=b)||(b+c<=a)){
printf("a,b,c can't buildup a triangle\n");
assert(0);
}
p=((double)(a+b+c))/2;
area=sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式
printf("area=%lf\n",area);
}
private:
int side1;
int side2;
int side3;
};double operator+(Triangle &t1, Triangle &t2){
return t1.area+ t2.area;
} void main(){
double areaAdd;
Triangle t1(3,4,5);
Triangle t2(3,4,5);
areaAdd=t1+t2;
printf("面积之和:%lf\n",areaAdd);
//getchar();//test
}
#include<stdlib.h>
#include<assert.h>
#include<math.h>
class Triangle{
public :
double area;//面积
friend double operator+(Triangle & ,Triangle &);//用友元函数重载运算符+
Triangle(int a,int b,int c){
double p;
side1=a;
side2=b;
side1=c;
if( (a+b<=c) ||(a+c<=b)||(b+c<=a)){
printf("a,b,c can't buildup a triangle\n");
assert(0);
}
p=((double)(a+b+c))/2;
area=sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式
printf("area=%lf\n",area);
}
private:
int side1;
int side2;
int side3;
};double operator+(Triangle &t1, Triangle &t2){
return t1.area+ t2.area;
} void main(){
double areaAdd;
Triangle t1(3,4,5);
Triangle t2(3,4,5);
areaAdd=t1+t2;
printf("面积之和:%lf\n",areaAdd);
//getchar();//test
}
2013-07-25
展开全部
文件1:tringle.h 代码:#ifndef _TRINGLE_H_
#define _TRINGLE_H_class CTringle
{
public:
//公共函数
CTringle();
CTringle(double da,double db,double dc);
double getLenth();
double getArea();
friend double operator+ (CTringle ca,CTringle cb);private:
double a;
double b;
double c;
double lenth;
double area;}; #endif 文件2. tringle.cpp 代码:#include <iostream>
#include "tringle.h"
#include <math.h>
#include <iostream>
using namespace std;
CTringle::CTringle()
{
a = b = c =0;
}
CTringle::CTringle(double da,double db,double dc)
{
if ( da+db<dc || db+dc<da || da+dc<db)
{
cout<<"不能构成三角形"<<endl;
return ;
}
a = da;
b = db;
c = dc;
}
double CTringle::getArea()
{
area = sqrt(lenth*(lenth-a)*(lenth-b)*(lenth-c));
return area;
}
double CTringle::getLenth()
{
lenth = a+b+c;
return lenth;
}double operator+ (CTringle ca,CTringle cb)
{
return ca.area+cb.area;
}
3.文件3 主函数:#include <iostream>
#include "tringle.h"
#include <string>
using namespace std;void main(int argc,char* argv[])
{
CTringle a(12,90,80);
CTringle b(20,43,52); cout<<a.getArea()<<endl;
cout<<b.getArea()<<endl;
cout<<a+b<<endl;
}
#define _TRINGLE_H_class CTringle
{
public:
//公共函数
CTringle();
CTringle(double da,double db,double dc);
double getLenth();
double getArea();
friend double operator+ (CTringle ca,CTringle cb);private:
double a;
double b;
double c;
double lenth;
double area;}; #endif 文件2. tringle.cpp 代码:#include <iostream>
#include "tringle.h"
#include <math.h>
#include <iostream>
using namespace std;
CTringle::CTringle()
{
a = b = c =0;
}
CTringle::CTringle(double da,double db,double dc)
{
if ( da+db<dc || db+dc<da || da+dc<db)
{
cout<<"不能构成三角形"<<endl;
return ;
}
a = da;
b = db;
c = dc;
}
double CTringle::getArea()
{
area = sqrt(lenth*(lenth-a)*(lenth-b)*(lenth-c));
return area;
}
double CTringle::getLenth()
{
lenth = a+b+c;
return lenth;
}double operator+ (CTringle ca,CTringle cb)
{
return ca.area+cb.area;
}
3.文件3 主函数:#include <iostream>
#include "tringle.h"
#include <string>
using namespace std;void main(int argc,char* argv[])
{
CTringle a(12,90,80);
CTringle b(20,43,52); cout<<a.getArea()<<endl;
cout<<b.getArea()<<endl;
cout<<a+b<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询