#include<iostream>
using namespace std;
class Complex
{
private:
int real;//记录实部;
int imag;//记录虚部;
public:
Complex(){}//构造函数并对其赋初值为0;
Complex(int a,int b);//重载构造两int型函数;
friend Complex operator*(Complex &,Complex &);//友元函数重载*运算符;
Complex operator/(Complex &);//成员函数重载/运算符;
void display();//输出格式;
};
Complex::Complex(int a,int b)//构造函数定义;
{
real=a;
imag=b;
}
Complex operator*(Complex &a,Complex &b)//友元重载*函数的定义,友元函数的定义不用加Complex::形式;
{
Complex c;
c.real=a.real*b.real;
c.imag=a.imag*b.imag;
return c;
}
Complex Complex::operator/(Complex &a)//成员重载/运算符函数 定义
{
Complex d;
d.real=this->real/a.real;
d.imag=this->real/a.imag;
return d;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<")"<<endl;
}
int main()
{
Complex a(1,2);
Complex b(4,8);
Complex c;
Complex d;
c=a*b;
d=a/b;
cout<<"c=";
c.display();
cout<<"d=";
d.display();
return 0;
}
用友元函数重载乘法,用成员函数重载除法
猜你喜欢
转载自blog.csdn.net/qq_41404557/article/details/85149086
今日推荐
周排行