#include<iostream>
#include<cstring>
using namespace std;
class Complex
{
public :
Complex(){x1=0;x2=0;x3=0;y1=0;y2=0;y3=0;}
Complex(int a,int b,int c,int d,int e,int f){x1=a;x2=b;x3=c;y1=d;y2=e;y3=f;}
Complex operator +(Complex &t);
friend ostream& operator <<(ostream&, Complex&);//一定要定义为友元函数,如果是重载双目操作符(即为类的成员函数),就只要设置一个参数作为右侧运算量,而左侧运算量就是对象本身。。。。。。
而 >> 或<< 左侧运算量是 cin或cout 而不是对象本身,所以不满足后面一点
void setting();
private:
int x1,x2,x3;
int y1,y2,y3;
};
Complex Complex:: operator+( Complex &t)
{
return Complex(x1+t.x1,x2+t.x2,x3+t.x3,y1+t.y1,y2+t.y2,y3+t.y3);
}
ostream& operator <<(ostream &output, Complex &c)
{
output<<c.x1<<" "<<c.x2<<" "<<c.x3<<endl;
output<<c.y1<<" "<<c.y2<<" "<<c.y3<<endl;
}
void Complex::setting()
{
cin>>x1>>x2>>x3>>y1>>y2>>y3;
}
int main()
{
int e;
Complex t1,t2,t3;
t1.setting();
t2.setting();
t3=t1+t2;
cout<<t3;
return 0;
}
1273面向对象程序设计上机练习十一(运算符重载)
最新推荐文章于 2021-03-11 20:27:21 发布