📄 p9_1.cpp
字号:
/*******************************************
* p9_1.cpp *
* 重载+、-、++为类的友元函数,进行复数运算*
*******************************************/
#include<iostream>
// using namespace std;
using std::cout;
using std::endl;
class Complex //复数类定义
{
private:
double real; //复数实部
double image; //复数虚部
public:
Complex(double real=0.0,double image=0.0) //构造函数
{
this->real=real,this->image=image;
}
void display()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
friend Complex operator + (Complex A, Complex B) //重载+为友元函数
{ //重载运算符+的函数实现
return Complex(A.real+B.real, A.image+B.image);
}
friend Complex operator - (Complex A, Complex B); //重载-为友元函数
friend Complex operator - (Complex A); //重载-(取负)为友元函数
friend Complex operator ++ (Complex& A); //重载前置++为友元函数
friend Complex operator ++ (Complex& A,int); //重载后置++为友元函数
};
Complex operator -(Complex A,Complex B) //重载运算符-的函数实现
{
return Complex(A.real-B.real, B.image-B.image);
}
Complex operator -(Complex A) //重载运算符-(取负)的函数实现
{
return Complex(-A.real,-A.image);
}
Complex operator ++(Complex& A) //重载运算符前置++的函数实现
{
return Complex(++A.real,A.image);
}
Complex operator ++(Complex& A,int) //重载运算符后置++的函数实现
{
return Complex(A.real++,A.image);
}
void main()
{
Complex A(100.0,200.0),B(-10.0,20.0),C;
cout<<"A=", A.display();
cout<<"B=", B.display();
C=A+B; //使用重载运算符完成复数加法
cout<<"C=A+B=", C.display();
C=A-B; //使用重载运算符完成复数减法
cout<<"C=A-B=", C.display();
C=-A+B;
cout<<"C=-A+B=", C.display();
C=A++;
cout<<"C=A++, C=", C.display();
C=++A;
cout<<"C=++A, C=", C.display();
C=A+5;
C.display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -