c++学习14 类与对象(三)运算符重载

类与对象

运算符重载

概念

对已有的运算符重新定义,赋予其另一种功能,以适应不同的数据类型
运算符重载还是可以发生函数重载的
编译器实现的运算符只能执行常规设定好的计算方式,而运算符重载可以自定义数据类型的运算

加号运算符重载

作用:
实现两个自定义数据类型相加的运算
本质:
重载 **operator+()**方法
实现:

  1. 成员函数重载+号
  2. 全局函数重载+号
#include<iostream>
using namespace std;

//加号运算符重载

//1.成员函数做加号运算符重载
class P {
public: 
	int m_A;
	int m_B;
	P(){};
	P(int a,int b);
	/*P operator+(P& p) {
		P temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}*/
};

P::P(int a,int b) {
	this->m_A = a;
	this->m_B = b;
}

//2.全局函数做加号运算符重载
P operator+(P& p1, P& p2) {
	P temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}
void test01() {
	P p1(4, 8);
	P p2(6, 2);
	P p3 = p1 + p2; //可以直接用operator+也可以直接用+号
	cout << p3.m_A << endl;
	cout << p3.m_B<< endl;
}


int main() {
	test01();
	system("pause");
	return 0;
}

左移运算符重载

不会利用成员函数重载<<(左移运算符),因为无法实现cout在左侧
配合着友元可以让<<访问对应类的私有成员,而关于私有属性的赋值则可以通过一个公有的构造方法去实现

#include<iostream>
using namespace std;

//左移运算符重载
class P {
	friend ostream& operator<< (ostream& cout, P& p);
private:
	int m_A;
	int m_B;
public:
	P(int a, int b) {
		m_A = a;
		m_B = b;
	}
};

//只能用全局函数来重载左移运算符
//由于cout全局只能有一个,所以要采用引用的方式去拿到
ostream& operator<< (ostream & cout, P & p) {

	cout << "p.m_A的值为:" << p.m_A << "p.m_B的值为: " << p.m_B;
	return cout;//返回一个引用是为了能够以链式结构继续调用下去

}
int main() {
	P p(10,20);	
	cout << p <<" 你好呀" << endl;
	system("pause");
	return 0;
}

递增运算符重载

通过重载递增运算符,实现自己的整型数据

前置++和后置++

前置++则是先加,再结果

//重载前置++运算符
	MyInterger& operator++() {
		m_Int++;
		return *this;
	}

后置++,先用一个int占位参数来区分其为后置++,后先结果再++,则需要用一个临时状态存储最初的结果,再对值进行++操作
注意后置递增应该返回的是值,而不是引用,其内部将成员属性做了++,而要展示的只是一个返回值,而局部对象引用在程序结束后便会被释放,如果返回局部对象引用则是非法操作

MyInterger operator++(int) {
		MyInterger temp = *this;//先记录当前结果,再进行++操作
		m_Int++;
		return temp;
	}

完整代码(++和- -运算符重载

#include<iostream>
using namespace std;
//递增运算符重载
class MyInterger {
	friend ostream& operator<<(ostream& cout, MyInterger myInt);
public:
	MyInterger() {
		m_Int = 0;
   }

	//重载前置++运算符
	MyInterger& operator++() {
		m_Int++;
		return *this;
	}
	//重载后置++运算符
	//int表示占位参数,可以用来区分前置和后置运算符
	MyInterger operator++(int) {
		MyInterger temp = *this;//先记录当前结果,再进行++操作
		m_Int++;
		return temp;
	}
	//重载前置--运算符
	MyInterger& operator--() {
		m_Int--;
		return *this;
	}
	//重载后置--运算符
	//int表示占位参数,可以用来区分前置和后置运算符
	MyInterger operator--(int) {
		MyInterger temp = *this;//先记录当前结果,再进行--操作
		m_Int--;
		return temp;
	}
private:
	int m_Int;
};


//重载左移运算符
ostream& operator<<(ostream& cout, MyInterger myInt) {
	cout << myInt.m_Int;
	return cout;
}
void test01() {
	MyInterger myInt;
	cout << ++myInt << endl;
	cout << ++myInt << endl;
	cout << myInt << endl;
}

void test02() {
	MyInterger myInt;
	cout << myInt++ << endl;
	cout << myInt++ << endl;
	cout << myInt << endl;
}
void test03() {
	MyInterger myInt;
	cout << --myInt << endl;
	cout << --(--myInt) << endl;
	cout << myInt << endl;
}

void test04() {
	MyInterger myInt;
	cout << myInt-- << endl;
	cout << (myInt--)-- << endl;
	cout << myInt << endl;
}
int main() {
	cout << "前置++" << endl;
	test01();
	cout << "后置++" << endl;
	test02();
	cout << "前置--" << endl;
	test03();
	cout << "后置--" << endl;
	test04();
	system("pause");
	return 0;
}

赋值运算符重载

系统自动编译的赋值运算符,只是简单的浅拷贝,如果成员属性在堆区创建,则会触发重复释放堆区的问题,这时,我们就需要一个深拷贝去解决这个问题。
且要满足能够连=的操作,则需在重构赋值运算符时返回本身的引用,下面是全部代码:

#include<iostream>
using namespace std;
//赋值运算符重载

class Person {
public:
	Person(int age) {
		m_Age = new int(age);
	}
	

	~Person() {
		if (m_Age == NULL) {
			return;
		}
		else {
			delete m_Age;
			m_Age = NULL;
		}
	}

	Person& operator=(Person& p) {
		if (m_Age != NULL) {
			delete m_Age;
			m_Age = NULL;
		}
		//深拷贝
		m_Age = new int(*p.m_Age);
		return *this;
	}
	int* m_Age;
};

void test01() {
	Person p(18);
	Person p3(20);
	Person p2(30);
	p3 = p = p2;
	cout << "p的年龄为:" <<* p.m_Age << endl;
	cout << "p3的年龄为:" <<* p3.m_Age << endl;
}
int main() {
	test01();
	system("pause");
	return 0;
}

关系运算符重载

这个没啥好见的,直接上代码

#include <iostream>
using namespace std;

//关系运算符重载

class Person {
public:
	Person(string name,int age) {
		m_Name = name;
		m_Age = age;
	}
	string m_Name;
	int m_Age;

	bool operator==(Person &p) {
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
			return true;
		}
		else {
			return false;
		}
	}
	bool operator!=(Person& p) {
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
			return false;
		}
		else {
			return true;
		}
	}
};

void test01() {
	Person p1("张三", 18);
	Person p2("张三", 34);
	
	if (p1 != p2) {
		cout <<  "p1和p2是不相等的" << endl;
	}
	else {
		cout << "p1和p2是相等的" << endl;
	}
	
	
}
int main() {
	test01();
	system("pause");
	return 0;
}

函数调用运算符重载

  1. 函数调用运算符()也可以进行重载
  2. 由于重载后的使用方式非常像函数的调用,因此称为仿函数
  3. 仿函数没有固定写法,非常灵活
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值