运算符重载的分析及代码实现

这面是一些运算符重载的代码,其中的注释是对函数调用返回及运算符重载的解释,帮助理解


#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<windows.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

//运算符重载
class Complex
{
private:
	double _real;
	double _image;
public:
	Complex(double real = 0.0, double image = 0.0)
	{
		cout << "Complex()" << endl;
	        _image = image;
		_real = real;
	}
	Complex(Complex& com)    //用来对创建同类对象对其初始化时调用
	{
		cout << "Complex(Complex &com)" << endl;
		_image = com._image;
		_real = com._real;
	}
	~Complex()
	{
		cout << "~Complex()" << endl;
	}

//加法运算符重载
	Complex operator+(Complex& com)
	{
		Complex tmp;
		tmp._image = this->_image + com._image;
		tmp._real = this->_real + com._real;
		return tmp;    //这里是返回值,返回时还要调用用一次拷贝构造函数,tmp在函数调用完就销毁了,所以这里要调用拷贝构造函数创建一个相同                                //的对象来返回tmp的值,
	}  

//赋值运算符重载
	Complex& operator=(const Complex& com)   //返回的还是d1自己,所以要返回引用,否则的话会返回时会调用拷贝构造函数。
	{
		if (this != &com)
		{
			this->_image = com._image;
			this->_real = com._real;
		}
		return *this;  //这里是返回引用,就不用再调用拷贝构造函数来创建一个对象返回其值,引用是其对象的别名就可直接返回。
	}

//减法运算符重载
	Complex operator-(Complex& com)
	{
		Complex tmp;
		tmp._image = this->_image - com._image;
		tmp._real = this->_real - com._real;
		return tmp;
	}

// 运算符++和--有前置和后置两种形式,如果不区分前置和后置,则使用operator++( )或operator--( )即可;
//否则,要使用operator++( )或operator--( )来重载前置运算符
//使用operator++(int)或operator--(int)来重载后置运算符,
//调用时,后置运算符重载函数比前置运算符重载函数多了一个int类型的参数,这个参数只是为了区别前置和后置运算符,此外没有任何作用。
//所以在调用后置运算符重载函数时,int类型的实参可以取任意值。

//前置++ 运算符重载
	Complex& operator++()
	{
		++this->_image;
		++this->_real;
		return *this;
	}
//后置++ 运算符重载
	Complex operator++(int)
	{
		Complex tmp(*this);
		this->_image++;
		this->_real++;
		return tmp;
	}
//前置-- 运算符重载
	Complex& operator--()
	{
		--this->_image;
		--this->_real;
		return *this;
	}
//后置-- 运算符重载
	Complex operator--(int)
	{
		Complex tmp(*this);
		this->_image--;
		this->_real--;
		return tmp;
	}
//取地址操作符重载(这两个一般不用重新定义,这和默认提供的一样)
	Complex* operator&()
	{
		return this;
	}
//取const修饰的运算符重载
	const Complex* operator&() const
	{
		return this;
	}


	void print()
	{
		cout << _real << "+" << _real << "i" << endl;
	}

};


int main()
{ 
	Complex d1(1.0,1.0);
	Complex d2(2.0, 2.0);
	Complex d3;
	//d3 = d1 + d2;
	//d1.operator=(d2);
	//d1 = d2;
	//d1.print();
	//d3.print();
	//d1 = d1>>1;
	//d1.print();
	d3 = d2++;
	d2.print();
	d3.print();
	cout << &d1 << endl;
	return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
visual basic 2005 技术内部第六章第七节运算符重载代码。 operator部分: Module Module1 Sub Main() End Sub End Module Public Structure Fraction 'Read-Only fields Private num As Long Private den As Long 'Read-Only properties Public ReadOnly Property Numerator() As Long Get Return num End Get End Property Public ReadOnly Property Denominator() As Long Get Return den End Get End Property Sub New(ByVal numerator As Long, ByVal denominator As Long) 'Normalize the numerator and denominator If numerator = 0 Then numerator = 1 ElseIf denominator < 0 Then numerator = -numerator denominator = -denominator End If Dim div As Long = GCD(numerator, denominator) num = numerator \ div den = denominator \ div End Sub 'the greatest common divisor of two numbers (helper method) Private Function GCD(ByVal n1 As Long, ByVal n2 As Long) As Long n1 = Math.Abs(n1) n2 = Math.Abs(n2) Do 'ensure that n1>n2 If n1 < n2 Then Dim tmp As Long = n1 n1 = n2 n2 = tmp End If n1 = n1 Mod n2 Loop While n1 <> 0 End Function 'override ToString to provide a textual representation of the fraction Public Overrides Function ToString() As String If num = 0 OrElse den = 1 Then Return num.ToString Else Return String.Format("{0}/{1}", num, den) End If End Function Public Shared Operator +(ByVal f1 As Fraction, ByVal f2 As Fraction) As Fraction 'a/b+c/d=(a*d+b*c)/(b*d) Return New Fraction(f1.num * f2.den + f2.num * f1.den, f1.den * f2.den) End Operator Public Shared Operator -(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b-c/d=(a*d-b*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator *(ByVal f1 As Fraction, ByVal f2 As Fraction) 'a/b * c/d=(a*c)/(b*d) Return New Fraction(f1.num * f2.num, f1.den * f2.den) End Operator Public Shared Operator /(ByVal f1 As Fraction, ByVal f2 As Fraction) Return New Fraction(f1.num * f2.den, f1.den * f2.num) End Operator End Structure

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值