C++類與對(duì)象之運(yùn)算符重載詳解
運(yùn)算符重載
運(yùn)算符重載概念:對(duì)已有的運(yùn)算符重新進(jìn)行定義,賦予其另一種功能,以適應(yīng)不同的數(shù)據(jù)類型
加號(hào)運(yùn)算符重載
作用:實(shí)現(xiàn)兩個(gè)自定義數(shù)據(jù)類型相加的運(yùn)算
#include <iostream> using namespace std; class Person { public: // 構(gòu)造函數(shù) Person(int num1, int num2){ this->num1 = num1; this->num2 = num2; } // 加法函數(shù) Person operator+(const Person &p){ Person temp(0, 0); temp.num1 = this->num1 + p.num1; temp.num2 = this->num2 + p.num2; return temp; } // 打印輸出 void printMessage() { cout << "num1 = " << num1 << " num2 = " << num2 << endl; } private: int num1; int num2; }; void func() { Person p1(1, 2); Person p2(3, 4); Person p3 = p1.operator+(p2); // 可以簡(jiǎn)化為 Person = p1 + p2; p3.printMessage(); // num1 = 4 num2 = 6 Person p4 = p3 + p2; p4.printMessage(); // num1 = 7 num2 = 10 } int main() { func(); system("pause"); return 0; }
左移運(yùn)算符重載
作用:輸出自定義數(shù)據(jù)類型
第一種:成員函數(shù)(對(duì)象 << cout)
#include <iostream> using namespace std; class Person { public: // 構(gòu)造函數(shù) Person(int num1, int num2){ this->num1 = num1; this->num2 = num2; } // 左移運(yùn)算符 p.operator<<(cout); 簡(jiǎn)化版本 p << cout void operator <<(ostream &cout) { cout << "num1 = " << num1 << " num2 = " << num2 << endl; } private: int num1; int num2; }; void func() { Person p1(1, 2); p1.operator<<(cout); // 簡(jiǎn)化為 p1 << cout Person p2(3, 4); p2 << cout; } int main() { func(); // outputs:num1 = 1 num2 = 2 // num1 = 3 num2 = 4 system("pause"); return 0; }
第二種:全局函數(shù)(cout << 對(duì)象)
#include <iostream> using namespace std; class Person { // 友元 friend ostream& operator <<(ostream &cout, Person &p); public: // 構(gòu)造函數(shù) Person(int num1, int num2){ this->num1 = num1; this->num2 = num2; } private: int num1; int num2; }; // 左移運(yùn)算符 p.operator<<(cout); 簡(jiǎn)化版本 p << cout ostream& operator <<(ostream &cout, Person &p) { cout << "num1 = " << p.num1 << " num2 = " << p.num2; return cout; } void func() { Person p1(1, 2); operator<<(cout, p1); // 簡(jiǎn)化為 cout << p1; cout << endl; cout << p1 << " 你是豬!" << endl; } int main() { func(); // outputs:num1 = 1 num2 = 2 // num1 = 1 num2 = 2 你是豬! system("pause"); return 0; }
遞增運(yùn)算符重載
通過(guò)重載遞增運(yùn)算符,實(shí)現(xiàn)自己的整型數(shù)據(jù)
#include <iostream> using namespace std; class MyInteger { //friend ostream& operator <<(ostream &cout, MyInteger &p); friend ostream& operator <<(ostream &cout, MyInteger p); public: // 構(gòu)造函數(shù) MyInteger(){ this->m_num = 0; } // 重載前置遞增運(yùn)算符 MyInteger& operator++() { ++this->m_num; return *this; } // 重載后置遞增運(yùn)算符 MyInteger operator++(int) { // int 是占位參數(shù),用于區(qū)分前置和后置遞增 MyInteger temp = *this; ++this->m_num; return temp; } private: int m_num; }; // 用于前置遞增,左移運(yùn)算符 p.operator<<(cout); 簡(jiǎn)化版本 p << cout //ostream& operator <<(ostream &cout, MyInteger &p) { // cout << "m_num = " << p.m_num; // return cout; //} // 用于后置遞增,左移運(yùn)算符 p.operator<<(cout); 簡(jiǎn)化版本 p << cout ostream& operator <<(ostream &cout, MyInteger p) { cout << "m_num = " << p.m_num; return cout; } void func() { MyInteger m_int; //cout << m_int.operator++() << endl; // m_num = 1 //cout << ++m_int << endl; // m_num = 2 cout << m_int++ << endl; // m_num = 0 cout << m_int << endl; // m_num = 1 } int main() { func(); system("pause"); return 0; }
遞減運(yùn)算符重載
通過(guò)重載遞減運(yùn)算符,實(shí)現(xiàn)自己的整型數(shù)據(jù)
#include <iostream> using namespace std; class MyInteger { friend ostream& operator <<(ostream &cout, MyInteger &p); //friend ostream& operator <<(ostream &cout, MyInteger p); public: // 構(gòu)造函數(shù) MyInteger(){ this->m_num = 0; } // 重載前置遞減運(yùn)算符 MyInteger& operator--() { --this->m_num; return *this; } // 重載后置遞減運(yùn)算符 MyInteger operator--(int) { // int 是占位參數(shù),用于區(qū)分前置和后置遞增 MyInteger temp = *this; --this->m_num; return temp; } private: int m_num; }; // 用于前置遞減,左移運(yùn)算符 p.operator<<(cout); 簡(jiǎn)化版本 p << cout ostream& operator <<(ostream &cout, MyInteger &p) { cout << "m_num = " << p.m_num; return cout; } // 用于后置遞減,左移運(yùn)算符 p.operator<<(cout); 簡(jiǎn)化版本 p << cout //ostream& operator <<(ostream &cout, MyInteger p) { // cout << "m_num = " << p.m_num; // return cout; //} void func() { MyInteger m_int; cout << m_int.operator--() << endl; // m_num = -1 cout << --m_int << endl; // m_num = -2 //cout << m_int-- << endl; // m_num = 0 //cout << m_int << endl; // m_num = -1 } int main() { func(); system("pause"); return 0; }
賦值運(yùn)算符重載
C++ 編譯器至少給一個(gè)類添加 4 個(gè)函數(shù)(此處不舉例,具體可見(jiàn)核心篇 5)
#include <iostream> using namespace std; class Student { public: // 構(gòu)造函數(shù) Student(int id) { m_id = new int(id); // 從堆區(qū)開(kāi)辟內(nèi)存用來(lái)存儲(chǔ) id } // 拷貝構(gòu)造函數(shù) Student(const Student &s) { this->m_id = new int(*s.m_id); } // 析構(gòu)函數(shù) ~Student() { if (m_id != NULL) { delete m_id; m_id = NULL; } } // 重載賦值運(yùn)算符 Student& operator=(Student &s) { if (m_id != NULL) { delete m_id; m_id = NULL; } m_id = new int(*s.m_id); return *this; } // 取出 id int getId() { return *m_id; } private: int *m_id; }; void func() { Student s1(1); cout << s1.getId() << endl; // 1 // 用拷貝構(gòu)造函數(shù)來(lái)作深拷貝 Student s2(s1); cout << s2.getId() << endl; // 1 // 用賦值重載運(yùn)算符來(lái)作賦值 Student s3(2); // id 為 2 s1 = s3; // 復(fù)雜版本:s1.operator=(s3) cout << s1.getId() << endl; // 2 // 多段賦值運(yùn)算符,例如 a = b = c Student s4(3); s1 = s2 = s3 = s4; // id 均為 3 cout << s1.getId() << s2.getId() << s3.getId() << s4.getId() << endl; // 3333 } int main() { func(); system("pause"); return 0; }
關(guān)系運(yùn)算符重載
重載關(guān)系運(yùn)算符,可以讓兩個(gè)自定義數(shù)據(jù)類型對(duì)象進(jìn)行對(duì)比操作
#include <iostream> using namespace std; class Student { public: // 構(gòu)造函數(shù) Student(int id) { m_id = new int(id); // 從堆區(qū)開(kāi)辟內(nèi)存用來(lái)存儲(chǔ) id } // 拷貝構(gòu)造函數(shù) Student(const Student &s) { this->m_id = new int(*s.m_id); } // 析構(gòu)函數(shù) ~Student() { if (m_id != NULL) { delete m_id; m_id = NULL; } } // 重載 == 運(yùn)算符 bool operator==(Student &s) { if (this->getId() == s.getId()) { return true; } else { return false; } } // 取出 id int getId() { return *m_id; } private: int *m_id; }; void func() { Student s1(1); Student s2(1); Student s3(2); if (s1 == s2) { cout << "s1 和 s2 相等" << endl; } if (s1 == s3) { cout << "s1 和 s3 相等" << endl; } else { cout << "s1 和 s3 不相等" << endl; } } int main() { func(); // outputs:s1 和 s2 相等 // s1 和 s3 不相等 system("pause"); return 0; }
函數(shù)調(diào)用運(yùn)算符重載
特點(diǎn):函數(shù)調(diào)用運(yùn)算符 () 也可以重載;重載后調(diào)用的方式很像函數(shù),被稱為仿函數(shù);沒(méi)有固定寫(xiě)法,非常靈活
#include <iostream> using namespace std; #include <string> class Print { public: void operator()(string text) { cout << "利用函數(shù)調(diào)用重載運(yùn)算符打印輸出:" << text << endl; } }; void func() { Print p1; p1("你是一個(gè)小豬豬!"); // 利用函數(shù)調(diào)用重載運(yùn)算符打印輸出:你是一個(gè)小豬豬! } int main() { func(); // outputs:s1 和 s2 相等 // s1 和 s3 不相等 system("pause"); return 0; }
總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Qt/C++編寫(xiě)視頻監(jiān)控系統(tǒng)之自定義音柱顯示功能
通過(guò)音柱控件實(shí)時(shí)展示當(dāng)前播放的聲音產(chǎn)生的振幅的大小,得益于音頻播放組件內(nèi)置了音頻振幅的計(jì)算,可以動(dòng)態(tài)開(kāi)啟和關(guān)閉,開(kāi)啟后會(huì)對(duì)發(fā)送過(guò)來(lái)的要播放的聲音數(shù)據(jù),這篇文章主要介紹了Qt/C++編寫(xiě)視頻監(jiān)控系統(tǒng)之自定義音柱顯示功能,需要的朋友可以參考下2024-01-01C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單餐飲管理與點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單餐飲管理與點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05一篇文章帶你了解C++ static的作用,全局變量和局部變量的區(qū)別
這篇文章介紹了C++ static的作用,全局變量和局部變量的區(qū)別,需要的朋友可以過(guò)來(lái)參考下,希望能夠給你帶來(lái)幫助2021-09-09C/C++函數(shù)參數(shù)聲明解析int?fun()?與?int?fun(void)?的區(qū)別講解
C++中int fun()和int fun(void)的區(qū)別在于函數(shù)參數(shù)的聲明方式,前者默認(rèn)允許任意參數(shù),而后者表示沒(méi)有參數(shù),通過(guò)清晰的實(shí)例源代碼,詳細(xì)解釋了它們?cè)诤瘮?shù)聲明和調(diào)用中的不同之處,這篇文章介紹了C/C++函數(shù)參數(shù)聲明int?fun()與int?fun(void)的差異,需要的朋友可以參考下2024-01-01C/C++使用C語(yǔ)言實(shí)現(xiàn)多態(tài)
這篇文章主要介紹了C/C++多態(tài)的實(shí)現(xiàn)機(jī)制理解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下,希望能給你帶來(lái)幫助2021-08-08C++中的類成員函數(shù)當(dāng)線程函數(shù)
這篇文章主要介紹了C++中的類成員函數(shù)當(dāng)線程函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11