c++运算符重载(2)

成员运算符重载

语法格式
(1)类的内部

 函数类型 operator 运算符 (形参表)
{
	···
}

(2)类中声明原型 类外定义

class X {
函数类型 operator 运算符(形参表);
};

函数类型 X::operator 运算符 (形参表)
{
	···
}

说明 *可以用 return this 返回 当前对象的值
双目运算符重载
对双目运算符而言,成员运算符重载函数的形参表中仅有一个参数,它作为运算符的右操作符。另一个操作数是隐藏的 是该类当前的对象 它通过this指针隐含地址传给函数的。

class X{
···
	int operater +(x a);
···
}

在复数类Complex中定义了两个私有的成员变量 real 和 img 分别表示复数的实部和虚部,然后定义了几个共有的成员函数:
两个构造方法,一个为不带参的默认构造函数,另一个为 带两个参数的构造函数
通过该构造函数实现对私有成员变量real和img的初始化,
然后是一个没有返回值对的disp()函数用来显示输出复数的值,
接下来就是对四则运算的重载函数和一个赋值重载运算符的重载,
用来计算该复数的共轭复数,以及一个>运算符的重载用来判断复数的大小。
最后是一个普通的成员函数,用来进行复数的取模运算

#include "math.h"
#include<iostream>
using namespace std;

class Complex{
private:
	double real;
	double img;
public:
	Complex();
	Complex(double r,double i);
	void disp();
	Complex operator + (const Complex &c);
    Complex operator - (const Complex &c);
    Complex operator * (const Complex &c);
    Complex operator / (const Complex &c);
	//使用赋值运算符的重载来计算共轭复数
	Complex& operator=(const Complex &c);
	//比较复数的大小
	bool operator>(const Complex &c);
	//取模运算
	double getMol();
};

Complex::Complex(){

}
Complex::Complex(double r,double i):real(r),img(i)//通过使用初始化列表来初始化参数
{

}
Complex& Complex::operator=(const Complex &c){
	this->real=c.real;
	this->img=c.img*(-1);//共轭复数
	return *this;
}
void Complex::disp(){
	if(img>0){
		cout<<real<<'+'<<img<<"*i"<<endl;
	}else if(img==0){
		cout<<real<<endl;
	}else if(img<0){
		cout<<real<<img<<"*i"<<endl;
	}
}
double Complex::getMol(){//取模运算
	return sqrt(real*real+img*img);
}
Complex Complex::operator+(const Complex &c){
	Complex a;
	a.real=real+c.real;
	a.img=img+c.img;
	cout<<"两复数相加为:";
	return a;
}
Complex Complex::operator -(const Complex &c){
	Complex a;
	a.real=real-c.real;
	a.img=img-c.img;
	cout<<"两复数相减为:";
	return a;
}
Complex Complex::operator *(const Complex &c){
	Complex a;
	a.real=real*c.real-img*c.img;
	a.img=real*c.img+img*c.real;
	cout<<"两复数相乘为:";
	return 	a;
}
Complex Complex::operator /(const Complex &c){

	Complex tmp;
	Complex cc;
	cc=c;
	Complex parent;
	parent.real=c.real*cc.real-cc.img*c.img;//相互共轭的两个复数的乘积运算
	
	tmp.real=(real*cc.real-img*cc.img)/parent.real;
	tmp.img=(real*cc.img+img*cc.real)/parent.real;

	cout<<"两复数相除为:";
	return tmp;
}
bool Complex::operator>(const Complex &c){
	return real>c.real && img>c.img;
}
int main(int argc, char* argv[])
{
	Complex c1(1,-2);
	Complex c2(2,3);
	c1.disp();
	c2.disp();

	Complex cAdd=c1.operator +(c2);
	cAdd.disp();

	Complex cSub=c1.operator -(c2);
	cSub.disp();

	Complex cMul=c1.operator *(c2);
	cMul.disp();

	Complex cChu=c1.operator /(c2);
	cChu.disp();

	double mol=c1.getMol();
	cout<<"该复数的模为:"<<mol<<endl;

	bool isBig=c1.operator >(c2);
	if(isBig==true){
		cout<<"第一个复数大于第二个复数"<<endl;
	}else{
		cout<<"第一个复数小于第二个复数"<<endl;
	}
	return 0;
}

输出

1-2* i
2+3* i
两复数相加为:3+1* i
两复数相减为:-1-5* i
两复数相乘为:8-1* i
两复数相除为:-0.307692-0.538462*i
该复数的模为:2.23607
第一个复数小于第二个复数
Press any key to continue

说明
用类的构造函数可以建立临时对象 (不命名) ,无名对象返回结果执行效率更高
普通写法可读性高

Complex Complex::operator+(const Complex &c){
	Complex a;
	a.real=real+c.real;
	a.img=img+c.img;
	cout<<"两复数相加为:";
	return a;
}
Complex Complex::operator+(const Complex &c){
	return  Complex(a.rear+b.rear,a.imag+b.imag);//无名对象返回结果
}

ps :
visual c++ 6.0 没有完全实现c++标准,#include 不支持友元函数运算符重载 需要加.h 就可以了。

成员运算符重载函数与友元运算符重载函数的比较

(1) 双目运算中 成员 ~的参数表有一个参数 友元含两个
单目运算中 成员没有参数 友元 有一个
(2)当整数与对象相加时最好用 友元

例如,
如果将一个复数与一个整数相加,用成员运算符重载函数"+"运算符:

Complex operator+(int a)
{
     return (real+a,imag);
}	

若com和com1是类Complex的对象,则以下语句是正确的

com = com+100; //正确,
//这条语句被C++编译系统解释为:
com = com.operator(100);

然而,以下语句就不能工作了:

com = 100+com;
这条语句被C++编译系统解释为:
com = 100.operator(com);

如果定义以下的两个友元运算符重载函数

friend Complex operator+(Complex com,int x) 
//运算符+的左侧是类的对象,右侧是整数 
{
      return Complex(com.real+x,com.imag); 
}

friend Complex operator+(int a,Complex com) 
//运算符+的左侧是整数,右侧是类的对象
{
      return Complex(a+com.real,com.imag); 
}

说明

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值