In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
 
            
                     
                
2025-10-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
这篇文章主要介绍C++中运算符重载怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
运算符重载
为什么要使用运算符重载
-C/C++的运算符,支持的数据类型,仅限于基本数据类型。
问题:一头牛+一头马 = ?(牛马神兽?)
一个圆 +一个圆 = ? (想要变成一个更大的圆)
一头牛 – 一只羊 = ? (想要变成4只羊,原始的以物易物:1头牛价值5只羊)
解决方案:
使用运算符重载
方式一, 使用成员函数重载运算符 需求:把牛肉换猪肉, 羊肉换猪肉
规则:
一斤牛肉:2斤猪肉
一斤羊肉:3斤猪肉
实现:
牛 + 牛 = ?猪肉
牛 + 羊 = ?猪肉
Cow类
> Cow.h#pragma onceclass 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 using namespace std;class Pork{ //猪肉类public: Pork(int weight = 0); string description() const;private: int weight;};_________________________________________________________________________________________________________________________________> Pork.cpp#include #include "Pork.h"Pork::Pork(int weight){ this->weight = weight;}string Pork::description() const{ stringstream ret; ret weight Pork.h#pragma once#include using namespace std;class Pork{ //猪肉类public: Pork(int weight = 0); string description() const;private: int weight;};_________________________________________________________________________________________________________________________________> Pork.cpp#include #include "Pork.h"Pork::Pork(int weight){ this->weight = weight;}string Pork::description() const{ stringstream ret; ret weight
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about

The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r


A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from

Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





 
             
            About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.