1.在昨天作业的基础上,完成运算符重载
#include <iostream>
using namespace std;
//乘法运算符的重载
class Complex
{
int real;
int vir;
public:
Complex(){}
Complex(int a,int b):real(a),vir(b){}
void show()
{
cout<<real<<"+"<<vir<<"i"<<endl;
}
//成员函数版的*运算符重载
Complex operator*(Complex &a)
{
Complex temp;
temp.real=this->real*a.real;
temp.vir=this->vir*a.vir;
return temp;
}
//后自增++
Complex &operator++(int)
{
static Complex temp;
temp.real=this->real++;
temp.vir=this->vir++;
return temp;
}
// friend Complex operator*(Complex &c1,Complex &c2);
friend Complex &operator+=(Complex &c1,Complex &c2);
friend Complex &operator++(Complex &c1);
friend istream &operator>>(istream &in,Complex &a1);
friend ostream &operator<<(ostream &out,Complex &a2);
};
//全局函数重载+=运算符
Complex &operator+=(Complex &c1,Complex &c2)
{
c1.real=c1.real+c2.real;
c1.vir=c1.vir+c2.vir;
return c1;
}
//++(前自增)
Complex &operator++(Complex &c1)
{
c1.real=c1.real+1;
c1.vir=c1.vir+1;
return c1;
}
//输入>>
istream &operator>>(istream &in,Complex &a1)
{
in>>a1.real>>a1.vir;
return in;
}
//输出<<
ostream &operator<<(ostream &out,Complex &a2)
{
out<<a2.real<<"+"<<a2.vir<<"i"<<endl;
return out;
}
int main()
{
Complex c1(3,5);
Complex c2(4,6);
Complex c3;
//c3=c1.operator*(c2);
// c3=operator*(c1,c2);
// c3=c1*c2;
//c3.show();
//operator+=(c1,c2);
// ++c2;
// c2.show();
// c1=c2++;
// c2.show();
// cout<<"***********"<<endl;
// c1.show();
Complex a1,a2;
cin>>a1>>a2;
cout<<a1<<a2;
return 0;
}
2.思维导图