c++学习笔记14类和对象--c++运算符重载

4.5运算符重载

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

4.5.1加号运算符重载

作用:实现两个自定义数据类型相加得运算
1、通过成员函数重载加号

//加号运算符重载

class person
{
public:
     //1、通过成员函数实现加号重载
	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;

};

void test01()
{
	person p1;
	p1.m_A = 10;
	p1.m_B = 20;
	person p2;
	p2.m_A = 15;
	p2.m_B = 30;
	person p3 = p1 + p2;
	cout << p3.m_A << endl;
}

int main()
{
	
	test01();

2、通过全局函数重载加号

person operator+(person &p1,person &p2)//写在类外
	{
		person temp;
		temp.m_A = p1.m_A + p2.m_A;
		temp.m_B = p1.m_B + p2.m_B;
		return temp;
	}

成员函数重载本质调用:
person p3 = p1 + p2;等同于 person p3 = p1.operator+(p2);

全局函数重载本质调用:
person p3 = p1 + p2;等同于 person p3 = operator+(p1,p2);

运算符重载也可以发生函数重载;实现不同数据类型相加,如 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;
	}

即可实现:person p4 = p1 + 100;

总结:1)对于内置的数据类型的表达式的运算符是不可能改变的;即:int string double等
2)不可滥用运算符重载;

4.5.2左移运算符重载

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

//左移运算符重载

class person
{
public:

	int m_A;
	int m_B;
};

//只能利用全局函数重载左移运算符
void operator<<(ostream& cout,person &p) //本质 operator<<(cout,p) 简化 cout<<p
{
	cout << p.m_A <<"      "<< p.m_B;
}
void test01()
{
	person p;
	p.m_A = 10;
	p.m_B = 20;
	cout << p;
}
int main()
{
	test01();

或者:

ostream & operator<<(ostream& cout,person &p) 
{
	cout << p.m_A <<"      "<< p.m_B;
	return cout;
}
void test01()
{
	person p;
	p.m_A = 10;
	p.m_B = 20;
	cout << p<<endl;
}

加入链式编程后,可以在 cout<<p 后面无限追加输出
当类内成员属性为私有时,可以用友元进行访问

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

4.5.3递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据;关于“++”运算符的重载
后置递增返回值,前置递增返回引用

class myinteger
{
	friend ostream& operator<<(ostream& cout, myinteger myint);
public:
	myinteger()
	{
		m_num = 0;
	}
	//重载前置++运算符
	myinteger& operator++() //返回引用为了一直对一个数据进行递增操作
	{
		m_num++;//先进行++运算,再将自身做返回
		return *this;
	}
	//重载后置++运算符
	myinteger& operator++(int) //int代表占位参数,可以用于区分前置和后置递增
	{
		//先记录当时结果
		myinteger temp = *this;
		//再做递增运算
		m_num++;
		//最后将记录结果返回
		return temp;
	}//后置递增步骤是先返回当前值,再做递增运算,若先用return返回值则会出错,所有需要用临时变量记录
private:	
	int m_num;
};


ostream & operator<<(ostream& cout,myinteger myint)
{
	cout << myint.m_num;
	return cout;
}
void test01()
{
	myinteger myint;
	cout << ++myint << endl;
}

void test02()
{
	myinteger myint;
	cout << myint++ << endl;
	cout << myint << endl;
}

int main()
{
	
	test01();
	test02();

4.5.4赋值运算符重载

c++编译器至少给一个类添加4个函数
1、默认构造函数(无参,函数体为空)
2、默认析构函数(无参,函数体为空)
3、默认拷贝构造函数,对属性进行值拷贝
4、赋值运算符operator=,对属性进行值拷贝

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

class person
{
public:
	person(int age)
	{
		m_age = new int(age);
	}
	~person()  //堆区数据由程序员手动添加,手动释放,所以需要手动添加析构函数
	{
		if (m_age != NULL)
		{
			delete m_age;
			m_age = NULL;
		}
	}
	//重载赋值运算符
	person& operator=(person& p)
	{
		//编译器是提供浅拷贝
		//m_age = p.m_age;

		//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
		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 p1(18);
	person p2(20);
	//p2 = p1;直接写会引起堆区内存重复释放,导致程序崩溃
	//解决办法:利用深拷贝解决浅拷贝带来的问题,进行赋值运算符的重载操作

	p2 = p1;
	cout << "p1= " << *p1.m_age << endl;
	cout << "p2= " << *p2.m_age << endl;
}


int main()
{
	
	test01();

4.5.5关系运算符重载

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

//==运算符重载
class person
{
public:
	person(string name, int age)
	{
		m_name = name;
		m_age = age;
	}
	bool operator==(person& p)
	{
		if (this->m_name == p.m_name && this->m_age == p.m_age)
		{
			return true;
		}
		return false;
	}
	
	int m_age;
	string m_name;
};

void test01()
{
	person p1("tom", 18);
	person p2("jay", 18);
	if (p1 == p2)
	{
		cout << "p1和p2是同一个人!" << endl;
	}
	else
	{
		cout << "p1和p2不是同一个人!" << endl;
	}
}


int main()
{
	
	test01();

!=运算符重载类似

4.5.6函数调用运算符重载

1)函数调用运算符()也可以重载
2)由于重载后使用的方式非常像函数的调用,因此称为仿函数
3)仿函数没有固定写法,非常灵活

//仿函数非常灵活,没有固定的写法
//例1:打印输出类
class myprint
{
public:
	void operator()(string test)
	{
		cout << test << endl;
	}
	
	int m_age;
	string m_name;
};

void print02(string test)
{
	cout << test << endl;
}
void test01()
{
	myprint print;
	print("hello world");//通过重载()打印输出,非常像函数调用,故称为仿函数
	print02("hello world 02");//通过函数调用实现打印输出
}

//例2:加法类
class myadd
{
public:
	int operator()(int num1, int num2)
	{
		return num1 + num2;
	}
};

void test02()
{
	myadd add1;
	int ret = add1(19, 21);
	cout << ret << endl;
//匿名函数对象
cout<<myadd()(19,21)<<endl;//效果等同于上述三步
}

int main()
{
	
	test01();
	test02();

其中myadd()为匿名对象,运行完立即被释放

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值