如何编写程序用成员函数形式实现复数类加减法运算重载
答案:3 悬赏:70 手机版
解决时间 2021-01-26 00:35
- 提问者网友:椧運幽默
- 2021-01-25 21:33
1 如何编写程序用成员函数形式实现复数类加减法运算重载? 2 编写程序,含有三个名为add的重载函数,分别实现两整数相加、两实数相加和两个复数相加的功能。?
最佳答案
- 五星知识达人网友:想偏头吻你
- 2021-01-25 22:58
正解见下:
#include<iostream>
using namespace std;
class complex//复数类声明
{
private:
double real;
double image;
public:
complex(double r=0.0,double i=0.0)//构造函数
{
real=r;
image=i;
}
complex operator+(complex c2);//+重载为成员函数
complex operator-(complex c2);//-重载为成员函数
complex operator*(complex c2);//*重载为成员函数
friend complex operator/(complex,complex);///重载为成员函数
bool operator==(complex c2);//==重载为成员函数
void display();
};
complex complex::operator +(complex c2)//重载的实现
{
complex c;
c.real=c2.real+real;
c.image=c2.image+image;
return complex(c.real,c.image);
}
complex complex::operator -(complex c2)//重载的实现
{
complex c;
c.real=real-c2.real;
c.image=image-c2.image;
return complex(c.real,c.image);
}
complex complex::operator *(complex c2)//重载的实现
{
complex c;
c.real=c2.real*real;
c.image=c2.image*image;
return complex(c.real,c.image);
}
complex operator/(complex c1,complex c2)//重载的实现
{
return complex(c1.real/c2.real,c1.image/c2.image);
}
bool complex::operator ==(complex c2)//重载的实现
{
if((real==c2.real)||(image==c2.image))return true;
else return false;
}
void complex::display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
void main()
{
complex c1(5.0,4),c2(5.0,4),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;//使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.display();
c3=c1-c2;//使用重载运算符完成复数减法
cout<<"c3=c1-c2=";
c3.display();
c3=c1*c2;//使用重载运算符完成复数乘法
cout<<"c3=c1*c2=";
c3.display();
c3=c1/c2;//使用重载运算符完成复数除法
cout<<"c3=c1/c2=";
c3.display();
//使用重载运算符完成两个复数的比较
bool result=(c1==c2);
cout<<"(c1==c2)="<<result<<endl
#include<iostream>
using namespace std;
class complex//复数类声明
{
private:
double real;
double image;
public:
complex(double r=0.0,double i=0.0)//构造函数
{
real=r;
image=i;
}
complex operator+(complex c2);//+重载为成员函数
complex operator-(complex c2);//-重载为成员函数
complex operator*(complex c2);//*重载为成员函数
friend complex operator/(complex,complex);///重载为成员函数
bool operator==(complex c2);//==重载为成员函数
void display();
};
complex complex::operator +(complex c2)//重载的实现
{
complex c;
c.real=c2.real+real;
c.image=c2.image+image;
return complex(c.real,c.image);
}
complex complex::operator -(complex c2)//重载的实现
{
complex c;
c.real=real-c2.real;
c.image=image-c2.image;
return complex(c.real,c.image);
}
complex complex::operator *(complex c2)//重载的实现
{
complex c;
c.real=c2.real*real;
c.image=c2.image*image;
return complex(c.real,c.image);
}
complex operator/(complex c1,complex c2)//重载的实现
{
return complex(c1.real/c2.real,c1.image/c2.image);
}
bool complex::operator ==(complex c2)//重载的实现
{
if((real==c2.real)||(image==c2.image))return true;
else return false;
}
void complex::display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
void main()
{
complex c1(5.0,4),c2(5.0,4),c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3=c1+c2;//使用重载运算符完成复数加法
cout<<"c3=c1+c2=";
c3.display();
c3=c1-c2;//使用重载运算符完成复数减法
cout<<"c3=c1-c2=";
c3.display();
c3=c1*c2;//使用重载运算符完成复数乘法
cout<<"c3=c1*c2=";
c3.display();
c3=c1/c2;//使用重载运算符完成复数除法
cout<<"c3=c1/c2=";
c3.display();
//使用重载运算符完成两个复数的比较
bool result=(c1==c2);
cout<<"(c1==c2)="<<result<<endl
全部回答
- 1楼网友:深街酒徒
- 2021-01-25 23:45
#include<iostream>
using namespace std;
class vector{
double a;double b;double c;
public:
vector(double x=0,double y=0,double z=0)
{a=x;b=y;c=z;}
vector operator+(const vector &c1,const vector &c2)
{return vector(c1.a+c2.a,c1.b+c2.b,c1.c+c2.c);}
vector operator-(const vector &c1,const vector &c2)
{return vector(c1.a-c2.a,c1.b-c2.b,c1.c-c2.c);}
};
int main()
{
vector c5(1,2),c6(2,3),c7;
c7=c5+c6;
cout<<"c5+c6="<<c7<<endl;
c7=c5-c6;
cout<<"c5-c6="<<c7<<endl;
return 0;
}
- 2楼网友:蕴藏春秋
- 2021-01-25 23:08
编写程序,含有三个名为add的重载函数,分别实现两整数相加、两实数相加和两个复数相加的功能。?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯