c++ 基础知识-类和对象-运算符重载1

本文详细介绍了C++中类和对象的运算符重载,包括加法、左移和递增运算符的重载。通过实例展示了如何使用友元函数实现自定义输出,并探讨了前置和后置递增运算符的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c++ 基础知识-类和对象-运算符重载

1.加法运算符重载

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

class Person
{
public:
	//构造函数
	Person();
	//析构函数
	~Person();
	//error C2673: “operator +”: 全局函数没有“this”指针
	//重载加法运算符1
	//Person operator+(const Person &p)
	//{
	//	Person temp;
	//	temp .m_A = this->m_A + p.m_A;
	//	return temp;
	//}

public:
	int m_A;
};

Person::Person()
{
	m_A = 10;
}

Person::~Person()
{
	cout<<" ~Person() 析构函数 "<<endl;
}

//重载加法运算符2
Person operator+(const Person &p1,const Person &p2)
{
	Person temp;
	temp .m_A = p1.m_A + p2.m_A;
	return temp;
}

void fun()
{
	Person p1;
	Person p2;
	//Person p3 = p1.operator+(p2);//第一种显式调用方式
	Person p4 = p2 + p2;//第一种隐式调用方式
	cout<<"p1 m_A : "<<p1.m_A<<endl;
	cout<<"p2 m_A : "<<p2.m_A<<endl;
	//cout<<"p3 m_A : "<<p3.m_A<<endl;
	cout<<"p4 m_A : "<<p4.m_A<<endl;
	//输出
	//~Person() 析构函数
	//	p1 m_A : 10
	//	p2 m_A : 10
	//	p3 m_A : 20
	//	~Person() 析构函数
	//	~Person() 析构函数
	//	~Person() 析构函数
}

int main()
{
	//特别注意这种调用方式,类和调用都写在main函数外面,main内部只写一些简单的
	fun();
	return 0;
}  

2.左移运算符

重载左移运算符结合友元可以实现自定义输出
#include <iostream>  
#include <string>  
using namespace std;

class Person
{
	//友元函数
	friend ostream & operator<<(ostream &cout, Person &p);
public:
	//构造函数
	Person();
	//析构函数
	~Person();

private:
	int m_A;
};

Person::Person()
{
	m_A = 10;
}

Person::~Person()
{
	cout<<" ~Person() 析构函数 "<<endl;
}

//重载左移运算符
ostream & operator<<(ostream &cout, Person &p)//ostream 输出
{
	cout<<p.m_A;
	return cout;//返回ostream
}

void fun()
{
	Person p1;
	cout<<p1<<endl;
}

int main()
{
	//特别注意这种调用方式,类和调用都写在main函数外面,main内部只写一些简单的
	fun();
	return 0;
}  

3.递增运算符

后置递增运算符先调用当前值,之后对当前值进行递增运算
前置递增运算符先进行递增运算,然后返回的是递增之后的值
#include <iostream>  
#include <string>  
using namespace std;

class Int
{
	//友元
	friend ostream& operator<<(ostream &cout, Int  &p);

public:
	Int();//构造函数
	~Int();//析构函数

	//前置++ 返回引用
	Int& operator++()
	{
		m_Int ++;
		return *this;
	}

	//后置++ 返回值
	Int operator++(int)
	{
		Int temp = *this;
		temp.m_Int ++;
		return temp;
	}

private:
	int m_Int;
};

Int::Int()
{
	m_Int = 0;//初始化
}

Int::~Int()
{
	cout<<"析构函数"<<endl;//初始化
}

//重载左移运算符
ostream& operator<<(ostream &cout, Int  &p)//ostream 输出
{
	cout<<p.m_Int;
	return cout;//返回ostream
}

void fun1()
{
	Int p;
	cout<<++p<<endl;
	cout<<++p<<endl;
	cout<<p<<endl;
}
 
void fun2()
{
	Int p;
	cout<<p++<<endl;
	cout<<p++<<endl;
	cout<<p++<<endl;
}

int main()
{
	//特别注意这种调用方式,类和调用都写在main函数外面,main内部只写一些简单的
	fun1();
	cout<<"-----------------------------------"<<endl;
	fun2();

	//输出
	//1
	//2
	//2
	//析构函数
	//-----------------------------------
	//析构函数
	//1
	//析构函数
	//析构函数
	//1
	//析构函数
	//1
	//析构函数

	return 0;
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值