💻
C++
  • C++学习指南
  • 第一章 基础入门
    • 1 C++初识
      • 1.1 Visual Studio 下载及安装
      • 1.2 第一个C++程序
      • 1.3 注释
      • 1.4 变量
      • 1.5 常量
      • 1.6 关键字
      • 1.7 标识符命名规则
  • 2 数据类型
    • 2.1 整型
    • 2.2 sizeof关键字
    • 2.3 实型(浮点型)
    • 2.4 字符型
    • 2.5 转义字符
    • 2.6 字符串型
    • 2.7 布尔类型 bool
    • 2.8 数据的输入
  • 3 运算符
    • 3.1 算术运算符
    • 3.2 赋值运算符
    • 3.3 比较运算符
    • 3.4 逻辑运算符
  • 4 程序流程结构
    • 4.1 选择结构
    • 4.2 循环结构
    • 4.3 跳转语句
  • 5 数组
    • 5.1 概述
    • 5.2 一维数组
    • 5.3 二维数组
  • 6 函数
    • 6.1 概述
    • 6.2 函数的定义
    • 6.3 函数的调用
    • 6.4 值传递
    • 6.5 函数的常见样式
    • 6.6 函数的声明
    • 6.7 函数的分文件编写
  • 7 指针
    • 7.1 指针的基本概念
    • 7.2 指针变量的定义和使用
    • 7.3 指针所占内存空间
    • 7.4 空指针和野指针
    • 7.5 const修饰指针
    • 7.6 指针和数组
    • 7.7 指针和函数
    • 7.8 指针、数组、函数
  • 8 结构体
    • 8.1 结构体基本概念
    • 8.2 结构体定义和使用
    • 8.3 结构体数组
    • 8.4 结构体指针
    • 8.5 结构体嵌套结构体
    • 8.6 结构体做函数参数
    • 8.7 结构体中const使用场景
    • 8.8 结构体案例
  • 第二章 核心编程
    • 9 内存分区模型
      • 9.1 程序运行前
      • 9.2 程序运行后
      • 9.3 new操作符
    • 10 引用
      • 10.1 引用的基本使用
      • 10.2 引用的注意事项
      • 10.3 引用做函数参数
      • 10.4 引用做函数返回值
      • 10.5 引用的本质
      • 10.6 常量的引用
    • 11 函数提高
      • 11.1 函数默认参数
      • 11.2 函数占位参数
      • 11.3 函数重载
    • 12 类和对象
      • 12.1 封装
      • 12.2 对象的初始化和清理
      • 12.3 C++对象模型和this指针
      • 12.4 友元
      • 12.5 运算符重载
      • 12.6 继承
      • 12.7 多态
    • 13 文件操作
      • 13.1 文本文件
      • 13.2 二进制文件
  • 第三章 提高编程
    • 14 模板
      • 14.1 模板的概念
      • 14.2 函数模板
      • 14.3 类模板
    • 15 STL初识
      • 15.1 STL的诞生
      • 15.2 STL基本概念
      • 15.3 STL六大组件
      • 15.4 STL中容器、算法、迭代器
      • 15.5 容器算法迭代器初识
    • 16 STL常用容器
      • 16.1 string容器
      • 16.2 vector容器
      • 16.3 deque容器
      • 16.4 评委打分案例
      • 16.5 stack容器
      • 16.6 queue容器
      • 16.7 list容器
      • 16.8 set/multiset容器
      • 16.9 map/multimap容器
      • 16.10 员工分组案例
    • 17 STL函数对象
      • 17.1 函数对象
      • 17.2 谓词
      • 17.3 内建函数对象
    • 18 STL常用算法
      • 18.1 常用遍历算法
      • 18.2 常用查找算法
      • 18.3 常用排序算法
      • 18.4 常用拷贝和替换算法
      • 18.5 常用集合算法
      • 18.6 常用算法生成算法
由 GitBook 提供支持
在本页
  • 1.加号运算符重载
  • 2.左移运算符重载
  • 3.递增运算符重载
  • 5.关系运算符重载
  • 6.函数调用运算符重载

这有帮助吗?

  1. 第二章 核心编程
  2. 12 类和对象

12.5 运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

1.加号运算符重载

作用:实现两个自定义数据类型相加的运算

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


public:
	int m_A;
	int m_B;
};

//全局函数实现 + 号运算符重载
//Person operator+(const Person& p1, const Person& p2) {
//	Person temp(0, 0);
//	temp.m_A = p1.m_A + p2.m_A;
//	temp.m_B = p1.m_B + p2.m_B;
//	return temp;
//}

//运算符重载 可以发生函数重载 
Person operator+(const Person& p2, int val)  
{
	Person temp;
	temp.m_A = p2.m_A + val;
	temp.m_B = p2.m_B + val;
	return temp;
}

void test() {

	Person p1(10, 10);
	Person p2(20, 20);

	//成员函数方式
	Person p3 = p2 + p1;  //相当于 p2.operaor+(p1)
	cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;


	Person p4 = p3 + 10; //相当于 operator+(p3,10)
	cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;

}

int main() {

	test();

	system("pause");

	return 0;
}

总结1:对于内置的数据类型的表达式的的运算符是不可能改变的

总结2:不要滥用运算符重载

2.左移运算符重载

作用:可以输出自定义数据类型

class Person {
	friend ostream& operator<<(ostream& out, Person& p);

public:

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

	//成员函数 实现不了  p << cout 不是我们想要的效果
	//void operator<<(Person& p){
	//}

private:
	int m_A;
	int m_B;
};

//全局函数实现左移重载
//ostream对象只能有一个
ostream& operator<<(ostream& out, Person& p) {
	out << "a:" << p.m_A << " b:" << p.m_B;
	return out;
}

void test() {

	Person p1(10, 20);

	cout << p1 << "hello world" << endl; //链式编程
}

int main() {

	test();

	system("pause");

	return 0;
}

总结:重载左移运算符配合友元可以实现输出自定义数据类型

3.递增运算符重载

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

class MyInteger {

	friend ostream& operator<<(ostream& out, MyInteger myint);

public:
	MyInteger() {
		m_Num = 0;
	}
	//前置++
	MyInteger& operator++() {
		//先++
		m_Num++;
		//再返回
		return *this;
	}

	//后置++
	MyInteger operator++(int) {
		//先返回
		MyInteger temp = *this; //记录当前本身的值,然后让本身的值加1,但是返回的是以前的值,达到先返回后++;
		m_Num++;
		return temp;
	}

private:
	int m_Num;
};


ostream& operator<<(ostream& out, MyInteger myint) {
	out << myint.m_Num;
	return out;
}


//前置++ 先++ 再返回
void test01() {
	MyInteger myInt;
	cout << ++myInt << endl;
	cout << myInt << endl;
}

//后置++ 先返回 再++
void test02() {

	MyInteger myInt;
	cout << myInt++ << endl;
	cout << myInt << endl;
}

int main() {

	test01();
	//test02();

	system("pause");

	return 0;
}

总结: 前置递增返回引用,后置递增返回值

4.赋值运算符重载

c++编译器至少给一个类添加4个函数

  1. 默认构造函数(无参,函数体为空)

  2. 默认析构函数(无参,函数体为空)

  3. 默认拷贝构造函数,对属性进行值拷贝

  4. 赋值运算符 operator=, 对属性进行值拷贝

如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题

示例:

class Person
{
public:

	Person(int age)
	{
		//将年龄数据开辟到堆区
		m_Age = new int(age);
	}

	//重载赋值运算符 
	Person& operator=(Person &p)
	{
		if (m_Age != NULL)
		{
			delete m_Age;
			m_Age = NULL;
		}
		//编译器提供的代码是浅拷贝
		//m_Age = p.m_Age;

		//提供深拷贝 解决浅拷贝的问题
		m_Age = new int(*p.m_Age);

		//返回自身
		return *this;
	}


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

	//年龄的指针
	int *m_Age;

};


void test01()
{
	Person p1(18);

	Person p2(20);

	Person p3(30);

	p3 = p2 = p1; //赋值操作

	cout << "p1的年龄为:" << *p1.m_Age << endl;

	cout << "p2的年龄为:" << *p2.m_Age << endl;

	cout << "p3的年龄为:" << *p3.m_Age << endl;
}

int main() {

	test01();

	//int a = 10;
	//int b = 20;
	//int c = 30;

	//c = b = a;
	//cout << "a = " << a << endl;
	//cout << "b = " << b << endl;
	//cout << "c = " << c << endl;

	system("pause");

	return 0;
}

5.关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

示例:

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = 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;
		}
	}

	string m_Name;
	int m_Age;
};

void test01()
{
	//int a = 0;
	//int b = 0;

	Person a("孙悟空", 18);
	Person b("孙悟空", 18);

	if (a == b)
	{
		cout << "a和b相等" << endl;
	}
	else
	{
		cout << "a和b不相等" << endl;
	}

	if (a != b)
	{
		cout << "a和b不相等" << endl;
	}
	else
	{
		cout << "a和b相等" << endl;
	}
}


int main() {

	test01();

	system("pause");

	return 0;
}

6.函数调用运算符重载

  • 函数调用运算符 () 也可以重载

  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数

  • 仿函数没有固定写法,非常灵活

示例:

class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text << endl;
	}

};
void test01()
{
	//重载的()操作符 也称为仿函数
	MyPrint myFunc;
	myFunc("hello world");
}


class MyAdd
{
public:
	int operator()(int v1, int v2)
	{
		return v1 + v2;
	}
};

void test02()
{
	MyAdd add;
	int ret = add(10, 10);
	cout << "ret = " << ret << endl;

	//匿名对象调用  
	cout << "MyAdd()(100,100) = " << MyAdd()(100, 100) << endl;
}

int main() {

	test01();
	test02();

	system("pause");

	return 0;
}
上一页12.4 友元下一页12.6 继承

最后更新于4年前

这有帮助吗?