C++操作符重载

例子一:重写二元操作符+(复数对象的+操作符与普通整型变量的+操作不一样,所以需要重写+)

实现代码一:通过友元函数来实现

//在vc 6.0中有可能不能通过,换个编译器就好了 
#include <iostream>
using namespace std;

class Complex //复数
{
	friend Complex &operator+(Complex &c1, Complex &c2);//利用友元函数,实现对私有成员变量的访问
public:
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void PrintComplex()
	{
		cout << this->a <<" + " << this->b << "i" << endl;
	}
private:
	int a;
	int b;
};

Complex &operator+(Complex &c1, Complex &c2)
{
	Complex tmp(c1.a + c2.a, c1.b + c2.b);
	return tmp;
}

int main()
{
	Complex c1(1, 2);
	Complex c2(2, 3);
	Complex c3 = c1 + c2; //通过返回一个引用,将引用直接转换成c3对象 
	c3.PrintComplex();
	return 0;
}

实现方法二:通过类成员函数

//在vc 6.0中有可能不能通过,换个编译器就好了 
#include <iostream>
using namespace std;

class Complex //复数
{
public:
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void PrintComplex()
	{
		cout << this->a <<" + " << this->b << "i" << endl;
	}
	Complex &operator+(Complex &c2)
	{
		Complex tmp(this->a + c2.a, this->b + c2.b);
		return tmp;
	}
private:
	int a;
	int b;
};

int main()
{
	Complex c1(1, 2);
	Complex c2(2, 3);
	Complex c3 = c1 + c2; //通过返回一个引用,将引用直接转换成c3对象 
	c3.PrintComplex();
	return 0;
}

例子二:重写一元操作符前置++

方法一:利用友元函数

//在vc 6.0中有可能不能通过,换个编译器就好了 
#include <iostream>
using namespace std;

class Complex //复数
{
public:
	friend void operator++(Complex &c1);
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void PrintComplex()
	{
		cout << this->a <<" + " << this->b << "i" << endl;
	}

private:
	int a;
	int b;
};
void operator++(Complex &c1)
{
	c1.a++;
	c1.b++;
}
int main()
{
	Complex c1(1, 2);
	Complex c2(2, 3);
	++c1;
	c1.PrintComplex();
	return 0;
}

方法二:利用类成员函数

//在vc 6.0中有可能不能通过,换个编译器就好了 
#include <iostream>
using namespace std;

class Complex //复数
{
public:
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void PrintComplex()
	{
		cout << this->a <<" + " << this->b << "i" << endl;
	}
	void operator++()
	{
		this->a++;
		this->b++;
	}
private:
	int a;
	int b;
};

int main()
{
	Complex c1(1, 2);
	Complex c2(2, 3);
	++c1;
	c1.PrintComplex();
	return 0;
}

例子三:重写一元操作符后置++

方法一:利用友元函数实现

//在vc 6.0中有可能不能通过,换个编译器就好了 
#include <iostream>
using namespace std;

class Complex //复数
{
public:
	friend Complex & operator++(Complex &c1, int);
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void PrintComplex()
	{
		cout << this->a <<" + " << this->b << "i" << endl;
	}
	
private:
	int a;
	int b;
};
Complex & operator++(Complex &c1, int)
{
	Complex tmp = c1;
	c1.a++;
	c1.b++;
	return tmp;
}
int main()
{
	Complex c1(1, 2);
	Complex c2(2, 3);
	c1++;
	c1.PrintComplex();
	return 0;
}

方法二:利用成员函数

//在vc 6.0中有可能不能通过,换个编译器就好了 
#include <iostream>
using namespace std;

class Complex //复数
{
public:
	Complex(int a, int b)
	{
		this->a = a;
		this->b = b;
	}
	void PrintComplex()
	{
		cout << this->a <<" + " << this->b << "i" << endl;
	}
	Complex & operator++(int)
	{
		Complex tmp = *this;
		this->a++;
		this->b++;
		return tmp;
	}
private:
	int a;
	int b;
};

int main()
{
	Complex c1(1, 2);
	Complex c2(2, 3);
	c1++;
	c1.PrintComplex();
	return 0;
}

 例子四:重写操作符<<(>>右移操作符类似左移操作符,istream &input)

#include <iostream>
using namespace std;

class Complex
{
	public:
		friend ostream & operator<<(ostream &out, Complex c);
		Complex(int a, int b)
		{
			this->a = a;
			this->b = b; 		
		}
	private:
		int a;
		int b;
};

ostream & operator<<(ostream &out, Complex c) //串行调用 
{
	cout << c.a << "+" << c.b << "i" << endl;
	return out;
}

int main()
{
	Complex c(1, 2);
	cout << c << "hello world";  //调用顺序 out.operator<<(c).operator<<("hello world") 
	return 0;
}

 C++中不能使用友元函数重载的运算符: = () [] ->

例子五:=的重载(深copy)

结论:1.先释放一个旧的内存; 2.返回一个引用; 3.=操作符,从左往右

 
#include <iostream>
using namespace std;
class Name
{
	private:
		char *name;
		int len;
	public:
		Name(const char *myp)
		{
			len = strlen(myp);
			name =(char *) malloc(len + 1); //
			strcpy(name, myp);
		}
		Name& operator=(Name &obj1)
		{
			//先释放旧的内存
			if (this->name != NULL)
			{
				delete[] name;
				len = 0;
			}
			//2 根据obj1分配内存大小
			this->len = obj1.len;
			this->name = new char [len+1];
			
			//把obj1赋值
			strcpy(name, obj1.name);
			return *this;
		}
		~Name()
		{
			if (name != NULL)
			{
				free(name);
				name = NULL;
				len = 0;
			}
		}
};

void objDisplay()
{
	Name n1("abcdef");
	Name n2("abc");
	n2 = n1;
}
int main()
{
	objDisplay();
	return 0;
} 

  

转载于:https://www.cnblogs.com/qkqBeer/articles/10735232.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值