C++基础知识之运算符重载详解

 更新时间:2022年02月23日 10:05:55   作者:骆驼胡杨  
这篇文章主要为大家详细介绍了C++基础知识之运算符重载,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

运算符重载

  • 为什么要使用运算符重载

-C/C++的运算符,支持的数据类型,仅限于基本数据类型。

  • 问题:一头牛+一头马 = ?(牛马神兽?)

一个圆 +一个圆 = ? (想要变成一个更大的圆)
一头牛 – 一只羊 = ? (想要变成4只羊,原始的以物易物:1头牛价值5只羊)

  • 解决方案:

使用运算符重载

方式一, 使用成员函数重载运算符 需求:把牛肉换猪肉, 羊肉换猪肉

规则:

一斤牛肉:2斤猪肉

一斤羊肉:3斤猪肉

实现:

牛 + 牛 = ?猪肉

牛 + 羊 = ?猪肉

 Cow类

> Cow.h
#pragma once
class Pork;
class Sheep;	
class Cow{	//牛类
public:
	Cow(int weight = 0);
	//使用运算符重载, 实现 牛肉 + 牛肉 = 猪肉 
	Pork operator+(const Cow& cow);
	//使用运算符重载, 实现 牛肉 + 羊肉 = 猪肉 
	Pork operator+(const Sheep& sheep);
private:
	int weight;	//重量
};
_________________________________________________________________________________________________________________________________
> Cow.cpp
#include "Cow.h"
#include "Pork.h"
#include "Sheep.h"
Cow::Cow(int weight){
	this->weight = weight;
}
//一斤牛肉换两斤猪肉
Pork Cow::operator+(const Cow& cow){	
	return Pork((this->weight + cow.weight) * 2);
}
//一斤牛肉换两斤猪肉, 一斤羊肉换三斤猪肉
Pork Cow::operator+(const Sheep& sheep){
	int tmp = (this->weight * 2) + (sheep.getWeight() * 3);
	return Pork(tmp);
}

 Sheep类

> Sheep.h
#pragma once
//羊类
class Sheep{
public:
	Sheep(int weight = 0);
	int getWeight() const;
private:
	int weight;	//重量
};
_________________________________________________________________________________________________________________________________
> Sheep.cpp
#include "Sheep.h"
Sheep::Sheep(int weight){
	this->weight = weight;
}
int Sheep::getWeight() const{
	return weight;
}

Pork类

> Pork.h
#pragma once
#include <string>
using namespace std;
class Pork{	//猪肉类
public:
	Pork(int weight = 0);
	string description() const;
private:
	int weight;
};
_________________________________________________________________________________________________________________________________
> Pork.cpp
#include <sstream>
#include "Pork.h"
Pork::Pork(int weight){
	this->weight = weight;
}
string Pork::description() const{
	stringstream ret;
	ret << this->weight << "斤";
	return ret.str();
}

main.cpp

#include <iostream>
#include <Windows.h>
#include "Cow.h"
#include "Pork.h"
#include "Sheep.h"
using namespace std;
int main(void) {
	Pork p1;
	Cow c1(100);
	Cow c2(200);
	Sheep s1(100);
	//调用运算符重载 Pork operator+(const Cow& cow);
	p1 = c1 + c2;
	cout << "牛 + 牛 = 猪肉:" << p1.description() << endl;
	//调用运算符重载 Pork operator+(const Sheep& c1);
	p1 = c1 + s1;
	cout << "牛 + 羊 = 猪肉:" << p1.description() << endl;
	//羊+牛会报错, 因为没有定义对应的羊+牛运算符重载
	//p1 = s1 + c1;
	system("pause");
	return 0;
}

方式二, 使用非成员函数【友元函数】重载运算符

实现:

牛 + 牛 = ?猪肉

牛 + 羊 = ?猪肉

 Cow类

> Cow.h
#pragma once
class Pork;
class Sheep;	
class Cow{	//牛类
public:
	Cow(int weight = 0);
	//使用友元运算符重载, 实现 牛肉 + 牛肉 = 猪肉 
	friend Pork operator+(const Cow& c1, const Cow& c2);
	//使用友元运算符重载, 实现 牛肉 + 羊肉 = 猪肉 
	friend Pork operator+(const Cow& c1, const Sheep& s1);
private:
	int weight;	//重量
};
_________________________________________________________________________________________________________________________________
> Cow.cpp
#include "Cow.h"
Cow::Cow(int weight){
	this->weight = weight;
}

 Sheep类

> Sheep.h
#pragma once
//羊类
class Sheep{
public:
	Sheep(int weight = 0);
	int getWeight() const;
private:
	int weight;	//重量
};
_________________________________________________________________________________________________________________________________
> Sheep.cpp
#include "Sheep.h"
Sheep::Sheep(int weight){
	this->weight = weight;
}
int Sheep::getWeight() const{
	return weight;
}

Pork类

> Pork.h
#pragma once
#include <string>
using namespace std;
class Pork{	//猪肉类
public:
	Pork(int weight = 0);
	string description() const;
private:
	int weight;
};
_________________________________________________________________________________________________________________________________
> Pork.cpp
#include <sstream>
#include "Pork.h"
Pork::Pork(int weight){
	this->weight = weight;
}
string Pork::description() const{
	stringstream ret;
	ret << this->weight << "斤";
	return ret.str();
}

main.cpp

#include <iostream>
#include <Windows.h>
#include "Cow.h"
#include "Pork.h"
#include "Sheep.h"
using namespace std;
//要想访问类的私有数据成员, 就把这个函数定义为友元
Pork operator+(const Cow& c1, const Cow& c2) {
	return ((c1.weight + c2.weight) * 2);
}
//要想访问类的私有数据成员, 就把这个函数定义为友元
Pork operator+(const Cow& c1, const Sheep& s1) {
	return((c1.weight * 2) + (s1.getWeight() * 3));
}
int main(void) {
	Pork p1;		
	Cow c1(100);	//100斤的牛
	Cow c2(200);	//200斤的牛
	Sheep s1(100);	//100斤的羊
	//调用 friend Pork operator+(const Cow& c1, const Cow& c2);
	p1 = c1 + c2;
	cout << "使用友元 牛 + 牛 = 猪肉:" << p1.description() << endl;
	//调用 friend Pork operator+(const Cow& c1, const Sheep& s1);
	p1 = c1 + s1;
	cout << "使用友元 牛 + 羊 = 猪肉:" << p1.description() << endl;
	system("pause");
	return 0;
}

两种方式的区别

区别:

使用成员函数来实现运算符重载时,少写一个参数,因为第一个参数就是this指针。

两种方式的选择:

  • 一般情况下,单目运算符重载,使用成员函数进行重载更方便(不用写参数)
  • 一般情况下,双目运算符重载,使用友元函数更直观

方便实现a+b和b+a相同的效果,成员函数方式无法实现。

例如: 100 + cow; 只能通过友元函数来实现

cow +100; 友元函数和成员函数都可以实现

  • 特殊情况:

(1)= () [ ] -> 不能重载为类的友元函数!!!(否则可能和C++的其他规则矛盾),只能使用成员函数形式进行重载。

(2)如果运算符的第一个操作数要求使用隐式类型转换,则必须为友元函数(成员函数方式的第一个参数是this指针)

注意:

同一个运算符重载, 不能同时使用两种方式来重载,会导致编译器不知道选择哪一个(二义性)

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!   

相关文章

  • C语言通讯录实例分享

    C语言通讯录实例分享

    这篇文章主要为大家分享了C语言通讯录实例,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • C语言 动态内存分配的详解及实例

    C语言 动态内存分配的详解及实例

    这篇文章主要介绍了C语言 动态内存分配的详解及实例的相关资料,需要的朋友可以参考下
    2016-09-09
  • 带你用C语言实现strtok和字符串分割函数

    带你用C语言实现strtok和字符串分割函数

    下面小编就为大家带来一篇c语言中字符串分割函数及实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-09-09
  • C#如何调用原生C++ COM对象详解

    C#如何调用原生C++ COM对象详解

    这篇文章主要给大家介绍了C#如何调用原生C++ COM对象,在C++中实现C#的接口。文中通过示例代码介绍的很详细,相信对大家的理解和学习会有一定的参考借鉴价值,有需要的朋友们下面来一起看看吧。
    2016-12-12
  • C语言实现简单弹跳球游戏

    C语言实现简单弹跳球游戏

    这篇文章主要为大家详细介绍了C语言实现简单弹跳球游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-03-03
  • C++实现线程池的简单方法示例

    C++实现线程池的简单方法示例

    这篇文章主要给大家介绍了关于C++实现线程池的简单方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C++具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-05-05
  • C++超详细讲解稀疏矩阵

    C++超详细讲解稀疏矩阵

    今天小编就为大家分享一篇关于C++稀疏矩阵的转置思路并实现乘法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2022-05-05
  • C++中新手容易犯的十种编程错误汇总

    C++中新手容易犯的十种编程错误汇总

    一段C语言代码,在编译、链接和运行的各个阶段都可能会出现问题,下面这篇文章主要给大家介绍了关于C++中新手容易犯的十种编程错误的相关资料,需要的朋友可以参考下
    2021-10-10
  • C语言结构体超详细讲解

    C语言结构体超详细讲解

    C语言结构体(Struct)从本质上讲是一种自定义的数据类型,只不过这种数据类型比较复杂,是由 int、char、float 等基本类型组成的。你可以认为结构体是一种聚合类型
    2022-04-04
  • 深入理解C++中std::chrono库的使用

    深入理解C++中std::chrono库的使用

    在程序设计中,时间管理是一个核心概念,它不仅关系到程序的效率和性能,而且直接影响用户体验,C++作为一门高效的编程语言,提供了std::chrono库,用于精确地处理和计算时间,下面就跟随小编一起学习一下std::chrono库的使用吧
    2023-12-12

最新评论