C++程序的設計教程_第1頁
C++程序的設計教程_第2頁
C++程序的設計教程_第3頁
免費預覽已結束,剩余17頁可下載查看

下載本文檔

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

文檔簡介

1、第 1 章 類和對象一、 選擇題1.C 2.B 3.C 4.A 5.C6.A 7.C 8 C 9A 10 C 二、閱讀題 1 x=2,y=32 x=2,y=3 x!=y3Cstatic:va1=0cs1.vaI=1cs2.val=2cs1.val=4cs2.vaI=4四、改錯題#include #include class personpublic:person(int n,char* nam,char s)num=n; strcpy(name,nam); sex=s;coutConstructor called.endl; person( )coutDestructor called.end

2、l;void display( )coutnum: numendl; coutname: nameendl; coutsex: sexendlendl;private:int num;char name10;char sex;int main( )person s1(10010,Wang_li,f); s1.display( );person s2(10011,Zhang_fun,m);s2.display( );return 0;五、編程題51#include using namespace std;class CBoxpublic :CBox(double l=0,double w=0,d

3、ouble h=0); double area();double volume ();private :double lengh;double width; double high;CBox:CBox(double l,double w,double h) lengh=l; width=w; high=h;double CBox:area()return 2*(lengh*width+lengh*high+width*high);double CBox:volume ()return lengh*width*high;void main()CBox box1(4,5,6);coutbox1.a

4、rea()endl;coutbox1.volume()endl;5.2#include using namespace std;class CPointpublic :CPoint(double a=0,double b=0)x=a;y=b;CPoint(CPoint & p)x=p.x;y=p.y;void print()cout(x,y);private :double x,y;class CLinepublic:CLine(double x1=0,double y1=0,double x2=0,double y2=0):p1(x1,y1),p2(x2,y2) CLine(CPoint x

5、,CPoint y):p1(x),p2(y)CLine(CLine &lin)p1=lin.p1;p2=lin.p2;void DrawLine() coutLine form; p1.print(); coutto;p2.print(); coutendl;void Linedel() coutdelete lineendl;void move(CPoint &x,CPoint &y)coutmove lineendl;p1=x;p2=y;private :CPoint p1,p2;void main()CPoint point1(1,5),point2(5,8),point3(20,30)

6、,point4(40,50); CLine line1(point1,point2);CLine line2(2,3,8,12); line1.DrawLine (); line2.DrawLine (); line2.move(point3,point4);line2.DrawLine (); line2=line1;line2.DrawLine (); line1.Linedel ();5.3#include using namespace std; class CComplexpublic:CComplex(double, double);CComplex(CComplex &c); /

7、 復數類的拷貝構造函數聲明 double GetReal();double GetImag();void Print();private:double real;double imag;CComplex:CComplex (double r=0.0, double i=0.0)real = r;imag = i;cout 調用兩個參數的構造函數 endl;CComplex:CComplex (CComplex &c) / 復數類的拷貝構造函數定義real = c.real;imag = c.imag;cout 調用拷貝構造函數 endl;double CComplex:GetReal()ret

8、urn real;double CComplex:GetImag()return imag;void CComplex:Print() / 顯示復數值cout ( real , imag ) endl;CComplex add(CComplex &x,CComplex &y) / 普通函數完成兩個數的加法,對象作為 函數參數 ,return CComplex(x.GetReal() +y.GetReal() ,x.GetImag ()+y.GetImag ();void main(void)CComplex a(3.0,4.0), b(5.6,7.9);CComplex c(a); / 調用復

9、數類的拷貝構造函數 cout a = ;a. Print();cout b = ;b. Print();cout c = ;c. Print(); coutc=a+bendl; c=add(a,b); cout c = ; c.Print ();5.4#include #include using namespace std;類聲明class CStudent /public:CStudent(char *,float,float,float); CStudent(CStudent &s);CStudent();void display();friend float avg(CStudent

10、&s); private:char *name;float grad3;CStudent:CStudent(char *na,float a,float b,float c) name=new charstrlen(na)+1; strcpy(name,na);grad0=a;grad1=b;grad2=c;CStudent:CStudent(CStudent &s)name=new charstrlen()+1; strcpy(name,);grad0=s.grad0;grad1=s.grad 1;grad2=s.grad 2;CStudent:CStudent()d

11、elete name;void CStudent:display( )int i;coutname:nameendl;for(i=0;i3;i+)coutgradi:gradiendl;float avg(CStudent &s) / 普通函數,需要引用私有成員,聲明為學生類的友元函數 return (s.grad0+s.grad1 +s.grad2)/3;int main( )CStudent stud1( 三,79,98,82);/ 定義對象stud1.display();cout 平均成績: avg(stud1)endl;return 0;5.5#include using namesp

12、ace std;class CStringpublic :CString(); /缺省構造函數,初始化為空串CString(char ch,int nRepeat);/ 用一個字符重復 n 次,初始化字符串CString(const char*psz); /用一個字符串初始化CString(CString &stringser); / 拷貝構造函數CString();int GetLength() const;bool IsEmpty() const;char GetAt(int nindex) const;void Setat(int nindex,char ch);void Print()

13、;int compare(CString& string);int compare(const char* psz)const;void Vacate();private :char *s;CString:CString()s=NULL;CString:CString(CString& stringser)s=new charstrlen(stringser.s)+1;if(s!=0)strcpy(s,stringser.s);CString:CString(char ch,int nRepeat) s=new charnRepeat+1;for(int i=0;inRepeat;i+) si

14、=ch;snRepeat=0;CString:CString(const char*psz)s=new charstrlen(psz)+1;if(s!=0)strcpy(s,psz);int CString:GetLength() const int i=0;while (si!=0)i+;return i;bool CString:IsEmpty() constif(s=NULL)return 1;elsereturn 0;void CString:Vacate()s0=0;coutNow have vacated string.1&nindex1&nindex=GetLength()+1)

15、 s nindex-1=ch;void CString:Print()couts0)return 1;else if(strcmp(s,string.s)0)return 1;else if(strcmp(s,psz)0)return -1;else return 0;CString:CString()/coutendl 析構 :sendl;if(s!=NULL)deletes;void main()char a4=y;char b4;CString c1(Hellow),c2(c1); coutStringc1 is: ; c1.Print(); coutendl;coutStringc2

16、is: ; c2.Print(); coutendl;CString c3(b,3);coutStringc3 is: ; c3.Print(); coutendl;cout以下是對串的基本操作 *endl;int num=c1.GetLength(); char ch;coutc1 的長度是 : numendl;ch=c1.GetAt(5);cout獲得字符串cl中第5個字符是:chendl;cout 下面是插入字符串 c1 中一個特殊字符 dendl;c1.Setat(5,d);cout 插入后字符串 c1 變為: ;c1.Print();/coutendl;if(c1.IsEmpty()

17、=1)coutString is empty.endl; elsecoutString isnt empty.endl;cout 下面是判斷字符串 c1 清空 endl; c1.Vacate();cout 清空后輸出字符串 c1: n;c1.Print();cout 字符串已被清空 endl;cout 請按任意鍵繼續 b;/*CString c5=c2;coutString c5=c2 is: ;c5.Print(); coutendl;cout以下是對串的比較*endl;int temp=pare(c3); cout 以下比較 c2 與 c30)coutStringc3endl; else

18、if(temp0)coutStringc2Stringc3endl; elsecoutStringc9=Stringc4endl; coutendl;cout 以下比較 c2 與任意字符串 Goodboy!0) coutGoodboy!endl;else if(pare(Goodboy!)0) coutStringc2Goodboy!endl;elsecoutStringc2 =Goodboy!endl;第二章 繼承和派生一、選擇題1D 2.B 3. D 一、閱讀程序題四、編程題4.1#include #include#define PAI 3.14class Circlepublic:Cir

19、cle()r=0;Circle (double d)r=d;double area()return(PAI*r*r);void display1()cout 桌子的面積 :area()endl;private:double r;class Tablepublic:Table()heig=0;Table (double h) heig=h;void display2()cout 桌子的高度 :heigendl;private:double heig;class Roundtable : public Circle,public Tablepublic:Roundtable()strcpy(col

20、or,白色 );Roundtable(double a,double b,char* c):Circle(a),Table(b)strcpy(color,c);void display () display1();display2();cout 桌子的顏色 :colorendl; private:char color20;void main()Roundtable s(2.0,3.0, 黑色 );s.display();4.2如果 Age 在基類中被定義為私有的, SetAge(int n) 不能直接給 Age 賦值,如果 Age 是基類 的公有成員變量,則可以直接賦值。class Anima

21、lpublic:Animal();int Age;class Dog:public Animalpublic:Dog();Void SetAge(int n)Age=n;4.3 #include class Rectanglepublic:Rectangle (double l,double w)length=l,width=wdouble area()return length*width; void display1();private:double length;double width;void Rectangle:display1()cout 長 :lengthendl; cout

22、寬 :widthendl; class Cuboid:public Rectanglepublic:Cuboid (double L,double w,double h):Rectangle(L,w)high=h,volume=L*w*high ; double vol()return area()*high;void show (); private: double high;double volume;void Cuboid:show()display1();cout 高 :highendl; cout 體積 :vol()endl;void main()Cuboid cub (10,20,

23、30);cub.show();4.4#include class Vehiclepublic:Vehicle():maxspeed(0),weight(0)Vehicle(double m,double w):maxspeed(m),weight(w);void run () cout 可以奔跑 endl; void stop () cout 可以停止奔跑 endl; private:double maxspeed;double weight;class Bicycle:virtual public Vehiclepublic:Bicycle (double m,double w,double

24、 h):Vehicle(m,w),height(h) private:double height;class Motorcar : virtual public Vehiclepublic:Motorcar (double m,double w,double s):Vehicle(m,w),setnum(s) private:double setnum;class Motorcycle:public Bicycle,public Motorcarpublic:Motorcycle(double m,double w,double h,double s):Bicycle(m,w,h),Motor

25、car(m,w,s), Vehicle(m,w);void main()Motorcycle s1(300,10,6,30);s1.run();s1.stop();如果不把 Vehicle 設置為虛基類,會產生二義性問題。第 3 章 多態性3.1 簡答題3.1.1 哪些運算符可以重載? 幾乎所有的運算符都可以重載,除了以下的幾個運算符: , : , ?: J,3.1.2 運算符重載后,優先級和結合性如何?用戶重新定義運算符, 不改變原運算符的的優先級和結合性。 同時運算符重載后, 也不 改變運算符的語法結構, 即單目運算符只能重載為單目運算符, 雙目運算符只能重載為雙目 運算符。3.1.3 編

26、譯程序如何選用運算符函數?在每一種編譯系統中, 運算符實際上都對應一個函數, 只是這種運算對用戶具有透明性, 使用者并不知道函數的存在。 運算符重載實際上是運算符函數的重載, 所以運算符的重載實 際上是函數的重載。編譯程序對運算符重載的選擇, 遵循著函數重載的選擇原則。 當遇到不很明顯的運算符 時,編譯程序將去尋找參數相匹配的運算符函數。3.1.4 重載運算符有哪些限制?(1) 不可臆造新的運算符(2) 堅持四個不能改變。 不能改變運算符原有的優先級 不能改變運算符原有的結合性 不能改變運算符原有的語法結構 不能改變運算符原有的操作數個數但允許改變 運算符的返回類型(3) C+規定,運算符中,

27、參數類型都是部類型時, 不允許重載。“:、*、-、?: ” 這五個運算符不能重載。3.1.5 運算符重載必須遵循哪些原則? 運算符重載可以使程序更加簡潔,使表達式更加直觀,增強可讀性。但是,運算符重載 使用不宜過多,否則會帶來一定的麻煩。運算符重載必須遵循以下原則:( 1)重載運算符含義必須清楚。( 2)重載運算符不能有二義性。3.2填空題3.2.1 C+中多態性包括兩種多態性:(1) 和 (2)。前者是通過(3) 實現的,而后者是通過 (4) 和 (5)來實現的。答案:(1)編譯時的 (2 )運行時的(3) 函數和運算符的重載(4) 類繼承關系(5) 虛函數3.2.2 純虛函數定義時在函數參

28、數表后加(1),它表明程序員對函數(2),其本質是將指向函數體的指針定為(3) o答案:(1) =0(2) 不定義(3) NULL在基類中將一個成員函數說明成虛函數后,在其派生類中只要(1)、(2)和(3)完全一樣就認為是虛函數,而不必再加關鍵字(4)。如有任何不同,則認為是(5)而不是虛函數。除了非成員函數不能作為虛函數外,(6)、( 7)和( 8)也不能作為虛函數。答案:(1)同虛函數名(2 )同參數表(3) 同返回類型。如基類中返回基類指針,而派生類中返回派生類指針是允許的(4) virtual(5) 重載(6) 靜態成員函數(7) 聯函數(8 )構造函數第三大題答案:3. 1答案:#i

29、n clude#in cludeclass CStude ntchar n ame20;char n ativeplace20;char code30;int age;float score;public :CStude nt(char *, char*,char*,i nt ,float);CStude nt(CStude nt &s);void display();float operator+(CStude nt s1);CStudent:CStudent(char *name, char*native,char*code,int age,float score) strcpy(this

30、-name,name); strcpy(this-nativeplace,native);strcpy(this-code,code);this-age=age; this-score=score;CStudent:CStudent(CStudent &s)strcpy(this-name,); strcpy(this-nativeplace,s.nativeplace);strcpy(this-code,s.code);this-age=s.age; this-score=s.score;void CStudent:display()agecoutname nativeplace

31、 code scorescore+s1.score;void main()CStudent w(whl,zhengzhou,30,90); w.display();CStudent c(ctm,zhengzhou,30,90);c.display(); coutw+cendl;32 答案:#include#include#includeclass CTrianglefloat a,b,c;public:CTriangle(float a,float b,float c)this-a=a; this-b=b; this-c=c; float GetArea() float t=(a+b+c)/2

32、;return sqrt(t*(t-a)*(t-b)*(t-c);float operator +(CTriangle t)return this-GetArea()+t.GetArea();void main()CTriangle tr1(3,4,5),tr2(6,8,10); couttr1+tr2endl;33 答案:#include class BaseClass public: virtual void f1() coutBaseClass:f1()endl; void f2() coutBaseClass:f2()endl; ;class DerivedClass: public

33、BaseClass public:void f1() coutDerivedClass:f1()endl; void f2() coutDerivedClass:f2()f1();Bptr-f2();Dptr=&d1;Dptr-f1();Dptr-f2(); 習題四、選擇題1D 2 A 3 B 4 C 5. C 6 C 7 A 二、簡答題1什么叫做流?流的提取和插入是指什么?I/O流在C+中起著怎樣的作用?答:流是一種抽象,它負責在數據的生產者(程序 /文件)和數據的消費者(文件 /程序)之 間建立聯系,并管理數據的流動。一般意義下的讀操作在流數據抽象中被稱為 (從流中)提取,寫操作被稱為 (

34、向流中)插入。 完成數據從動態(存)到靜態(外存)的轉換,或著是從靜態(外存)到動態(存)的轉換。 2什么是字節流、字符流和二進制流?答:根據對字節容的解釋方式,字節流分為字符流(也稱文本流)和二進制流。字符流將字節流的每個字節按ASCH字符解釋,它在數據傳輸時需作轉換,效率較低。例如源程序文件和文本文件都是字符流。由于ASCH字符是標準的,所以字符流可以直接編輯,顯示或打印,字符流產生的文件通行于各類計算機。它在數據傳輸時不作任何轉換, 故效率高。且無法人工閱讀, 故二進制流產生的文件和 clog 的用法相同,但作用不同。 cerr 的輸出有關出錯信息。 cerr 流中的信息只能二進制流將字

35、節流的每個字節以二進制方式解釋,但各類計算機對數據的二進制存放格式各有差異, 可移植性較差。3 cerr 和 clog 作用是什么?有何區別? 答:對于輸入提示信息或輸出結果而言, cerr 作用是向標準錯誤設備 (standard error device)在顯示器輸出。 當調試程序時, 往往不希望程序運行時的出錯信息被送到其他文件,而要求在顯示器上及時輸出,這時應該用 cerr , cerr 流中的信息是用戶根據需要指定的。clog 流對象也是標準錯誤流,它是 console log 的縮寫。它的作用和 cerr 相同,都是在終端顯示 器上顯示出錯信息。不同的是 cerr 不經過緩沖區,直

36、接向顯示器上輸出有關信息,而 clog 中的信息存放在緩沖區中,緩沖區滿后或遇 endl 時向顯示器輸出。4用什么方法來控制輸入輸出流中出現的錯誤? 答:為提高程序的可靠性, 應在程序中檢測 I/O 流的操作是否正常。 當檢測到流操作出現錯 誤時,可以通過異常處理來解決問題。5比較讀寫文本文件與二進制文件的異同。答:從文件編碼的方式來看,文件可分為 ASCII 碼文件和二進制碼文件兩種。ASCII 文件也稱為文本文件,這種文件在磁盤中存放時每個字符對應一個字節,用于存 放對應的 ASCII 碼。例如,數 5678的存儲形式為:ASM00110101 00110110 00110111 0011

37、1000十進制碼:5678 共占用 4 個字節。 ASCII 碼文件可在屏幕上按字符顯示,例如源程序文件就是ASCII文件,用DOS命令 TYPE可顯示文件的容。由于是按字符顯示,因此能讀懂文件容。二進制文件是按二進制的編碼方式來存放文件的。 例如, 數 5678 的存儲形式為: 00010110 00101110 只占二個字節。二進制文件雖然也可在屏幕上顯示, 但其容無法讀懂。 C+系統在處理這些文件時,并不區分類型,都看成是字符流,按字節進行處理。輸入輸出字符流的開始和結束只由程序控制而不受物理符號 (如回車符 )的控制。 因此也把這種文件 稱作“流式文件”。6隨機讀寫是什么意思,常用于哪

38、種類型的文件?答:在C+中可以由程序移動文件指針,從而實現文件的隨機訪問,即可讀寫流中任意一段 容。一般文本文件很難準確定位,所以隨機訪問多用于二進制文件。7文件流和字符串流有什么區別?答:文件在C+看來是字符流或二進制流,文件流是以外存文件為輸入輸出對象的數據,所 以文件流是與設備相關的。可以把流的概念應用到字符串 (String )上。這樣字符串就可以 看作字符串流。字符串流不是以外存文件為輸入輸出的對象, 而以存中用戶定義的字符數組 為輸入輸出的對象。字符串流是與存相關, 所以也稱存流。可以用輸入輸出操作來完成字符 串流的操作。三、編程題1x= 468y= -3.42565x=468 y=-3.42565 x=+468*y=-3.43*2 生成一個名稱為 data.txt 文件,容: This is test data3 源程序。#in

溫馨提示

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

評論

0/150

提交評論