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

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

 更新時(shí)間:2023年01月28日 09:09:26   作者:久許  
這篇文章主要介紹了C++中如何將operator==定義為類的成員函數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(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++對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返回的值是存儲字符串的地址
    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語言中讀取時(shí)間日期的基本方法

    C語言中讀取時(shí)間日期的基本方法

    這篇文章主要介紹了C語言中讀取時(shí)間日期的基本方法,分別是time()函數(shù)和gmtime()函數(shù)的使用,注意返回值的區(qū)別,需要的朋友可以參考下
    2015-08-08
  • 在std::thread中創(chuàng)建并管理QEventLoop的全面解析

    在std::thread中創(chuàng)建并管理QEventLoop的全面解析

    QEventLoop的工作原理可以簡單地理解為一個(gè)無限循環(huán),它會不斷地檢查是否有新的事件需要處理,如果有,就將事件從事件隊(duì)列中取出,然后找到相應(yīng)的事件處理器進(jìn)行處理,這篇文章主要介紹了在std::thread中創(chuàng)建并管理QEventLoop的全面指南,需要的朋友可以參考下
    2023-06-06
  • C++中不得不說的map容器

    C++中不得不說的map容器

    大家好,本篇文章主要講的是C++中不得不說的map容器,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • C++入門之list的使用詳解

    C++入門之list的使用詳解

    這篇文章主要為大家介紹了C++入門之list的使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-11-11
  • C語言for循環(huán)嵌套for循環(huán)在實(shí)踐題目中應(yīng)用詳解

    C語言for循環(huán)嵌套for循環(huán)在實(shí)踐題目中應(yīng)用詳解

    初學(xué)C語言,常常遇到for循環(huán)中嵌套個(gè)for循環(huán),初學(xué)者對于這種形式總是一知半解,這次我就整理了常見的for循環(huán)嵌套for循環(huán)的題目,我們一起爭取一舉拿下這類題。學(xué)廢他們,以后再見到就不怕啦!每天都要學(xué)一點(diǎn)呀。加油,奮斗的我們
    2022-05-05
  • C++調(diào)用matlab引擎實(shí)現(xiàn)三維圖的繪制

    C++調(diào)用matlab引擎實(shí)現(xiàn)三維圖的繪制

    這篇文章主要為大家詳細(xì)介紹了C++如何調(diào)用matlab引擎實(shí)現(xiàn)三維圖的繪制,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C++和Matlab有一定的幫助,需要的可以參考一下
    2022-12-12
  • 解決了個(gè)困擾了2天的問題,定點(diǎn)運(yùn)算問題

    解決了個(gè)困擾了2天的問題,定點(diǎn)運(yùn)算問題

    本文主要講解定點(diǎn)運(yùn)算問題,需要的朋友可以參考一下。
    2016-06-06
  • C語言中pthread_exit和pehread_join的使用

    C語言中pthread_exit和pehread_join的使用

    pthread_exit用于強(qiáng)制退出一個(gè)線程,pthread_join用于阻塞等待線程退出,獲取線程退出狀態(tài),本文主要介紹了C語言中pthread_exit和pehread_join函數(shù)的使用,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 一文讀懂C++ 虛函數(shù) virtual

    一文讀懂C++ 虛函數(shù) virtual

    這篇文章主要介紹了C++ 虛函數(shù) virtual的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 實(shí)現(xiàn)posix消息隊(duì)列示例分享

    實(shí)現(xiàn)posix消息隊(duì)列示例分享

    這篇文章主要介紹了實(shí)現(xiàn)posix消息隊(duì)列示例,學(xué)習(xí)記錄鎖,線程互斥量,線程條件變量,內(nèi)存映射,信號,線程的綜合應(yīng)用,需要的朋友可以參考下
    2014-02-02

最新評論