C練習(xí)題2015年下讀程序練習(xí)題(20210424194926)_第1頁
C練習(xí)題2015年下讀程序練習(xí)題(20210424194926)_第2頁
C練習(xí)題2015年下讀程序練習(xí)題(20210424194926)_第3頁
C練習(xí)題2015年下讀程序練習(xí)題(20210424194926)_第4頁
C練習(xí)題2015年下讀程序練習(xí)題(20210424194926)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、讀程序題目1. 本題考核指針與字符串,指向字符串的指針,意味著指針變量中 存放字符串在內(nèi)存中所占存儲空間的首地址。此時指針變量可以 作為數(shù)組名來使用。#include using namespace std;void mainOchar *str = abbcdace;int cl = 0, c2 = 0, c3 = 0, c4 = 0;for (int i = 0; stri; i+)switch (stri)default: c4+;case a* :cl+;case b :c3+;case c* :c2+;cout cl c2 c3 ” c4 endl;知識點(diǎn)復(fù)習(xí):字符串結(jié)朿標(biāo)記為0 ,

2、字符串在內(nèi)存中存放時實(shí)際字符后存放0這個標(biāo)記。 當(dāng)輸岀字符串時遇到0停止輸出。 for(int i = 0; stri; i+) 中的stri表示循環(huán)條件,讀出字符數(shù)組str中的各 個字符,顯然遇到0停止。即stri等價(jià)于striU (T o Switch結(jié)構(gòu)特點(diǎn):找到表達(dá)式滿足的入口后開始執(zhí)行本分支中的語句,遇到break語句 跳出switch結(jié)構(gòu),但如果沒有遇到break,執(zhí)行完本分支后繼續(xù)執(zhí)行下一分支語句。將switch結(jié)構(gòu)改為如下形式,結(jié)果是什么?switch (stri)default: c4+; break;case * a* :cl+; break;case b :c3+; br

3、eak;case c :c2+; break;2. 枚舉類型與枚舉變量的定義enum枚舉類型名枚舉常量列表變量列表;定義枚舉類型的同時定義枚舉變量:enum Color Red, Yellow, White, Blue, Black cl:先定義類型,再定義變量:enum Color Red, Yellow, White, Blue, Black;Color cl;nclude using namespace std;enum Color Red, Yellow, White, Blue, Blackc; void main()c= Red; cout c endl;c = Yellow; c

4、out c endl;c = Black; cout c endl;第一行運(yùn)行結(jié)果為?第二行運(yùn)行結(jié)果為?第三行運(yùn)行結(jié)果為?3. 復(fù)合語句:在復(fù)合語句屮定義的變量僅在該語句屮有效。#include using namespace std;int c = 0;class Baseint a;public:BaseOa = 2;c+;BaseO c; friend void show();;void show() cout 結(jié)果為:” c endl; void mainOBase a;show(); Base b3;show();show();4. 繼承,組合#include using names

5、pace std;class Baseint x;public:Base(int y) cout 調(diào)用基類構(gòu)適函數(shù) endl; x=y; Base0 cout ”調(diào)用基類析構(gòu)函數(shù) endl; void showxO cout x int GetxO return x; ;class Derived :public Baseint k;Base bl:public:Derived (int n, int m, int p) :Base (m), bl (p) cout 刃調(diào)用派生類構(gòu)造函數(shù) endl;k= n;DerivedO cout using namespace std;class Tes

6、tpublic:Test(double m, double n, double d) :p(d) a = m; b = n; void Show();void Show() const;private:double a, b;const double p;void Test::ShowOcout a b endl;cout p二 p endl;void Test::ShowO constcout a / b const endl;cout p二 p ” const endl;void mainOTest tl (3. 3, 5. 5, 7. 7);tl. Show();const Test t

7、2(8, 6, 4);t2. Show ();6. 指針nclude using namespace std;void funl (int a, int *pl, int *p2)*pl = 2*a;*p2 = a*a;void main()int a, p, q;a 二 5;funl (a, &p, &q);cout p q using namespace std;union dataint i;char c;float f;d;void mainOfor (int j = 0; j3; j+)switch (j)case 0: d i =8; cout di endl; break: ca

8、se 1: d c = a ; cout d c endl; break; case 2: df 二 9.1; cout d f endl; break;8. 運(yùn)算符重載include #include using namespace std;class Pointpublic:Point(char *s)p = new char strlen(s) + 1; strcpy (p, s);void ShowO cout p endl; char& operator (int i) return *(p + i); private:char *p;void mainOchar *s = PEKI

9、NG;Point x(s);x. ShowO ;int n = strlen(s);while (n = 0)3/8xn - 1 = xn - 1 + 32;n;x. ShowO ;9. 多重繼承與虛函數(shù)#includeusing namespace std;class A public:virtual void fun()cout fun in class endl;class Bpublic:virtual void fun 0cout fun in class B endl;class C :public A, public Bpublic:void fun()cout fun in c

10、lass C endl;int mainOC c;A& pl = c;B& p2 = c;C& p3 = c;pl. fun();p2. fun();p3 fun();10. 純虛函數(shù)(抽象類),基類指針指向派生類對象。#include#includeusing namespace std;class Vehiclepublic:virtual void show() = 0;protected:char Name20;class Car :public Vehicle public:Car(char *name)strcpy(Name, name); void show()cout Name

11、 endl;protected:int Radius;class Truck :public Vehiclepublic:Truck(char *name)strcpy(Name, name); void show()cout Name endl;class Boat :public Vehiclepublic:Boat(char *name)strcpy(Name, name); void show()cout Name endl; ;void mainOVehicle *p;Car car (法拉利”);Truck truck (卡乍);Boat boat (潛水艇); p 二 &car;

12、p-show();p = &truck; p-show();p = &boat; p-show();11. 異常處理機(jī)制讀懂程序9-1, 9-2o運(yùn)行程序,理解什么情況下程序停止繼續(xù)執(zhí) 行。請自己輸入代碼驗(yàn)證。12. 函數(shù)模板include using namespace std;template void bubblesort (T a, int n) /從大到小,即降序int i, j;T temp;for (i = 0; in - 1; i+)/輪數(shù)for (j = 0; jn - i - 1; j+) /每論兩兩交換的次數(shù),j也是下標(biāo) if (ajaj + 11)temp = aj;aj = aj + 1;aj + 1 = temp;template void p

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論