C 語法總結

2021-04-13 13:46:08 字數 4751 閱讀 6770

1 const 與volatile 的用法

1 const

#include

#include

//行引數指向const 型別變數的指標

void display_c(cons int * pi)

6 new 與delete 運算子

double * pd;   // define pointer variable

pd=new double; // allocate memory

if(pd!=null)

double1=null

* pds=new double[100];

if(pds)

void main()

9 typeid 運算子 用來求得變數或隊形愛女嘎的資料型別,返回乙個type_info 型別的物件,通過該物件

可以獲取資料型別的名稱

include

int i;

const type_info &t0=typeid(i);

t0.name();

10 函式

1 模板函式

templatet min(r &x,tv&y)

;book:book(char *pbn)

void book::setbookname(char * pbn)

int book::getbookname(char * pbn,unsigned intmaxsize)

}// 使用

book b1;

b1.setbookname("test");

book b2(test1);

2 物件的引用引數傳遞

void add(book b)

void addref(book & b);

3 靜態成員變數 是乙個公共變數

在初始化 的時候利用作用運算子:: 對私有型別的靜態成員變數可以向公有型別的靜態成變數一樣賦值

但不能直接引用

3 const 型別成員函式與mutable

class cdate

;int betmonth() const ;//read only

void setmonth(int m);// write only

}void cdate::setmonth(int m)

void main()

在const 型別的成員函式定義中,不可一直接或簡介的修改普通型別的成員變數

如果象修改const 型別物件的成員函式,可以使用關鍵字mutable 對該成員變數進行修改

5 物件的初始化與初始化行

將引數類表中的數值賦予成員變數時,不僅可以在建構函式的函式體中進行,以阿寬衣在初始化行中進行

在初始化處驚醒初始化的情況有:

1 分層類的構在函式可以呼叫它的任何乙個子物件的建構函式

2 對常量const 型別的成員變數的初始化必須在初始化行上

3 對於引用型別的成員變數的初始化必須在初始化行上

class cpoint

};class crect

};void main()

6 拷貝建構函式

拷貝建構函式與普通建構函式的差別在與棋引數類表,引數列表中有乙個物件,該物件的資料型別是

本類的乙個引用,而且一般情況下,該物件還應該是乙個const 型別的物件。

如果在類的定義是不是顯示的定義乙個拷貝函式,則編譯器會自動的定義乙個拷貝建構函式

class cpoint

;cpoint::cpoint(int m_x,int m_y)

cpoint::cpoint(const cpoint &c)

void main()

7 template class

templateclass array // template class

;arrayarray_i1(i1,i0);

}1 友員類和友員函式

#include

#include

#include

class softwareengineer; //先對softwareengineer 類進行顯示說明一下

class computer // 定義computer 類

;friend class softwareengineer; //將友員類載這裡宣告

frined void judge(computer &c,softwareengineer  & s) //友員函式

};class softwareengineer

int getcomputerprice(computer &c) //訪問computer 的思友變數

friend void judge(computer &c,softwareengineer & s) //友員函式

};//判斷乙個軟體工程師是否可以用乙個月的薪水買的器一台電腦

void judge(computer &c,softwareengineer &s) //橋友員函式

2運算子過載

#include

#include

#include

class tvalue

//成員函式過載運算子

tvalue operator!();

tvalue operator+(tvalue & rv);

// 友員函式過載運算子

friend ostream & operator<<;

}tvalue tvalue::operator!()

tvalue tvalue:operator+(tvalue& rv)

ostream & operator<<(ostream & os,tvalue rv)

3 類的派生和繼承

1class box;

void setwidth(int h);

};class tcolorbox::public box;

};void main()

2 不能被繼承的成員

建構函式,析構函式,使用者定義的新操作符,使用者定義的賦值操作,友員關係

3 建構函式,析構函式的呼叫順序

class a

};class b}例項

class currency

currency(double d)

currency(currency &rc)

currency & operator=

currency & operator+(currency & c)

}//人民幣

class rmb:public currency

;//呼叫派生類的建構函式前,需要呼叫器基類的工造函式

rmb(double d):currency(d){};

rmb(currency& c):currency(c){};

rmb(rmb& rmb):currency(rnb){};

friend ostream& operator<<;//派生類的附加功能

};ostream& operator<< //output 運算子不能是乙個類的成員函式

triangular tris;

cin>>tri2

4 虛基類

載繼承關係中,同一基類被繼承多次,不僅會引器歧異,而起可能浪費空間

class a

;class b:public virtual a(); //虛基類 編譯器只產生乙個基類版本。如果不定義為virtual 編譯器不知到呼叫那個value 了,當然

class c:public virtual a();//  也可以return b::value;

class d:public b.public c

};void main()

5 多型形,和虛函式

class tfirst

;void tfirst::display()

class tsecond:public tfirst

;void tsecond::display()

void display(trist * pfirst)

void main()

c++ builder 中的集合的

1 集合的概念基本

setsets1

tydefef set(char,1,255) uppercaset;

uppercaseset s1;

s1<<'a'<<'b'<<'c';

sysset.h 直接包含載vcl.h 中

2 集合的操作

#include

#include

#include

using namespace std;

typedef setupperset;

typedef setloverset;

typeder sethalflowerset;

void set_example()

異常處理

1 c++ 的異常處理

#include

#include

class crange

}class cstring

char & operator(int i)

}catch(int ie)

catch(exception &e)

不能引發如int double 等簡單型別的異常

3) c++ 異常處理

trycatch(int errorcode)

}4 單一捕捉異常

trycatch(edivbyzero & e)

5 捕捉所有異常

trycatch(...) or(eexternalexception &e)

C 語法總結

1 const 與volatile 的用法 1 const include include 行引數指向const 型別變數的指標 void display c cons int pi 6 new 與delete 運算子 double pd define pointer variable pd new...

C 語法總結

語法 例子 類的通式 class class nameobject list class vehicle int vehicle range 建構函式 class name class myclass myclass myclass myclass myclass 拷貝建構函式 classname ...

總結整理 C 基礎語法

輸入輸出 cin n 等同於 scanf d n cout n 等同於 printf d n cout hello world endl 等同於 printf hello world n string類 string s hello world 賦值字串 string s2 s string s3 ...