日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

复习笔记(六)——C++运算符重载(难点)

發布時間:2025/3/21 c/c++ 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 复习笔记(六)——C++运算符重载(难点) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

運算符重載

運算符重載的概念

運算符重載類似于函數重載。

運算符重載允許把標準運算符(如+、-、*、<等)應用于定制數據類型的對象。

什么情況下需要考慮運算符重載?
需要用運算符操作自定義類的對象時,如對象之間直觀自然,可以提高比較大小等,通過重載支持類的運算。

運算符重載:①體現了程序的可讀性;②體現了C++的可擴充性

運算符重載的定義

作為類的成員函數或友元函數、作為一般函數(很少用)。

1、成員函數原型的格式:
函數類型 operator 運算符(參數表);
成員函數定義的格式:

函數類型 類名::operator 運算符(參數表) {函數體; }

以成員函數的方式重載運算符
-單目運算符:不帶參數,該類對象為唯一操作數
-雙目運算符:帶一個參數,該類對象為左操作數、參數為右操作數


2、友元函數原型的格式:
friend 函數類型 operator 運算符(參數表);
友元函數定義的格式:

函數類型 operator 運算符(參數表) {函數體; }

以友元函數的方式重載運算符
-單目運算符:帶一個參數,該參數為唯一操作數,是自定義類的對象 ++(a)
-雙目運算符:帶兩個參數,第一個參數為左操作數、第二個參數為右操作數,至少有一個參數為自定義類的對象
+(a, b)

實例

#include <iostream> using namespace std;class Complex { public:Complex(double = 0.0, double = 0.0);Complex operator+(const Complex&) const;Complex Add(const Complex&) const;Complex operator-(const Complex&) const;Complex& operator=(const Complex&);void print() const; private:double real; // real partdouble imaginary; // imaginary part }; Complex::Complex(double r, double i) {real = r;imaginary = i; } Complex Complex::operator+(const Complex &operand2) const {Complex sum;sum.real = this->real + operand2.real;sum.imaginary= this->imaginary + operand2.imaginary;return sum; } Complex Complex::Add(const Complex &operand2) const {//功能的實現同上 } Complex Complex::operator-(const Complex &operand2) const {Complex diff;diff.real = real - operand2.real;diff.imaginary=imaginary - operand2.imaginary;return diff; } Complex& Complex::operator=(const Complex &right) {real = right.real;imaginary = right.imaginary;return *this; // enables concatenation } void Complex::print() const {cout<<'('<<real<< "," << imaginary << ')'; } int main() {Complex x, y(4.3, 8.2), z(3.3, 1.1);cout << "x: "; x.print();cout << "\ny: "; y.print();cout << "\nz: "; z.print();x = y + z; //比表達式x=y.Add(z);更簡練,更直觀cout << "\n\nx = y + z:\n"; x.print();cout << " = "; y.print();cout << " + "; z.print();return 0; }

執行結果:

x: (0,0) y: (4.3,8.2) z: (3.3,1.1)x = y + z: (7.6,9.3) = (4.3,8.2) + (3.3,1.1)

運算符重載的規則

①運算符重載不允許發明新的運算符。

②不能改變運算符操作對象的個數。

③運算符被重載后,其優先級和結合性不會改變。

④不能重載的運算符:

一元運算符重載

操作數是自定義類的對象或對象的引用。

作為成員函數重載沒有參數。

作為友元函數重載參數為自定義類的對象或對象的引用(概念介紹)。

實例

(成員函數的方式重載!)

#include <iostream>**自增、自減運算符重載** #include <string.h> using namespace std;class CString { public:CString(const char *s="");CString(const CString& s);~CString();CString& operator = (const CString& s);CString& operator = (const char *s);bool operator !();char *m_str; private:int m_size; }; CString::CString(const CString& s) {m_size=strlen(s.m_str);m_str=new char[m_size+1];strcpy(m_str,s.m_str); } CString::CString(const char *s/* ="" */) {m_size=strlen(s);m_str=new char[m_size+1];strcpy(m_str,s); } bool CString::operator !() {if (strlen(m_str)==0){return true;}elsereturn false; } CString::~CString() {delete []m_str; } int main() {CString s1, s2("some string");if (!s1)//括號中等價于s1.operator!()=>顯示調用cout<<"s1 is NULL!"<<endl;else cout<<"s1 is not NULL!"<<endl;if (!s2)cout<<"s2 is NULL!"<<endl;elsecout<<"s2 is not NULL!"<<endl;return 0; }

執行結果:

s1 is NULL! s2 is not NULL!

自增、自減運算符重載

在C++中,單目運算符有++和- -,它們是變量自動增1和自動減1的運算符。在類中可以對這兩個單目運算符進行重載。

前置自增和前置自減的重載:
1、成員函數的方式重載,原型為:
函數類型 & operator++();
函數類型 & operator--();
2、友元函數的方式重載,原型為:
函數類型 & operator++(類類型 &);
函數類型 & operator--(類類型 &);

后置自增和后置自減的重載:
1、成員函數的方式重載,原型為:
函數類型 operator++(int);
函數類型 operator--(int);
2、友元函數的方式重載,原型為:
函數類型 operator++(類類型 &,int);
函數類型 operator--(類類型 &,int);

使用前綴運算符的語法格式:++<對象>;
使用后綴運算符的語法格式:<對象>++;

實例

#include <iostream> using namespace std;class CInt { public:CInt(int a=0);void Print();CInt &operator ++();CInt operator ++(int); private:int i; }; CInt::CInt (int a) {i = a; } void CInt::Print() {cout << "i=" << i << endl; } CInt &CInt::operator ++() {++i;return *this; } CInt CInt::operator ++(int) {CInt sum;sum=*this;++i;return sum; } int main(void) {CInt a(5), b(5), c, d;c = a++;d = ++b;cout << "a: ";a.Print();cout << "b: ";b.Print();cout << "c: ";c.Print();cout << "d: ";d.Print();return 0; }

執行結果:

a: i=6 b: i=6 c: i=5 d: i=6

二元運算符重載

1、成員函數的方式重載二元運算符
函數原型:
函數類型 operator 二元運算符(類型 參數);
帶有一個參數
左操作數必須為該類的對象或對象的引用

2、二元運算符重載為帶有兩個參數的非成員函數
函數原型:
函數類型 operator 二元運算符(類型 參數1,類型 參數2);
參數之一必須是類的對象或對象的引用

賦值運算符的重載

1、賦值運算符可直接用在自定義類的對象賦值。

2、如果沒有提供重載的賦值運算符函數來復制類的對象。編譯器就會提供默認版本的operator=()。

3、賦值運算符的默認版本會簡單地進行逐個成員的復制過程,類似于默認的拷貝構造函數。

4、運算符“=”重載時,要檢查兩個操作數是否為同一個對象。

5、如果對象中包含動態分配的空間,這種賦值方式就不合適了,如:

CString s1("abc"), s2("def"); //具體類見一元運算符重載實例 s1 = s2;

賦值的結果是:對象s1和s2的指針str都指向了同一塊數據空間。

6、對象中包含動態分配的空間,賦值運算符需要自己重載,函數實現的算法與拷貝構造函數類似。

實例

#include <iostream> #include <string.h> using namespace std;class CString { public:CString(const char *s="");CString(const CString& s);CString & operator = (const CString & s);CString & operator = (const char *s);char *m_str; private:int m_size; }; CString::CString(const CString& s) {m_size=strlen(s.m_str);m_str=new char[m_size+1];strcpy(m_str,s.m_str); } CString::CString(const char *s/* ="" */) {m_size=strlen(s);m_str=new char[m_size+1];strcpy(m_str,s); } CString& CString::operator =(const CString& str) {if (this!=&str){delete[] m_str;m_size=strlen(str.m_str);m_str=new char[m_size+1];strcpy(m_str,str.m_str);}return *this; } CString& CString::operator =(const char *str) {delete[] m_str;m_size=strlen(str);m_str=new char[m_size+1];strcpy(m_str,str);return *this;//為什么需要返回值? } int main() {CString s1("abc"),s2(s1),s3;s3=s2;cout<<"s1:"<<s1.m_str<<endl; //m_str應該聲明成私有,如何輸出cout<<"s2:"<<s2.m_str<<endl; //cout<<s2;cout<<"s3:"<<s3.m_str<<endl;s3="tom";cout<<"s3:"<<s3.m_str<<endl;return 0; }

執行結果:

s1:abc s2:abc s3:abc s3:tom

‘+’運算符重載的使用

實例

#include <iostream> #include <string.h> #include <windows.h> using namespace std;class CString { public:CString(const char *s="");CString(const CString& s);CString operator + (const CString &s);CString operator + (const char *s);CString & operator = (const CString & s);CString & operator = (const char *s);char *m_str; private:int m_size; }; CString::CString(const CString& s) {m_size=strlen(s.m_str);m_str=new char[m_size+1];strcpy(m_str,s.m_str); } CString::CString(const char *s/* ="" */) {m_size=strlen(s);m_str=new char[m_size+1];strcpy(m_str,s); } CString CString::operator+(const CString &s) {CString tempStr;char *p=new char[strlen(this->m_str)+strlen(s.m_str)+1];if(p==NULL){exit(1);}strcpy(p,this->m_str);strcat(p,s.m_str);tempStr.m_str=p;return tempStr; } CString CString::operator+(const char *s) {CString tempStr;char *p=new char[strlen(this->m_str)+strlen(s)+1];strcpy(p,this->m_str);strcat(p,s);tempStr.m_str=p;return tempStr; } CString& CString::operator =(const CString& str) {if (this!=&str){delete[] m_str;m_size=strlen(str.m_str);m_str=new char[m_size+1];strcpy(m_str,str.m_str);}return *this; } CString& CString::operator =(const char *str) {delete[] m_str;m_size=strlen(str);m_str=new char[m_size+1];strcpy(m_str,str);return *this; } int main() {CString s1="hello",s2("world"),s3;s3=s1+s2;cout<<"s3 = s1+s2 -- "<<s3.m_str<<endl;s3=s3+"abc"; //s3="abc"+s1; //會出現什么問題?? =>會報錯:no match for 'operator+' in '"abc" + s1'cout<<"s3:"<<s3.m_str<<endl;return 0; }

執行結果:

s3 = s1+s2 -- helloworld s3:helloworldabc

重載運算符‘[ ]’

實例

#include <iostream> #include <string.h> #include <windows.h> using namespace std;class CString { public:CString(const char *s="");CString(const CString& s);char operator [](int index);int GetSize();char *m_str; private:int m_size; }; CString::CString(const CString& s) {m_size=strlen(s.m_str);m_str=new char[m_size+1];strcpy(m_str,s.m_str); } CString::CString(const char *s/* ="" */) {m_size=strlen(s);m_str=new char[m_size+1];strcpy(m_str,s); } inline int CString::GetSize() {return m_size; } char CString:: operator [](int index) {if(index<0 || index>=m_size){//下標越界}return m_str[index]; } int main() {CString entry("extravagant");for(int i = 0;i<entry.GetSize();++i){cout<<"entry = "<<entry[i]<<endl;}return 0; }

執行結果:

entry = e entry = x entry = t entry = r entry = a entry = v entry = a entry = g entry = a entry = n entry = t 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的复习笔记(六)——C++运算符重载(难点)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。