最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中如何將operator==定義為類的成員函數(shù)

 更新時(shí)間:2023年01月28日 09:09:26   作者:久許  
這篇文章主要介紹了C++中如何將operator==定義為類的成員函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

C++將operator==定義為類的成員函數(shù)

Duree.h

#ifndef _DUREE_H
#define _DUREE_H
class Duree{
    public:
    Duree(int heures = 0,int minutes = 0,int secondes = 0);
    bool estEgal(Duree const &b)const;
    bool operator==(Duree const& other);//如果把operator==作為類的成員函數(shù),那么只需要一個(gè)參數(shù)就夠了
    private:
    int m_heures;
    int m_minutes;
    int m_secondes;

};
#endif

Duree.cpp

#include<iostream>
#include "Duree.h"
using namespace std;
Duree::Duree(int heures,int minutes,int secondes):m_heures(heures),m_minutes(minutes),m_secondes(secondes){
    cout<<m_heures<<" "<<m_minutes<<" "<<m_secondes<<endl;
}
bool Duree::estEgal(Duree const& other)const{
    return (m_heures==other.m_heures && m_minutes == other.m_minutes && m_secondes == other.m_secondes);
}

bool Duree::operator==(Duree const& other){
    return estEgal(other);
}

Duree_Main.cpp

#include<iostream>
#include "Duree.h"
using namespace std;

int main(){
    Duree first(10,10,10),second(15,20);
    if(first == second)
        cout<< "Les durees sont identiques"<<endl;
    else
        cout<< "Les durees sont differents"<<endl;

    return 0;
}

編譯運(yùn)行

參考

https://stackoverflow.com/questions/23162870/c-bool-operator

C++對(duì)operator=進(jìn)行重寫

/*************************************************************************
	> File Name: copy1.cpp
	> Author: 
	> Mail: 
	> Created Time: 2020年06月17日 星期三 15時(shí)36分57秒
 ************************************************************************/

#include<iostream>
using namespace std;

class Example{
    std::string* ptr;
    public:
    Example():ptr(new std::string){}//new std::string返回的值是存儲(chǔ)字符串的地址
    Example(const std::string& str):ptr(new std::string(str)){}
    Example(const Example& example):ptr(new std::string(example.content())){};
    ~Example(){delete ptr;}
    const std::string& content()const{return *ptr;}
    Example& operator= (const Example& x){
        cout<<"執(zhí)行了"<<endl;
        delete ptr;//釋放指針指向的字符串占用的內(nèi)存
        ptr = new string(x.content());
        return *this;
    }
    Example operator+(const Example & rhs){
        return Example(content()+rhs.content());
    }

    void print_information()const{
        cout<< *ptr<<endl;
    }
};

int main(){
    Example bar1("hello");
    bar1.print_information();
    Example bar2("world");
    bar2.print_information();
    Example bar3 = Example(bar1);
    bar3.print_information();
    Example bar4 = bar2;
    bar4.print_information();

    Example foo("Exam");
    Example ba = Example("ple");
    foo = foo + ba;
    cout<<"foo's content:"<<foo.content()<<endl;


    
    return 0;
}

編譯執(zhí)行

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言 一級(jí)指針與二級(jí)指針詳細(xì)介紹

    C語言 一級(jí)指針與二級(jí)指針詳細(xì)介紹

    這篇文章主要介紹了C語言 一級(jí)指針與二級(jí)指針詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • c++ string的erase刪除方法

    c++ string的erase刪除方法

    這篇文章主要介紹了c++ string的erase刪除方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • 使用c++11 constexpr時(shí)遇到的坑詳解

    使用c++11 constexpr時(shí)遇到的坑詳解

    c++11 constexpr將變量聲明為constexpr類型以便由編譯器來驗(yàn)證變量是否是一個(gè)常量表達(dá)式,這篇文章主要給大家介紹了關(guān)于使用c++11 constexpr時(shí)遇到的坑,需要的朋友可以參考下
    2021-05-05
  • C++實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng)

    C++實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++?常量成員函數(shù)學(xué)習(xí)筆記

    C++?常量成員函數(shù)學(xué)習(xí)筆記

    這篇文章主要為大家介紹了C++?常量成員函數(shù)學(xué)習(xí)筆記,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • C/C++語言中結(jié)構(gòu)體的內(nèi)存分配小例子

    C/C++語言中結(jié)構(gòu)體的內(nèi)存分配小例子

    當(dāng)未用 #pragma 指令指定編譯器的對(duì)齊位數(shù)時(shí),結(jié)構(gòu)體按最長(zhǎng)寬度的數(shù)據(jù)成員的寬度對(duì)齊;當(dāng)使用了 #pragma 指令指定編譯器的對(duì)齊位數(shù)時(shí),結(jié)構(gòu)體按最長(zhǎng)寬度的數(shù)據(jù)成員的寬度和 #pragma 指令指定的位數(shù)中的較小值對(duì)齊
    2013-10-10
  • 全面了解C語言?static?關(guān)鍵字

    全面了解C語言?static?關(guān)鍵字

    這篇文章主要介紹了全面了解C語言?static?關(guān)鍵字,文章首先通過先介紹一下頭文件的創(chuàng)建展開主題的詳細(xì)內(nèi)容,需要的小伙伴可以參考一下
    2022-04-04
  • C語言超詳細(xì)講解遞歸算法漢諾塔

    C語言超詳細(xì)講解遞歸算法漢諾塔

    漢諾塔問題是一個(gè)經(jīng)典的問題。漢諾塔(Hanoi Tower),又稱河內(nèi)塔,源于印度一個(gè)古老傳說。本文將用Java求解這一問題,感興趣的可以學(xué)習(xí)一下
    2022-05-05
  • C++11, 14, 17對(duì)tuple元素的訪問詳情

    C++11, 14, 17對(duì)tuple元素的訪問詳情

    這篇文章主要介紹了C++11, 14, 17對(duì)tuple元素的訪問詳情,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • C++實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理系統(tǒng)

    C++實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)簡(jiǎn)單學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論

正安县| 湘乡市| 原平市| 东源县| 台前县| 革吉县| 凤城市| 山东省| 南澳县| 桐柏县| 民勤县| 汉阴县| 黔东| 临城县| 大庆市| 余庆县| 江安县| 贵州省| 敦煌市| 定西市| 云龙县| 丘北县| 鹤山市| 田东县| 广丰县| 大连市| 治多县| 宝丰县| 建德市| 卓尼县| 正阳县| 呼和浩特市| 镇巴县| 澄江县| 上栗县| 吴堡县| 将乐县| 深州市| 常德市| 吴桥县| 安吉县|