1273面向对象程序设计上机练习十一(运算符重载)

面向对象程序设计上机练习十一(运算符重载)

#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&);//一定要定义为友元函数,如果是重载双目操作符(即为类的成员函数),就只要设置一个参数作为右侧运算量,而左侧运算量就是对象本身。。。。。。

而 >>  或<< 左侧运算量是 cincout 而不是对象本身,所以不满足后面一点
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值