login
欢迎访问QkqBeer博客园!

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;
} 

  

posted @ 2019-04-19 12:45  BeerQkq  阅读(190)  评论(0编辑  收藏  举报