運算子過載,友元函式

2021-08-13 19:32:55 字數 1390 閱讀 2708

運算子過載

#include

using namespace std;

class cint

; cint(const cint &timp)

public:

cint operator+(const cint &timp)const //cint add(const cint &timp)const

cint add(const cint &timp)const

cint operator+(const

int d) const

/**public:

cint operator*(const cint &timp);

cint operator+=(const cint &timp);

cint operator&(const cint &timp);

cint operator>(const cint &timp);

cint operator%(const cint &timp);

//等等。。。。。可以過載各種符號;

//但是對 ? . .* :: sizeof 這五個運算子不能過載

**/public:

cint& operator++()//返回this所以可以引用返回

cint operator++(int )//不能理解,就當做規定吧,區分前加加,和後加加

};ostream& operator

<<(ostream& out,const cint & timp)

int main()

友元函式

#include using namespace std;

/**成員函式

1.可以訪問物件的私有部分;

2.函式必須位於類裡面宣告,作用域必須在類裡面

3.呼叫函式時,必須由物件去呼叫

友元函式

1.可以訪問物件的私有部分;

2.作用域取決於函式自己;

3.不需要該物件呼叫;

其實就是乙個正常的函式,他不屬於類a,但是只要宣告為類a的友元函式,他就可以直接訪問類a的私有成員,其他的就和該函式的原來的性質一樣的;

**/class cbool

};class cint

cint(const cint & timp)

public:

ostream &operator<<(ostream &out )//如果我們把《過載為成員函式

};void show (const cint & timp)

cint operator+(const

int &b,const cint &timp)

int main()

友元運算子過載

運算子就是一種特殊的函式 運算子過載可以是普通函式 const money operator const money a1,const money a2 可以是成員函式 const money operator const money a2 const 當二元運算子作為成員函式進行過載時,只有乙個引...

8 1 2 運算子過載(友元函式)

程式頭部注釋開始 程式的版權和版本宣告部分 檔名稱 實現複數類中的運算子過載 作 者 張傳新 完成日期 2012 年04 月07 日 版 本 號 1.0 對任務及求解方法的描述部分 輸入描述 問題描述 定義乙個複數類過載運算子 使之能用於複數的加減乘除。程式輸出 程式頭部的注釋結束 includeu...

運算子過載為友元函式

運算子過載為友元函式 郭 煒 劉家瑛 frend function.cpp 定義控制台應用程式的入口點。include stdafx.h include using namespace std class complexs complexs void print friend complexs op...