#ifndef __mycomplex__ //1.防衛式的宣告
#define __mycomplex__
class complex;
complex&
__doapl (complex* ths, const complex& r);
complex&
__doami (complex* ths, const complex& r);
complex&
__doaml (complex* ths, const complex& r);
/*2. 布局:先申明後定義
3. 利用模板,不把資料型別寫死,泛化程式設計
4. 函式的申明與定義,在類內定義的是inline函式
5. 類的訪問級別:一般來說:data是private,函式如果外界使用是public,自己使用是private
*/class complex
complex& operator += (const complex&);
complex& operator -= (const complex&);
complex& operator *= (const complex&);
complex& operator /= (const complex&);
//8. 常量成員函式:不改變資料內容,就要加const
double real () const
double imag () const
private:
double re, im;
friend complex& __doapl (complex *, const complex&);
friend complex& __doami (complex *, const complex&);
friend complex& __doaml (comp lex *, const complex&);
};//9.引數的傳遞盡量都傳引用,pass by value 和 pass by reference
//11.操作符過載,a.操作符先網左邊找 b.
inline complex&/*a.傳遞者不需要指定接收者以reference形式接收, b.必須是reference返回,c3+=c2+=c1*/
__doapl (complex* ths, const complex& r)
,inline complex&
complex::operator += (const complex& r)
inline complex&
__doami (complex* ths, const complex& r)
inline complex&
complex::operator -= (const complex& r)
inline complex&
__doaml (complex* ths, const complex& r)
inline complex&
complex::operator *= (const complex& r)
//12.全域性函式,非成員函式
inline double
imag (const complex& x)
inline double
real (const complex& x)
inline complex
operator + (const complex& x, const complex& y)
inline complex
operator + (const complex& x, double y)
inline complex
operator + (double x, const complex& y)
inline complex
operator - (const complex& x, const complex& y)
inline complex
operator - (const complex& x, double y)
inline complex
operator - (double x, const complex& y)
inline complex
operator * (const complex& x, const complex& y)
inline complex
operator * (const complex& x, double y)
inline complex
operator * (double x, const complex& y)
complex
operator / (const complex& x, double y)
inline complex
operator + (const complex& x)
//14. 返回的是value, operator+ 返回的是reference
inline complex
operator - (const complex& x)
inline bool
operator == (const complex& x, const complex& y)
inline bool
operator == (const complex& x, double y)
inline bool
operator == (double x, const complex& y)
inline bool
operator != (const complex& x, const complex& y)
inline bool
operator != (const complex& x, double y)
inline bool
operator != (double x, const complex& y)
#include inline complex
polar (double r, double t)
inline complex
conj (const complex& x)
inline double
norm (const complex& x)
#endif //__mycomplex__
#include #include "complex.h"
using namespace std;
//15. 只能是非成員函式,因為操作符作用在左邊身上;
ostream&
operator << (ostream& os, const complex& x)
int main()
c 類的指標
1,類指標呼叫成員函式,根據的不是指標所指向物件而是根據指標所屬的型別,呼叫相關的函式。此程式指標p照樣能呼叫類a的函式print.2,指向基類物件的指標不僅可以包含基類物件的位址,還可以包含派生類物件的位址。3,類的物件和類的指標的區別 http blog.csdn.net wang7890 ar...
C 類中的this指標
1.this指標的用處 乙個物件的this指標並不是物件本身的一部分,不會影響sizeof 物件 的結果。this作用域是在類內部,當在類的非靜態成員函式中訪問類的非靜態成員的時候,編譯器會自動將物件本身的位址作為乙個隱含引數傳遞給函式。也就是說,即使你沒有寫上this指標,編譯器在編譯的時候也是加...
C 的類和This指標
類 一.類的定義 類的定義 class classname 一定要注意後面的分號類的定義通常有兩種方式 a.類的宣告和定義全部放在類體中。宣告和定義放在一起 class ctest ctest b.類的宣告放在.h檔案中,類的定義放在放在.cpp檔案中。test.h類的宣告 class ctest ...