嘀嘀咕(3)
目錄
- 1.this
- 2.友元
- 2.1:友元函數(shù)
- 2.2:友元類
- 3:操作符重載
- 3.1:重載+=
- 3.2:重載++
- 3.3:重載<<、>>
- 3.4:練習
1.this
class A {public:int get( ) const //成員函數(shù)尾部出現(xiàn) const,修飾的是this指針{this->a = 100; //err} A &add(int b) //返回對象本身{this->a += b;return *this;}private:int a; }2.友元
2.1:友元函數(shù)
可以直接訪問類的私有成員的非成員函數(shù)。 它是定義在類外的普通函數(shù),它不屬于任何類,但需要在類的定義中加以聲明,聲明時只需在友元的名稱前加上 關(guān)鍵字 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:友元類
友元類的所有成員函數(shù)都是另一個類的友元函數(shù),都可以訪問另一個類中的隱藏信息(包括私有成員和保護成員) class A {public:friend class B; //類 B 的所有成員函數(shù)都是類 A 的友元函數(shù),能存取類 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;//實數(shù)int b;//虛數(shù) };//方式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;//實數(shù)int b;//虛數(shù) };#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; } #endif3.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);//<<操作符只能寫在全局,不能夠?qū)懺诔蓡T方法中。否則調(diào)用的順序會變,c1<<cout; #if 0ostream& operator<<(ostream &os) //c1.operator<<(cout){os << "( " << this->a << ", " << this->b << "i )";return os;} #endifprivate:int a;//實數(shù)int b;//虛數(shù) };#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; } #endif3.4:練習
//練習操作符重載 自定義數(shù)組,實現(xiàn)<<操作符 實現(xiàn)取值操作符[]array[i] = 10;cout <<array<<endl;cin>>array;cout <<array[i] <<endl;==!=array1 == array2array1 != array2轉(zhuǎn)載于:https://www.cnblogs.com/EngineerZhang/p/9851927.html
總結(jié)
- 上一篇: 杭电acm 2177 取(2堆)石子游戏
- 下一篇: 阿里云消息队列