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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、精選優(yōu)質(zhì)文檔-傾情為你奉上一、 讀程序題目1. 本題考核指針與字符串,指向字符串的指針,意味著指針變量中存放字符串在內(nèi)存中所占存儲空間的首地址。此時指針變量可以作為數(shù)組名來使用。#include <iostream >using namespace std;void main()char *str = "abbcdace"int c1 = 0, c2 = 0, c3 = 0, c4 = 0;for (int i = 0; stri; i+)switch (stri) default: c4+;case 'a':c1+;case 'b

2、9;:c3+;case 'c':c2+; cout << c1 << "," << c2 << "," << c3 << "," << c4 << endl; 知識點復習:字符串結(jié)束標記為0 ,字符串在內(nèi)存中存放時實際字符后存放0這個標記。當輸出字符串時遇到0停止輸出。 for(int i = 0; stri; i+) 中的stri表示循環(huán)條件,讀出字符數(shù)組str中的各個字符,顯然遇到0停止。即stri等價于stri!=0。

3、 Switch結(jié)構特點:找到表達式滿足的入口后開始執(zhí)行本分支中的語句,遇到 break語句跳出switch結(jié)構,但如果沒有遇到break,執(zhí)行完本分支后繼續(xù)執(zhí)行下一分支語句。 將switch結(jié)構改為如下形式,結(jié)果是什么? switch (stri) default: c4+; break;case 'a':c1+; break;case 'b':c3+; break;case 'c':c2+; break; 2. 枚舉類型與枚舉變量的定義enum 枚舉類型名 枚舉常量列表 變量列表 ;定義枚舉類型的同時定義枚舉變量:enum Color Red,

4、 Yellow, White, Blue, Blackc1;先定義類型,再定義變量: enum Color Red, Yellow, White, Blue, Black; Color c1; #include <iostream>using namespace std;enum Color Red, Yellow, White, Blue, Blackc; void main()c= Red; cout << c << endl; c = Yellow; cout << c << endl;c = Black; cout <&

5、lt; c << endl;第一行運行結(jié)果為? 第二行運行結(jié)果為?第三行運行結(jié)果為?3. 復合語句:在復合語句中定義的變量僅在該語句中有效。#include <iostream >using namespace std;int c = 0;class Baseint a;public:Base()a = 2;c+;Base() c-; friend void show();void show() cout << "結(jié)果為: " << c << endl; void main()Base a;show(); Base

6、 b3; show();show();4. 繼承,組合#include <iostream >using namespace std;class Baseint x;public:Base(int y) cout << "調(diào)用基類構造函數(shù)" << endl; x=y; Base() cout << "調(diào)用基類析構函數(shù)" << endl; void showx() cout << x << "," int Getx() return x; ;class

7、Derived :public Baseint k;Base b1;public:Derived(int n, int m, int p) :Base(m), b1(p)cout << "調(diào)用派生類構造函數(shù)" << endl;k= n;Derived() cout << "調(diào)用派生類析構函數(shù)" << endl; void show()Base:showx();cout << k << "," << b1.Getx() << endl;voi

8、d main()Derived obj(8, 13, 24);obj.show();5. 函數(shù) #include<iostream >using namespace std;class Testpublic: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:Show()cout << a << "," << b <

9、< endl;cout << "p=" << p << endl;void Test:Show() constcout << a<< "," << b << " const" << endl;cout << "p=" << p << " const" << endl;void main()Test t1(3.3, 5.5, 7.7);t1.Show

10、();const Test t2(8, 6, 4);t2.Show();6. 指針#include <iostream >using namespace std;void fun1(int a, int *p1, int *p2)*p1 = 2*a;*p2 = a*a;void main()int a, p, q;a = 5;fun1(a, &p, &q);cout << p << "," << q << endl;7. 聯(lián)合#include<iostream >using namesp

11、ace std;union dataint i;char c;float f;d;void main()for (int j = 0; j<3; j+)switch (j) case 0: d.i = 8; cout << d.i << endl; break; case 1: d.c = 'a' cout << d.c << endl; break; case 2: d.f = 9.1; cout << d.f << endl; break;8.運算符重載#include <iostream

12、>#include <string>using namespace std;class Pointpublic:Point(char *s)p = new charstrlen(s) + 1;strcpy(p, s);void Show() cout << p << endl; char& operator (int i) return *(p + i); private:char *p;void main()char *s = "PEKING"Point x(s);x.Show();int n = strlen(s);wh

13、ile (n >= 0)xn - 1 = xn - 1 + 32;n-;x.Show();9.多重繼承與虛函數(shù)#include<iostream> using namespace std;class A public:virtual void fun()cout << "fun in class A" << endl;class Bpublic:virtual void fun()cout << "fun in class B" << endl;class C :public A, pub

14、lic Bpublic:void fun()cout << "fun in class C" << endl;int main()C c;A& p1 = c;B& p2 = c;C& p3 = c;p1.fun();p2.fun();p3.fun();10. 純虛函數(shù)(抽象類),基類指針指向派生類對象。#include<iostream>#include<string> using namespace std;class Vehiclepublic:virtual void show() = 0;pro

15、tected:char Name20;class Car :public Vehiclepublic:Car(char *name)strcpy(Name, name);void show()cout << Name << endl;protected:int Radius;class Truck :public Vehiclepublic:Truck(char *name)strcpy(Name, name);void show()cout << Name << endl;class Boat :public Vehiclepublic:Boa

16、t(char *name)strcpy(Name, name);void show()cout << Name << endl;void main()Vehicle *p;Car car("法拉利");Truck truck("卡車");Boat boat("潛水艇");p = &car;p->show();p = &truck;p->show();p = &boat;p->show();11. 異常處理機制讀懂程序9-1,9-2。運行程序,理解什么情況下程序停止繼續(xù)執(zhí)

17、行。請自己輸入代碼驗證。12. 函數(shù)模板#include <iostream>using namespace std;template <typename T>void bubblesort(T a, int n) /從大到小,即降序int i, j;T temp;for (i = 0; i<n - 1; i+) /輪數(shù)for (j = 0; j<n - i - 1; j+) /每論兩兩交換的次數(shù),j也是下標if (aj<aj + 1)temp = aj;aj = aj + 1;aj + 1 = temp;template <typename T>void print(T a, int n)for (int i = 0; i<n; i+)cout << ai << 't'void main()int x8 = 23, 21, 15, 45, 66, 57, 86, 9 ;bubblesort(x, 8);print(x,

溫馨提示

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

評論

0/150

提交評論