欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++基礎知識之運算符重載詳解

 更新時間:2022年02月23日 10:05:55   作者:駱駝胡楊  
這篇文章主要為大家詳細介紹了C++基礎知識之運算符重載,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

運算符重載

  • 為什么要使用運算符重載

-C/C++的運算符,支持的數據類型,僅限于基本數據類型。

  • 問題:一頭牛+一頭馬 = ?(牛馬神獸?)

一個圓 +一個圓 = ? (想要變成一個更大的圓)
一頭牛 – 一只羊 = ? (想要變成4只羊,原始的以物易物:1頭牛價值5只羊)

  • 解決方案:

使用運算符重載

方式一, 使用成員函數重載運算符 需求:把牛肉換豬肉, 羊肉換豬肉

規(guī)則:

一斤牛肉: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;
}

兩種方式的區(qū)別

區(qū)別:

使用成員函數來實現運算符重載時,少寫一個參數,因為第一個參數就是this指針。

兩種方式的選擇:

  • 一般情況下,單目運算符重載,使用成員函數進行重載更方便(不用寫參數)
  • 一般情況下,雙目運算符重載,使用友元函數更直觀

方便實現a+b和b+a相同的效果,成員函數方式無法實現。

例如: 100 + cow; 只能通過友元函數來實現

cow +100; 友元函數和成員函數都可以實現

  • 特殊情況:

(1)= () [ ] -> 不能重載為類的友元函數?。。。ǚ駝t可能和C++的其他規(guī)則矛盾),只能使用成員函數形式進行重載。

(2)如果運算符的第一個操作數要求使用隱式類型轉換,則必須為友元函數(成員函數方式的第一個參數是this指針)

注意:

同一個運算符重載, 不能同時使用兩種方式來重載,會導致編譯器不知道選擇哪一個(二義性)

總結

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關注腳本之家的更多內容!   

相關文章

  • C語言深入了解函數

    C語言深入了解函數

    C語言函數是用來模塊化構建程序的。如果你的功能少,你可以全都寫在mian函數中,但是當實現功能多的時候,如果全寫在main的函數里,不僅代碼不美觀,而且函數實現的時候結構復雜,代碼重復
    2022-05-05
  • c++中try catch的用法小結

    c++中try catch的用法小結

    這篇文章主要介紹了c++中try catch的用法小結,需要的朋友可以參考下
    2018-01-01
  • C語言數據結構之 折半查找實例詳解

    C語言數據結構之 折半查找實例詳解

    這篇文章主要介紹了C語言數據結構之 折半查找實例詳解的相關資料,需要的朋友可以參考下
    2017-06-06
  • 淺析C++標準庫元組(tuple)源碼

    淺析C++標準庫元組(tuple)源碼

    這篇文章主要介紹了C++標準庫元組(tuple)源碼,介紹了什么是元組以及用法,并進行了源碼分析,需要的朋友可以參考下
    2015-08-08
  • QT實現文件傳輸功能

    QT實現文件傳輸功能

    這篇文章主要為大家詳細介紹了QT實現文件傳輸功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • C++ Boost PropertyTree解析INI文件詳解

    C++ Boost PropertyTree解析INI文件詳解

    Boost PropertyTree庫不僅可以解析JSON,XML格式,還可以直接解析INI格式文件。這篇文章就是為大家介紹一下如何通過Boost PropertyTree解析INI文件,需要的可以參考一下
    2022-01-01
  • 詳解C/C++性能優(yōu)化背后的方法論TMAM

    詳解C/C++性能優(yōu)化背后的方法論TMAM

    開發(fā)過程中我們多少都會關注服務的性能,然而性能優(yōu)化是相對比較困難,往往需要多輪優(yōu)化、測試,屬于費時費力,有時候還未必有好的效果。但是如果有較好的性能優(yōu)化方法指導、工具輔助分析可以幫助我們快速發(fā)現性能瓶頸所在,針對性地進行優(yōu)化,可以事半功倍
    2021-06-06
  • C++迭代器介紹(iterator、const_iterator、reverse_interator、const_reverse_interator)

    C++迭代器介紹(iterator、const_iterator、reverse_interator、const_rev

    這篇文章主要介紹了C++迭代器介紹(iterator、const_iterator、reverse_interator、const_reverse_interator),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • opencv實現輪廓高斯濾波平滑

    opencv實現輪廓高斯濾波平滑

    這篇文章主要為大家詳細介紹了opencv實現輪廓高斯濾波平滑,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • C語言實現三子棋游戲簡易版

    C語言實現三子棋游戲簡易版

    這篇文章主要為大家詳細介紹了C語言實現三子棋游戲簡易版,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論