运算符重载的几个例子

1.二元运算符

二元运算符指的是一个运算符有左操作数和右操作数两个对象,比如常见的+、-、*、/等等

1)算术运算符:+、-、*、/、%

2)关系运算符:<=、>=、<、>、!=、==

3)位运算符:^、&、|

4)逻辑运算符:&&、||

以>=为例:

#include <iostream>
using namespace std;

class Cperson
{
public:
	int _age;

	Cperson(int age):_age(age)
	{
	}

};

int operator >= (Cperson ob1, Cperson ob2)
{
	if (ob1._age >= ob2._age)
		return 1;
	else
		return 0;
}

int main()
{
	Cperson per1(14);
	Cperson per2(6);
	cout << (per1 >= per2) << endl;

	system("pause");
	return 0;
}

2.一元运算符

1)+、-、&、*(正、负、取地址、内存操作符)

2)~、!

以&为例:

#include <iostream>
using namespace std;

class Cperson
{
public:
	int _age;

	Cperson(int age):_age(age)
	{
	}

	int* operator & ()
	{
		return &this->_age;
	}
};


int main()
{
	Cperson per1(14);
	int *p = &per1;
	cout << p << endl;

	system("pause");
	return 0;
}

3.输入输入

<<、>>

3.1输出<<

格式:

ostream &operator << (ostream &os, const class &classname) {return os}

1)参数1是ostream引用,参数2是对象的常引用

2)返回值保证连续输出

3)必须在类外

4)类友元。因为一般成员数据都是私有的,外部定义的运算符无法访问私有成员,这时就需要将运算符重载在类中声明为友元函数

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson(int age):_age(age)
	{
	}
private:
	int _age;

	friend ostream &operator << (ostream &os, const Cperson &ob);
};

ostream &operator << (ostream &os, const Cperson &ob)
{
	os << ob._age;
	return os;
}

int main()
{
	Cperson per1(14);
	Cperson per2(18);
	cout << per1 << " " << per2 << endl;

	system("pause");
	return 0;
}

执行结果:

3.2输入>>

格式:

istream &operator >> (istream & is, class &classname) {return is}

输入的时候需要检测是否失败

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson():_age(0),_weight(0)
	{
	}
	void show()
	{
		cout << "age = " << _age << endl;
		cout << "weight = " << _weight << endl;
	}

private:
	int _age;
	double _weight;

	friend istream &operator >> (istream &io, Cperson &ob);
};

istream &operator >> (istream &is, Cperson &ob)
{
	is >> ob._age >> ob._weight;
	if (is.fail())
	{
		cout << "Input Error" << endl;
		abort();    //输入错误就以非正常方式结束程序
	} 
	return is;
}

int main()
{
	Cperson per1;
	cin >> per1;

	per1.show();

	system("pause");
	return 0;
}

执行结果:

4.赋值运算符

1)=,必须类内

2)复合赋值运算符:+=、-=、*=、/=、%=、<<=、>>=、^=、&=、|=,建议类内,内外也行

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson():_age(0),_weight(0)
	{
	}

	void operator = (int age)
	{
		this->_age = age;
	}
	void operator = (double weight)
	{
		this->_weight = weight;
	}

	void show()
	{
		cout << "age = " << _age << endl;
		cout << "weight = " << _weight << endl;
	}
private:
	int _age;
	double _weight;
};


int main()
{
	Cperson per1;
	per1 = 23;
	per1 = 68.12;
	per1.show();

	system("pause");
	return 0;
}

执行结果:

5.下标运算符——[ ]

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson():_a(0),_b(0),_c(0),_d(0)
	{
	}

	int &operator [] (int num)
	{
		switch (num)
		{
		case(0):
			return _a;
		case(1):
			return _b;
		case(2):
			return _c;
		case(3):
			return _d;
		default:
			cout << "Error" << endl;
			abort();
		}
	}

public:
	int _a;
	int _b;
	int _c;
	int _d;
};


int main()
{
	Cperson per1;
	for (int i = 0; i < 4; i++)
	{
		per1[i] = i * 2;
		cout << per1[i] << endl;
	}

	system("pause");
	return 0;
}

执行结果:

注意:这是对于类内只有一种数据类型的情况,如果类内又有int型又有double型等等,就要通过返回指针来运算了

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson():_a(0),_b(0),_c(0),_d(0)
	{
	}

	void *operator [] (int num)
	{
		switch (num)
		{
		case(0):
			return &_a;
		case(1):
			return &_b;
		case(2):
			return &_c;
		case(3):
			return &_d;
		default:
			cout << "Error" << endl;
			abort();
		}
	}

public:
	int _a;
	char _b;
	float _c;
	double _d;
};


int main()
{
	Cperson per1;
	*(int *)per1[0] = 5;
	*(char *)per1[1] = 'A';
	*(float *)per1[2] = 3.14f;
	*(double *)per1[3] = 8.12;

	cout << *(int *)per1[0] << endl;
	cout << *(char *)per1[1] << endl;
	cout << *(float *)per1[2] << endl;
	cout << *(double *)per1[3] << endl;

	system("pause");
	return 0;
}

执行结果:

6.递增递减运算符

1)可以写在类内也可以写在类外

2)左++和左--,函数只用传递一个参数,那就是一个对象;但是右++和右--需传递两个参数,一个是对象,另一个是int n,这个参数可以理解为一个标记,用来区分左++和左--

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson(int a):_a(a)
	{
	}
	int operator ++ ()
	{
		return ++_a;
	}
	int operator ++ (int n)
	{
		n = _a;
		++_a;
		return n;
	}

public:
	int _a;
};


int main()
{
	Cperson per1(15);
	cout << ++per1 << endl;
	cout << per1++ << endl;

	system("pause");
	return 0;
}

执行结果:

7.重载类型转换

1)运算符重载没有显式的返回类型,但是要写返回值

2)没有参数

3)必须定义成类成员函数

4)不应该改变对象的内容,所以应该是const函数

5)避免过度使用,一般情况下可以用对象.成员来访问成员

#include <iostream>
using namespace std;

class Cperson
{
public:

	Cperson(int a,double b):_a(a),_b(b)
	{
	}
	operator int()
	{
		return _a;
	}
	operator double()
	{
		return _b;
	}

public:
	int _a;
	double _b;
};


int main()
{
	Cperson per1(2,3.14);
	cout << (int)per1 << endl;
	cout << (double)per1 << endl;

	system("pause");
	return 0;
}

执行结果:

猜你喜欢

转载自blog.csdn.net/qq_33757398/article/details/81394326