⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 p9_3.cpp

📁 相当丰富的C++源码
💻 CPP
字号:
/*******************************************
 *  p9_3.cpp                               *
 * 重载=为类的成员函数,进行复数赋值       *
 *******************************************/
#include<iostream>
using namespace std;
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;
	}
	Complex operator + (Complex B);	        //运算符+重载成员函数
	Complex operator = (Complex B);	        //运算符=重载成员函数
   };	                                     

Complex Complex::operator +(Complex B)	         //重载运算符+的函数实现
{
	return Complex(real+B.real, image+B.image);  //创建一个临时对象作为返回值
}
Complex Complex::operator =(Complex B)	         //重载运算符+的函数实现
{
	real=B.real,image=B.image;
	cout<<"operator = calling..."<<endl;
	return *this;  //return Complex(real,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;	                       //使用重载运算符完成复数减法
	cout<<"C=A=",    C.display();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -