C++运算符重载【加号、左移、递增、指针、赋值、中括号、关系、函数调用】,统统载了

学习目标

在c++对象的学习中,我们会使用到运算符重载,接下来大家一起学习一下吧!
在这里插入图片描述


学习内容

运算符重载:
operator overloading运算符重载是一种形式的C++多态
即对已有运算符进行重新定义,赋予新的功能,使其适配不同的类型。
形式:operator op(对象参数)

💖总结(建议仔细阅读!!!):

1、一元运算符:++ – a-- ++a-- 二元运算符:a+b
2、成员运算符函数: 一元是没有参数
二元只有一个参数:operator+(Time& t)
3、全局运算符函数: 一元有一个参数
二元有两个参数:operator+(Time& t1,Time& t2)
4、运算符重载的符号必须符合C++要求,不能自己虚构一个运算符,比如@
5、不能违反原来的运算规则
6、不能重载的运算符:

(1)sizeof
(2). 成员运算符 p.age
(3)* 成员指针运算符
(4):: 作用域运算符
(5)?: 条件运算符

7、只能在成员函数内重载的运算符

(1)= () [] -> 四个
(2)<< >> 只能通过全局函数和友元配合使用
(3)不要重载&&和||两个,因为无法实现短路的规则, flag&&(flag1||flag2)


学习代码

接下里代码演示一下如何去实现各个运算符的重载:

1、加号运算符重载:

#include<iostream>
using  namespace std;

class Time {
public:
	int m_hour;
	int m_min;
	Time() {
		m_hour = 0;
		m_min = 0;
	}
	Time(int hour, int min) {
		m_hour = hour;
		m_min = min;
	}
	void addtime(int hour, int min) {
		m_hour += hour+(min+m_min)/ 60;
		m_min = (min+m_min)% 60;
	}
	//调用成员方法
	Time count2(Time& t) {
		Time time;
		time.m_hour = this->m_hour + t.m_hour + (this->m_min + t.m_min) / 60;
		time.m_min= (this->m_min + t.m_min) % 60;
		return time;
	}
	//运算符重载
	/*Time operator+(Time& t) {
		Time time;
		time.m_hour = this->m_hour + t.m_hour + (this->m_min + t.m_min) / 60;
		time.m_min = (this->m_min + t.m_min) % 60;
		return time;
	}*/

	void showtime() {
		cout << "小时:" << m_hour << "分钟:" << m_min << endl;

	}


};
//全局函数运算符重载,只能与成员方法的运算符存在一个
Time operator+(Time& t1,Time& t2){

	Time time;
	time.m_hour = t1.m_hour + t2.m_hour + (t1.m_min + t2.m_min) / 60;
	time.m_min = (t1.m_min + t2.m_min) % 60;
	return time;
}


int main() {
	/*Time t1(1, 20);
	t1.showtime();
	t1.addtime(2, 50);
	t1.showtime();*/

	Time t1(1, 40);
	Time t2(2, 30);
	//调用成员方法相加
	Time t3 =t1.count2(t2);
	t3.showtime();
	//加号运算符重载
	Time t4 = t1 + t2;
	t4.showtime();


	return 0;
}

2、左移运算符重载:

#include <iostream>
using namespace std;

class Person {
	friend ostream& operator<<(ostream& out, Person& p);
private:
	int age;
	int height;
public:
	Person(int age, int height) {
		this->age = age;
		this->height = height;
	}
	void show() {
		cout << "年龄:" << age << " 身高:" << height << endl;
	}


};
//左移运算符重载:
//void operator<<(ostream& out, Person& p) {
//	cout << "年龄:" <<p.age << " 身高:" << p.height << endl;
//}
ostream& operator<<(ostream& out, Person& p) {
	cout << "年龄:" << p.age << " 身高:" << p.height << endl;
	return out;
}


int main() {
	Person p1(21, 183);
	p1.show();
	cout << p1;
	cout << p1 << endl;//operator(cout,p1) ..operator(void,cout),因此修改返回值即可


	return 0;
}

3、递增运算符重载

#include <iostream>
using namespace std;

class Num {
//	friend ostream& operator<<(ostream& out, Num& n);
	friend ostream& operator<<(ostream& out, Num n);
private:
	int num;
public:
	Num() {
		num = 0;
	}
	Num(int num) {
		this->num = num;
	}
	//前置++
	Num& operator++() {
		num++;
		return *this;
	}
	//后指++
	Num operator++(int) {//占位参数
		Num temp = *this;
		num++;
		return temp;
	}


};

//ostream& operator<<(ostream& out, Num& n) {
//	out << "数字num为:" << n.num ;
//	return out;
//}

ostream& operator<<(ostream& out, Num n) {
	out << "数字num为:" << n.num;
	return out;
}

int main() {
	Num n1(1);
	cout << n1 << endl;
	cout << ++n1 << endl;
	cout << n1++ << endl;
	cout << n1 << endl;

	return 0;
}

4、指针运算符重载

#include <iostream>
using namespace std;

class Person {
private:
	int age;
public:

	Person(int age) {
		this->age = age;
	}
	void show() {
		cout << "年龄为:" << this->age << endl;
	}
	~Person() {
		cout << "Person的析构函数。。。" << endl;
	}
};

//智能指针类
class smartpointer {
public:
	Person* operator->() {//指针重载
		return this->person;
	}
	Person& operator*() {//指针重载
		return *person;
	}
	
	smartpointer(Person *p) {
		person = p;
	}
	Person* person;
	~smartpointer() {
		cout << "smartpointer的析构函数。。。" << endl;
		if (person != nullptr) {
			delete person;
			person = nullptr;
		}
	}

};


int main() {
	Person* p1 = new Person(21);
	p1->show();
	(*p1).show();

	//delete p1;
	smartpointer s1(p1);
	s1->show();
	(*s1).show();

	return 0;
}

5、赋值运算符重载

#include <iostream>
#pragma warning(disable:4996)
using namespace std;

class Person {
public:
	char* name;

	Person& operator=(const Person& p) {//赋值运算符重载
		if (this->name!= nullptr) {
			delete[] this->name;
			this->name = nullptr;
		}
		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		return *this;
	}


	Person() {
		cout << "默认构造函数" << endl;
	}
	Person(const char* n) {
		name = new char[strlen(n) + 1];
		strcpy(name, n);
	}
	Person(Person& p) {
		name = new char[strlen(p.name) + 1];
		strcpy(name, p.name);
	}



	~Person() {
		cout << "Person的析构函数" << endl;
		if (name != nullptr) {
			delete[]name;
			name = nullptr;
		}
	}

};


int main() {

	Person p1("Tom");
	Person p2;
	p2 = p1;

	cout << "p1的名字:" << p1.name << endl;
	cout << "p2的名字:" << p2.name << endl;

	Person p3(p1);
	cout << "P3的名字:" << p3.name << endl;

	return 0;
}

6、中括号运算符重载

#include<iostream>
using namespace std;

class Array {
public:
	int m_num;
	int* arr;
	Array(int size) {
		m_num = 10;
		arr = new int[size];
		for (int i = 0; i < size; i++) {
			arr[i] = i;
		}
	}
	int getnum(int poisition) {
		return arr[poisition];
	}

	int& operator[](int position) {//中括号运算符重载
		return arr[position];
	}

};


int main() {
	Array arr1(5);
	for (int i = 0; i < 5; i++) {
		cout << arr1.arr[i] << endl;
	}
	cout << "==============" << endl;
	cout << arr1.getnum(1) << endl;
	cout << "==============" << endl;
	cout << arr1[2] << endl;

	return 0;
}

7、关系运算符重载

#include<iostream>
#include<string>

using  namespace std;

class Person {
public:
	string m_name;
	int m_age;
	Person(string  name, int age) {
		this->m_age = age;
		this->m_name = name;
	}
	bool operator==(const Person& p) {//==关系运算符重载
		return (this->m_name == p.m_name && this->m_age == p.m_age);
	}
	bool operator!=(const Person& p) {//!=关系运算符重载
		return !(this->m_name == p.m_name && this->m_age == p.m_age);
	}

};

int main() {
	Person p1("Tom", 18);
	Person p2("Lilua", 20);
	cout << p1.m_name << endl;
	cout << p2.m_name << endl;

	if (p1 == p2) {
		cout << "p1==p2" << endl;
	}
	else {
		cout << "p1!=p2" << endl;
	}

	if (p1 != p2) {
		cout << "p1!=p2" << endl;
	}
	else {
		cout << "p1==p2" << endl;
	}


	return 0;
}

8、函数调用重载

#include <iostream>
#include<string>
using namespace std;

class Print {
public:
	void operator()(string str) {//函数调用运算符重载
		cout << str << endl;
	}
	int operator()(int a, int b) {//函数调用运算符重载
		return a + b;
	}
};


int main() {

	Print p;
	p("nihao,my world");
	int sum = p(1, 2);//仿函数
	cout << sum << endl;

	return 0;
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不说二话的自家人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值