#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
int x,y,z;
double area;
public:
Triangle(int i,int j,int k)
{
double s;
x = i; y = j; z = k;
s = (x + y + z)/2.0;
area = sqrt(s * (s - x) * (s - y) * (s - z));
}
void disparea()
{
cout<<"Area="<<area<<endl;
}
friend double operator+(Triangle t1, Triangle t2)///重载为非成员函数会使用友元
{
return t1.area + t2.area;
}
};
int main()
{
Triangle t1(3,4,5),t2(4,5,6);
double s;
cout<<"t1:"; t1.disparea();
cout<<"t2:"; t2.disparea();
s=t1 + t2;
cout<<"总面积="<<s<<endl;
return 0;
}
========================================================================================================================
#include<iostream>
#include<cmath>
using namespace std;
class Triangle
{
int x,y,z;
double area;
public:
Triangle(int i,int j,int k)
{
double s;
x=i;y=j;z=k;
s=(x+y+z)/2.0;
area=sqrt(s*(s-x)*(s-y)*(s-z));
}
void disparea()
{
cout<<"Area="<<area<<endl;
}
friend double operator+(Triangle t1, Triangle t2)
{
return t1.area+t2.area;
}
friend double operator+(double d, Triangle t)///实现这个重载后就可以连加了
{
return d+t.area;
}
};
int main()
{
Triangle t1(3,4,5),t2(4,5,6),t3(5,6,7),t4(6,7,8);
double s;
cout<<"t1:"; t1.disparea();
cout<<"t2:"; t2.disparea();
cout<<"t3:"; t3.disparea();
cout<<"t4:"; t4.disparea();
s=t1+t2+t3+t4;
cout<<"总面积="<<s<<endl;
return 0;
}
================================================================================
class Point
{
int x,y;
public:
Point(){x=y=0;}
Point(int i,int j){x=i;y=j;}
Point(Point &);
~Point(){}
void offset(int,int); // 提供对点的偏移
void offset(Point); // 重载,偏移量用Point类对象表示
bool operator==(Point); // 运算符重载,判断两个对象是否相同
bool operator!=(Point); // 运算符重载,判断两个对象是否不相同
void operator+=(Point); // 运算符重载,将两个点对象相加
void operator-=(Point); // 运算符重载,将两个点对象相减
Point& operator+(Point); // 运算符重载,相加并将结果放在左操作数中
Point& operator-(Point); // 运算符重载,相减并将结果放在左操作数中
Point& operator=(Point);
Point& operator++();
Point operator++(int);
int getx(){return x;}
int gety(){return y;}
void disp()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};
07-25
07-25