栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++程序设计基础-类和对象:运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++程序设计基础-类和对象:运算符重载

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

目录

一、加号运算符重载

(1)注意事项

(2)代码示例

二、左移运算符重载

(1)注意事项

(2)示例代码

三、递增运算符重载

(1)注意事项

(2)示例代码


一、加号运算符重载

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

(1)注意事项

①不能滥用运算符重载:例如将加法运算符定义为两个值的差

(2)代码示例
#include
using namespace std;

//加号运算符重载
//①成员函数重载记号
//②全局函数重载加号
class Person
{
public:
	//成员函数重载+号
	Person operator+(Person& p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}
	int m_A;
	int m_B;
};

//重载版本:Person+int
Person operator+(Person& p1, int num)
{
	Person temp;
	temp.m_A = p1.m_A + num;
	temp.m_B = p1.m_B + num;
	return temp;
}

void test01()
{
	Person p1;
	p1.m_A = 10;
	p1.m_B = 10;

	Person p2;
	p2.m_A = 10;
	p2.m_B = 10;

	//成员函数重载加号的本质调用是:Person p3=p1.operator+(p2);
	//全局函数重载加号的本质调用是:Person p3=operator+(p1,p2);
	Person p3 = p1 + p2;
	//运算符重载也可以发生函数重载
	Person p4 = p1 + 100;     //会报错,操作数类型为Person+int
	cout << "p3.m_A=" << p3.m_A << endl;
	cout << "p3.m_B=" << p3.m_B << endl;
	cout << "p4.m_A=" << p4.m_A << endl;
	cout << "p4.m_B=" << p4.m_B << endl;
}

//全局函数重载+号


int main()
{
	test01();

	system("pause");
	return 0;
}

二、左移运算符重载

为类重载“<<”和">>"可以直接输入输出类的对象

(1)注意事项

①在重载“>>”和“<<”时,只能重载为友元函数形式,或者说只能用全局函数而不能用成员函数去重载。例如在示例代码中,cout是输出流对象,其类型为ostream;p是Person类对象。如果将“<<”重载为成员函数形式,由于cout是第一个操作数,编译器只能将该语句理解为cout.operator<<(p),这种形式实质上是为ostream类型重载<<运算符,与我们的实际需求不符合。

②ostream对象只能有一个

(2)示例代码
#include
using namespace std;

//左移运算符重载
class Person
{
	friend void test01();
	friend ostream& operator<<(ostream& cout, Person& p);
public:
	//利用成员函数重载左移运算符
	//通常不会使用成员函数重载<<运算符
private:
	int m_A;
	int m_B;

};

//使用全局函数重载左移运算符
//原本是void operator<<(ostream &cout,Person &p),后改为ostream&
//是因为使用需要返回cout属性以便cout< 

三、递增运算符重载

对递增运算符重载可以实现对对象的前置与后置递增

(1)注意事项

①前置递增返回为引用,后置递增返回为值

②对对象递增或递减要对ostream类的左移运算符进行重载,具体参考上文

(2)示例代码
#include
#include
using namespace std;

//重载递增运算符
class MyInteger
{
friend ostream& operator<<(ostream& cout, MyInteger& myint);
public:
	MyInteger()
	{
		m_Num = 0;
	}
	//注意这里要返回引用,为了对一个数据进行递增操作
	//前置递增运算符++myint
	MyInteger& operator++()
	{
		++m_Num;
		return *this;
	}
	//注意这里要在形参列表加int,及占位参数,可以用于区分前置递增
	//后置递增运算符myint++
	MyInteger operator++(int)
	{
		//先记录当时结果
		MyInteger temp = *this;
		//后递增
		m_Num++;
		//最后将结果做返回
		return temp;
	}
private:
	int m_Num;
};

//重载左移运算符
ostream& operator<<(ostream& cout, MyInteger& myint)
{
	cout << myint.m_Num;
	return cout;
}
//输出前置递增运算符
void test01()
{
	MyInteger myint;
	cout << "++myint=" << ++myint << endl;
	cout << "myint=" << myint << endl;
}

//输出后置递增运算符
void test02()
{
	MyInteger myint;
	cout << "++myint=" << myint++ << endl;      //<<会报错,将重载左移运算符第二个参数的&去掉就好;或者也可以将后置递增重载函数中的temp开辟在堆中就可以同时实现了
	cout << "myint=" << myint << endl;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

四、赋值运算符重载

五、关系运算符重载

六、函数调用运算符重载

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938501.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号