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)算符重載
通過重載遞增運(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)算符重載
通過重載遞減運(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ù)(此處不舉例,具體可見核心篇 5)
#include <iostream>
using namespace std;
class Student {
public:
// 構(gòu)造函數(shù)
Student(int id) {
m_id = new int(id); // 從堆區(qū)開辟內(nè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ù)來作深拷貝
Student s2(s1);
cout << s2.getId() << endl; // 1
// 用賦值重載運(yùn)算符來作賦值
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ū)開辟內(nè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ù);沒有固定寫法,非常靈活
#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é)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
C++有限狀態(tài)機(jī)實(shí)現(xiàn)計(jì)算器小程序
這篇文章主要為大家詳細(xì)介紹了C++有限狀態(tài)機(jī)實(shí)現(xiàn)計(jì)算器小程序的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
C++瓦片地圖坐標(biāo)轉(zhuǎn)換的實(shí)現(xiàn)詳解
常見的瓦片地圖有矩形、菱形、正六邊形幾種。此文章主要討論菱形瓦片,也就是大家常說的2.5D,斜45度瓦片地圖。比如《紅警2》、《帝國(guó)時(shí)代2》都是采用這種技術(shù)2022-09-09
C++實(shí)現(xiàn)LeetCode(61.旋轉(zhuǎn)鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(61.旋轉(zhuǎn)鏈表),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
詳解C語言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)
這篇文章主要介紹了C語言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中是如何存儲(chǔ)的,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家了解學(xué)習(xí)C語言整數(shù)和浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)有一定的幫助,需要的朋友可以參考下2024-03-03
C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié)
這篇文章主要介紹了C語言中獲取和改變目錄的相關(guān)函數(shù)總結(jié),包括getcwd()函數(shù)和chdir()函數(shù)以及chroot()函數(shù)的使用方法,需要的朋友可以參考下2015-09-09

