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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

嘀嘀咕(3)

發布時間:2024/1/17 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 嘀嘀咕(3) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 1.this
  • 2.友元
    • 2.1:友元函數
    • 2.2:友元類
  • 3:操作符重載
    • 3.1:重載+=
    • 3.2:重載++
    • 3.3:重載<<、>>
    • 3.4:練習

1.this

class A {public:int get( ) const //成員函數尾部出現 const,修飾的是this指針{this->a = 100; //err} A &add(int b) //返回對象本身{this->a += b;return *this;}private:int a; }

2.友元

2.1:友元函數

可以直接訪問類的私有成員的非成員函數。 它是定義在類外的普通函數,它不屬于任何類,但需要在類的定義中加以聲明,聲明時只需在友元的名稱前加上 關鍵字 friend。 class Point {public:Point(double xx, double yy){x = xx;y = yy;}void Getxy();friend double Distance(Point &a, Point &b);private:double x, y; };double Distance(Point &a, Point &b) {double dx = a.x - b.x;double dy = a.y - b.y;return sqrt(dx*dx + dy*dy); }

2.2:友元類

友元類的所有成員函數都是另一個類的友元函數,都可以訪問另一個類中的隱藏信息(包括私有成員和保護成員) class A {public:friend class B; //類 B 的所有成員函數都是類 A 的友元函數,能存取類 A的私有成員和保護成員};

3:操作符重載

Complex operator+(Complex &a, Complex &b) {Complex tmp(a.x+b.x, a.y+b.y);return tmp; }

3.1:重載+=

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 & operator+=(Complex &c1, Complex &c2);//方式2Complex &operator+=(Complex &another){this->a += another.a;this->b += another.b;return *this;} private:int a;//實數int b;//虛數 };//方式1:全局 #if 0 Complex & operator+=(Complex &c1, Complex &c2) {c1.a += c2.a;c1.b += c2.b;return c1; } #endif//(a += b) += c; a.operator+=(b).operator+=(c);

3.2:重載++

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 & operator++(Complex &c);//friend const Complex operator++(Complex &c1, int);Complex &operator++(){this->a++;this->b++;return *this;}const Complex operator++(int)//亞元{Complex temp(this->a, this->b);this->a++;this->b++;return temp;}private:int a;//實數int b;//虛數 };#if 0 //重載的是前++運算符 Complex & operator++(Complex &c) {c.a++;c.b++;return c; } #endif//重載的是后++運算符 #if 0 const Complex operator++(Complex &c1, int) {Complex temp(c1.a, c1.b);c1.a++;c1.b++;return temp; } #endif

3.3:重載<<、>>

class Complex { public:Complex(int a, int b){this->a = a;this->b = b;}void printComplex(){cout << "( " << this->a << ", " << this->b << "i )" << endl;}friend ostream& operator<<(ostream & os, Complex &c);friend istream & operator>>(istream &is, Complex &c);//<<操作符只能寫在全局,不能夠寫在成員方法中。否則調用的順序會變,c1<<cout; #if 0ostream& operator<<(ostream &os) //c1.operator<<(cout){os << "( " << this->a << ", " << this->b << "i )";return os;} #endifprivate:int a;//實數int b;//虛數 };#if 1 ostream& operator<<(ostream & os, Complex &c) //cout<<c; ===>第一個操作符類型ostream,第二個操作符類型Complex {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; } #endif

3.4:練習

//練習操作符重載 自定義數組,實現<<操作符 實現取值操作符[]array[i] = 10;cout <<array<<endl;cin>>array;cout <<array[i] <<endl;==!=array1 == array2array1 != array2

轉載于:https://www.cnblogs.com/EngineerZhang/p/9851927.html

總結

以上是生活随笔為你收集整理的嘀嘀咕(3)的全部內容,希望文章能夠幫你解決所遇到的問題。

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