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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

C++基础11-类和对象之操作符重载1

發(fā)布時間:2025/3/15 c/c++ 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++基础11-类和对象之操作符重载1 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

總結(jié):

1、運算符重載的本質(zhì)是函數(shù)重載
2、運算符重載可分為成員函數(shù)重載全局函數(shù)重載(差一個參數(shù))
3、運算符重載函數(shù)的參數(shù)至少有一個是類對象(或類對象的引用)
4、不可以被重載的操作符有:成員選擇符(.) 成員對象選擇符(.*)?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?域解析操作符(::) 條件操作符(?:)
5、前置加加 可以連續(xù)使用 后置加加 不可以連續(xù)使用
6、前置加加(減減)的運算符重載和普通運算符重載一樣后置加加(減減)的運算符重載需要加一個占位符
7、輸入輸出運算符重載定義為全局函數(shù)重載。因為成員函數(shù)重載會發(fā)生歧義
8、輸入輸出運算符重載借助輸入輸出流(ostream istream)
9、雙目運算符就是有兩個參數(shù)(+,-),單目運算符有一個參數(shù)(++,--)
10、重載的前置加加 Complex& operator++(Complex &c1) ?//返回引用的目的是使用連續(xù)前置加加
? ? ? ?重載的后置加加 const Complex& operator++(Complex &c1, int)?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //加const的原因是后置加加不能連續(xù)使用
11、右移操作符重載:friend ostream& operator<<(ostream &os, Complex &c);
?? ??? ??? ??? ??? ?ostream& operator<<(ostream &os) ?//c1.operator<<(cout)
?? ??? ??? ??? ??? ?{?
?? ??? ??? ??? ??? ??? ?os << "(" << this->a << "," << this->b << "i)";
?? ??? ??? ??? ??? ??? ?return os;
?? ??? ??? ??? ??? ?}
? ? ?左移操作符重載:friend istream& operator>>(istream &is, Complex &c);
?? ??? ??? ??? ??? ?istream& operator>>(istream &is, Complex &c)
?? ??? ??? ??? ??? ?{
?? ??? ??? ??? ??? ??? ?cout << "a:";
?? ??? ??? ??? ??? ??? ?is >> c.a;
?? ??? ??? ??? ??? ??? ?cout << "b:";
?? ??? ??? ??? ??? ??? ?is >> c.b;
?? ??? ??? ??? ??? ??? ?return is;
?? ??? ??? ??? ??? ?}
12、若某函數(shù)中類想引用私有成員,則將此函數(shù)在這個類中聲明為友元函數(shù)

13、除了賦值號(=)外,基類中被重載的操作符都將被派生類繼承

1、操作符重載格式

重載函數(shù)的一般格式如下:

函數(shù)類型 operator 運算符名稱(形參表列) {

? ? ? ? ?重載實體;

}

operator 運算符名稱 在一起構(gòu)成了新的函數(shù)名。比如

const Complex operator+(const Complex &c1,const Complex &c2);

我們會說,operator+ 重載了重載了運算符+。

2、重載規(guī)則

?

4.10.2 重載規(guī)則

(1)C++不允許用戶自己定義新的運算符,只能對已有的 C++運算符進行重載。

例如,有人覺得 BASIC 中用“* *”作為冪運算符很方便,也想在 C++中將“**”定義為冪運算符,用“3**5”表示 35,這是不行的。

(2)C++允許重載的運算符

C++中絕大部分運算符都是可以被重載的。

不能重載的運算符只有 4 個:成員選擇符(.) 成員對象選擇符(.*)???域解析操作符(::) 條件操作符(?:)

前兩個運算符不能重載是為了保證訪問成員的功能不能被改變,域運算符合sizeof 運算符的運算對象是類型而不是變量或一般表達式,不具備重載的特征。

?

3)重載不能改變運算符運算對象(即操作數(shù))的個數(shù)。

如,關(guān)系運算符“>”和“<”等是雙目運算符,重載后仍為雙目運算符,需要兩 個參數(shù)。運算符”+“,”-“,”*“,”&“等既可以作為單目運算符,也可以作為雙 目運算符,可以分別將它們重載為單目運算符或雙目運算符。

4)重載不能改變運算符的優(yōu)先級別。

例如”*“和”/“優(yōu)先級高于”+“和”-“,不論怎樣進行重載,各運算符之間的優(yōu)先級不會改變。有時在程序中希望改變某運算符的優(yōu)先級,也只能使用加括號的方法 強制改變重載運算符的運算順序。

(5)重載不能改變運算符的結(jié)合性。

如,復(fù)制運算符”=“是右結(jié)合性(自右至左),重載后仍為右結(jié)合性。

(6)重載運算符的函數(shù)不能有默認的參數(shù)

否則就改變了運算符參數(shù)的個數(shù),與前面第(3)點矛盾。

(7)重載的運算符必須和用戶定義的自定義類型的對象一起使用,其參數(shù)至少應(yīng)有 一 個是類對象(或類對象的引用)。

也就是說,參數(shù)不能全部是 C++的標準類型,以防止用戶修改用于標準類型數(shù)據(jù)成 員的運算符的性質(zhì),

(8)用于類對象的運算符一般必須重載,但有兩個例外,運算符”=“和運算 符”&“不 必用戶重載。

復(fù)制運算符”=“可以用于每一個類對象,可以用它在同類對象之間相互賦值。因為系統(tǒng)已為每一個新聲明的類重載了一個賦值運算符,它的作用是逐個復(fù)制類中的 數(shù)據(jù)成員地址運算符&也不必重載,它能返回類對象在內(nèi)存中的起始地址。

(9)應(yīng)當使重載運算符的功能類似于該運算符作用于標準類型數(shù)據(jù)時候時所實現(xiàn)的功能。

例如,我們會去重載”+“以實現(xiàn)對象的相加,而不會去重載”+“以實現(xiàn)對象相 減的功能,因為這樣不符合我們對”+“原來的認知。

(10)運算符重載函數(shù)可以是類的成員函數(shù),也可以是類的友元函數(shù),還可以是既非類的成員函數(shù)也不是友元函數(shù)的普通函數(shù)

操作符重載

#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class Complex { public:Complex(int a, int b){this->a = a;this->b = b;}void printComplex(){cout << "( " << this->a << ", " << this->b << "i )" << endl;}friend Complex complexAdd(Complex &c1, Complex &c2);/*friend Complex operator+(Complex &c1, Complex &c2);friend Complex operator-(Complex &c1, Complex &c2);*/Complex complexAdd(Complex &another){Complex temp(this->a + another.a, this->b + another.b);return temp;}//2操作符重載寫在局部Complex operator+(Complex &another){Complex temp(this->a + another.a, this->b + another.b);return temp;}Complex operator-(Complex &another){Complex temp(this->a - another.a, this->b - another.b);return temp;}private:int a;//實數(shù)int b;//虛數(shù) };Complex complexAdd(Complex &c1, Complex &c2) {Complex temp(c1.a + c2.a, c1.b + c2.b); //要訪問需要聲明為友元函數(shù)return temp; } void test01() {Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();Complex c3 = complexAdd(c1, c2);c3.printComplex(); }void test02() {Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();Complex c3 = c1.complexAdd(c2);c3.printComplex(); } //1操作符重載寫在全局 #if 0 Complex operator+(Complex &c1, Complex &c2) //+ 相當于函數(shù)名,接收兩個參數(shù)//+ 左邊是 第一個參數(shù)//+ 右邊是 第二個參數(shù) 順序固定 {Complex temp(c1.a + c2.a, c1.b + c2.b);return temp; }Complex operator-(Complex &c1, Complex &c2) {Complex temp(c1.a - c2.a, c1.b - c2.b);return temp; }#endif //全部操作符重載調(diào)用 void test03() {Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();//Complex c3 = operator+(c1, c2); //ok//等價于Complex c3 = c1 + c2; //此種為隱式寫法// c1 + c2 等價于 operator+(c1,c2) 全局的調(diào)用方式// c1 + c2 也等價于 c1.operator+(c2) 局部調(diào)用方式//所以局部調(diào)用方式和全局調(diào)用方式只能存在一個c3.printComplex(); } void test04() {Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();//Complex c3=c1.operator+(c2);//等價于Complex c3 = c1 + c2;c3.printComplex();Complex c4 = c1 - c2;c4.printComplex();Complex c5 = c1-c2-c1; //當 operator-函數(shù) 返回為元素時ok //c1-c2返回為匿名對象 //再調(diào)用函數(shù) 還為匿名對象 拷貝構(gòu)造給c5//當 operator-函數(shù)返回引用時 錯誤 //c1-c2返回為為temp的別名//再調(diào)用函數(shù)時 temp已經(jīng)析構(gòu) c1-c2變?yōu)閬y碼 再調(diào)用函數(shù)仍然為亂碼c5.printComplex(); } int main(void) {cout << "------test01-----------" << endl;test01();cout << "------test02-----------" << endl;test02();cout << "------test03-----------" << endl;test03();cout << "------test04-----------" << endl;test04();return 0; } #endif

雙目運算符(有兩個參數(shù)):

#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class Complex { public:/*friend Complex& operator+=(Complex &c1, Complex &c2);friend Complex& operator-=(Complex &c1, Complex &c2);*/Complex(int a, int b){this->a = a;this->b = b;}void printComplex(){cout << "( " << this->a << ", " << this->b << "i )" << endl;}//2操作符重載寫在局部Complex& operator+=(Complex &another){this->a += another.a;this->b += another.b;return *this;}Complex& operator-=(Complex &another){this->a -= another.a;this->b -= another.b;return *this;}private:int a;//實數(shù)int b;//虛數(shù) };//1操作符重載寫在全局 #if 0 Complex& operator+=(Complex &c1, Complex &c2) //+ 相當于函數(shù)名,接收兩個參數(shù)//+ 左邊是 第一個參數(shù)//+ 右邊是 第二個參數(shù) 順序固定 {c1.a += c2.a;c1.b += c2.b;return c1; }Complex& operator-=(Complex &c1, Complex &c2) {c1.a -= c2.a;c1.b -= c2.b;return c1; }#endif void test01() {Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();(c1 += c2)+=c2; //(c1.operator+=(c2)).operator(c2)c1.printComplex();c2.printComplex(); } /* ( 1, 2i ) ( 2, 4i ) ( 5, 10i ) ( 2, 4i ) */ //全部操作符重載調(diào)用int main(void) {cout << "------test01-----------" << endl;test01();/*cout << "------test02-----------" << endl;test02();cout << "------test03-----------" << endl;test03();cout << "------test04-----------" << endl;test04();*/return 0; } #endif

單目運算符:

#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class Complex { public:/*friend Complex& operator++(Complex &c1);friend Complex& operator--(Complex &c12);friend const Complex& operator++(Complex &c1, int);*/Complex(int a, int b){this->a = a;this->b = b;}void printComplex(){cout << "( " << this->a << ", " << this->b << "i )" << endl;}//2操作符重載寫在局部Complex& operator++(){this->a ++;this->b ++;return *this;}Complex& operator--(){this->a --;this->b --;return *this;}const Complex& operator++(int) { //亞元Complex temp(this->a, this->b);this->a++;this->b++;return *this;}private:int a;//實數(shù)int b;//虛數(shù) };//1操作符重載寫在全局 #if 0 //重載的前置加加 Complex& operator++(Complex &c1) //+ 相當于函數(shù)名,接收兩個參數(shù)//+ 左邊是 第一個參數(shù)//+ 右邊是 第二個參數(shù) 順序固定 {c1.a ++;c1.b ++;return c1; }Complex& operator--(Complex &c1) {c1.a--;c1.b--;return c1; }#endif //重載的后置加加 不可以用多次 所以用const限制 #if 0 const Complex& operator++(Complex &c1, int) { Complex temp(c1.a, c1.b);c1.a++;c1.b++;return c1; } #endif void test01() {Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();++c1;c1.printComplex();/*++c1;c1.printComplex();*/} /* ( 1, 2i ) ( 2, 4i ) ( 2, 3i ) ( 3, 4i ) */ void test02() {int a = 0;//a++++; 編譯錯誤 后置加加不可多次Complex c1(1, 2);Complex c2(2, 4);c1.printComplex();c2.printComplex();c1++; //后置加加返回值添加const 使得c1++++ 編譯錯誤c1.printComplex(); } /* ( 1, 2i ) ( 2, 4i ) ( 2, 3i ) */ int main(void) {cout << "------test01-----------" << endl;test01();cout << "------test02-----------" << endl;test02();/*cout << "------test03-----------" << endl;test03();cout << "------test04-----------" << endl;test04();*/return 0; } #endif

左移右移操作符:

#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>using namespace std;class Complex { public:friend ostream& operator<<(ostream &os, Complex &c);friend istream& operator>>(istream &is, Complex &c);Complex(int a, int b){this->a = a;this->b = b;}void printComplex(){cout << "( " << this->a << ", " << this->b << "i )" << endl;} #if 0//左移操作符只能不能夠?qū)懺诔蓡T函數(shù)里 只能寫在全局函數(shù)中 否則調(diào)用順序會變反 c1<<cout;ostream& operator<<(ostream &os) { //c1.operator<<(cout)os << "(" << this->a << "," << this->b << "i)";return os;} #endif//2操作符重載寫在局部private:int a;//實數(shù)int b;//虛數(shù) };//1操作符重載寫在全局 #if 1 //重載的前置加加 ostream& operator<<(ostream &os, Complex &c) {os << "(" << c.a << "," << c.b << "i)";return os; } istream& operator>>(istream &is, Complex &c) {cout << "a:";is >> c.a;cout << "b:";is >> c.b;return is; } #endifvoid test01() {Complex c1(1, 2);Complex c2(2, 4);cout << c1 << endl;cin >> c2;c1.printComplex();c2.printComplex();//cout << c1; //operator<<(cout,c1) 全局ok 局部不ok//c1 << cout; ok} /**/int main(void) {cout << "------test01-----------" << endl;test01();/*cout << "------test02-----------" << endl;test02();cout << "------test03-----------" << endl;test03();cout << "------test04-----------" << endl;test04();*/return 0; } #endif

總結(jié)

以上是生活随笔為你收集整理的C++基础11-类和对象之操作符重载1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。