freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

c要素類和對象模版類的繼承(已修改)

2024-10-28 16:16 本頁面
 

【正文】 ? C++要素 ? 類和對象 ? 模版 ? 類的繼承 C復(fù)習(xí) 數(shù)據(jù)類型 :在 C語言中 :基本類型和構(gòu)造類型 基本類型:整型、浮點型、字符型、 雙精度型 構(gòu)造類型:數(shù)組、結(jié)構(gòu)、聯(lián)合、指針、枚舉型 數(shù)據(jù)對象 : int n=3。 int a[3]。 a[0]=1。 //整型數(shù)據(jù)對象 char ch=‘A’。//字符類型數(shù)據(jù)對象 對象指針 : int *p=amp。n, *q=a。 C復(fù)習(xí) 標(biāo)識符 表達式 語句:賦值句、條件句、循環(huán)句、函數(shù)調(diào)用(輸入輸出) 函數(shù):返回值類型,函數(shù)名,參數(shù)列表 遞歸:直接遞歸、間接遞歸 C++介紹 ? 一 . C++要素 (Borland C , VC) *.cpp 注意: C++ 源文件擴展名用 .cpp C 源文件擴展名用 .c 1. 文件擴展名 /* ……… */ 段注釋 以 /* 開始 到 */ 結(jié)束 // 行注釋 到行末 例 1: const int m=10。 //定義 m為常量 值 是 10 4. 常量說明 const 例 2: const int a[ ]={1,3,5,7,9}。 // 定義 a是常量數(shù)組 例 3. int * const p。 //常指針 p指向一個固定的地址 例 4. const int * q。 //指針 q 指向常量 可以在任何地方申明一個變量 例 for(int i=0。 i5。i++) 其作用域從申明地起到文末或函數(shù)末 5. 變量申明 但不能 while(int i) i++。 int f(int a, int b=0)。 調(diào)用 f(5), 即調(diào)用 f(5,0)。 缺省參數(shù) 要寫在參數(shù)表的右邊 int f(int a, int b=0,int c=1)。 inline int square(int x) {return x*x。} 內(nèi)聯(lián)函數(shù)先編譯,效率高,速度快 但只能有四五個語句, 不能有循環(huán)語句,條件語句 . overload abs。 int abs(int)。 float abs(float)。 同名 不同 參數(shù) 的函數(shù)可以重載 系統(tǒng)會自動選擇調(diào)用 8. 函數(shù)重載 定義函數(shù)的變量參數(shù) 例 int f(int amp。 x) { int n=2*x++。 return n。} x 為函數(shù) f 的變量參數(shù) 調(diào)用實參的地址 調(diào)用后實參的值可以改變 函數(shù)需要兩個以上返回值時,用變量參數(shù) 9. 引用操作符 amp。 例 int *p,*q。 p = new int (3)。 q = new int [4]。 為 p分配一個整形地址( 2字節(jié) ) *p==3 為 q 分配 4個整形地址( 8個連續(xù)字節(jié)) delete p。 //撤銷 p的地址 delete [ ] q。 //撤銷 q的多個連續(xù)地址 10. 動態(tài)函數(shù) 動態(tài)變量 new delete include “” int a,b。 char x,y。 cout“ Enter x,y,a,b”endl。 cinxy。 \\從鍵盤為變量 x,y輸入數(shù)據(jù) cinab。 \\從鍵盤為變量 a,b輸入數(shù)據(jù) cout“ x=” xendl。 cout yabendl。 文件輸入輸出 include “” include “” void main( ) { ifstream infile(“”)。 if(!infile) { cerr“Cannot open ”endl。 exit(1)。} ofstream outfile(“”)。 if(!outfile) { cerr“Cannot open ”endl。 exit(1)。} int n。 string name。 while(infilen) { infilename。 outfilen“ ”nameendl。 } } ?一 . C++要素 (Borland C ) 1. 文件擴展名 *.cpp /* ……… */ 段注釋 // 行注釋 到行末 3. 續(xù)行 \ 4常量說明 const 5. 變量申明 可以在任何地方申明一個變量 int f(int a, int b=0)。 8. 重載 overload amp。 10. 動態(tài)函數(shù) 動態(tài)變量 new Delete 11輸入輸出 二 . 類和對象 1. 類的定義 class 類名稱 { private: 數(shù)據(jù)成員; 成員函數(shù); protected: 數(shù)據(jù)成員; 成員函數(shù); public: 數(shù)據(jù)成員; 成員函數(shù); }。 //類定義結(jié)束 必須有分號 “ ; ” ?class 是保留字 , 作用與 struct 相同 定義一個結(jié)構(gòu)也叫類 。 ?private( 私有 ) , 缺省 ?protected( 保護 ) , ? public( 公有 ) 都是訪問限制 例 計數(shù)器類 存儲于文件“ ”中 class counter { private: //私有成員 unsigned int value。 //數(shù)據(jù)成員 public: //公有成員 counter( ) { value=0。} //無參構(gòu)造函數(shù) counter(int x){ if(x0)value=x。 else value=0。}//有參構(gòu)造函數(shù) void increment( ){if(value65535)value++。} void decrement( ){if(value0)value。} unsigned access_value( ){return value。} }。 counter c1, c2。 //語句 1 counter c3(5)。 //語句 2 語句 1定義 counter 類的對象 c1, c2,即實際變量 (實例 )。 對象定義時必須為數(shù)據(jù)成員賦初值即初始化 。初始化由類中的構(gòu)造函數(shù)自動完成 。 語句 1 自動調(diào)用 counter 類中無參構(gòu)造函數(shù) , 使 ==0。 語句 2定義對象 c3, 自動調(diào)用有參構(gòu)造函數(shù)使=5. 2. 對象的定義 object 注意:不能使用 。 因為 value 在 counter類中是私有成員不可見,只能用成員函數(shù)來調(diào)用。 對象不能直接調(diào)用私有成員,只能通過公有成員函數(shù)來調(diào)用私有成員 對象調(diào)用成員函數(shù),叫發(fā)一個消息 為 c1發(fā)消息: ( )。 //計數(shù)器自動加 1 ( )。 // 計數(shù)器自動減 1 例 計數(shù)器測試程序 include “” include “” void main( ) { counter c1,c2。 for( int i=1。 i=8。i++) {( )。 cout“ \nc1=” ( )。 ( )。 } cout“ c2=” ( )。 for( i=1。 i=5。i++) {( )。 cout“ \nc2=” ( )。 ( )。 } cout“ c1=” ( )。 } 測試結(jié)果 c1=1 c1=2 c1=3 c1=4 c1=5 c1=6 c1=7 c1=8 c2=8 c2=7 c2=6 c2=5 c2=4 c2=3 c1=3 圓的類 class Circle { float radius。 public: Circle(float r=0):radius(r){}//構(gòu)造函數(shù) float GetRadius( )。 float CircleCircum( )。 float CircleArea( )。 }。 成員函數(shù)類外定義 Circle:: Circle(float r) //構(gòu)造函數(shù) { radius = r。 } float Circle:: GetRadius( ) { return radius。} float Circle:: CircleCircum( ) { return 2**radius。 } float Circle:: CircleArea( ) { return *radius*radius。 } 圓類的測試 include “” include “” void main( ) { Circle a(3), b(2)。 cout“Circum of Circle a = ” ( )endl。 cout“Area of Circle b = ” ( )endl。 } 長方形類 class Rectangle { float x, y。 public: Rectangle(float a=0, float b=0): x(a),y(b) { } float RecCircum( ){return 2*(x+y)。} float RecArea( ){return x*y。} }。 a. 對象指針 如同定義一個對象一樣,用類名可以申明一個對象指針。 例 counter *p, *q。 申明 counter 類的指針,指針沒有初始化, 調(diào)用指針必須 先分配內(nèi)存 ,或 指向一個變量的地址 ,否則出嚴重錯誤,甚至死機。 3. 對象指針和對象數(shù)組 ( 1) p=new counter( 3) 。 分配一個整形數(shù)據(jù)內(nèi)存 ,這時系統(tǒng)自動調(diào)用有參 構(gòu)造函數(shù)初始化 *p的 value=3。 ( 2) q=new counter[3]。 分配三個連續(xù)整形數(shù)據(jù)內(nèi)存,這時系統(tǒng)自動調(diào)用無參構(gòu)造函數(shù)初始化 q, q+1, q+2的 value都是 0。 如果類中沒有無參構(gòu)造函數(shù),語句( 2)出錯。 確定地址后的對象指針可以調(diào)用類中公有數(shù) 據(jù)及公有函數(shù)。 p→increment( )。 (q+1) →decremen
點擊復(fù)制文檔內(nèi)容
教學(xué)課件相關(guān)推薦
文庫吧 www.dybbs8.com
公安備案圖鄂ICP備17016276號-1