




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、C+蝴末復(fù)習(xí)(程序分析題)一、程序改錯題1 、指出下面程序段中的錯誤,并說明出錯原因。class A int a,b;public :A(int aa,int bb) a=aa;b=bb;void main() A x(2,3),y(4);答: Ax(2,3),y(4); 語句出錯,因為沒有單參數(shù)的構(gòu)造函數(shù)( 或者 y(4) 少寫了一個參數(shù))2、指出并改正下面利用類模板的對象定義中的錯誤。template <class T>class TanyT x,y;public:Tany(T a,T b)x=a,y=b;T sum( )return x+y;void main() Tany
2、(int) obj(10,100);答: Tany(int) obj(10,100); 出錯,應(yīng)為 Tany<int> obj(10,100); 語句。3 、 指出下面程序段中的錯誤,并說明出錯原因。class one private:int a;public:void func(two&);class two private:int b;friend void one:func(two&);void one:func(two& r) a=r.b; 答: void func(two&); 出錯, two 尚未聲明(應(yīng)在class one 前加聲明語句
3、 class two ; ) 。4、指出下面程序段中的錯誤,并說明出錯原因。 include <iostream.h>class A public:void fun( )cout<< " a.fun ” <<endl;class B public:void fun( )cout<< " b.fun ” <<endl;void gun( )cout<< " b.gun ” <<endl;class C:public A,public B private:int b; public:v
4、oid gun( ) cout<< c.gun <<endl;void hun( ) fun( ); ;答: void hun()fun(); 出錯, C : fun() 有二義性。5、指出下面程序段中的錯誤,并說明出錯原因。class Locationint X,Y=20;protected:int zeroX,zeroY;int SetZero(int ZeroX,int ZeroY);private:int length,height;public:float radius;void init(int initX,int initY);int GetX( );in
5、t GetY( );答: int X,Y=20; 出錯,不能采用這種方式初始化。6 、下面的程序類B 的定義中有一處錯誤,請用下橫線標(biāo)出錯誤所在行并說明錯誤原因。# include<iostream.h># include<string.h>class A public:A(const char *nm)strcpy(name,nm); private:char name80;class B:public A public:B(const char *nm):A(nm) void PrintName( )const;void B:PrintName( )const co
6、ut<< " name: " <<name<<endl; /錯誤, name 為私有成員void main( ) B b1( " wang li " ); b1.PrintName( );7 、用下橫線標(biāo)出下面程序 main 函數(shù)中的錯誤所在行,并說明錯誤原因。# include<iostream.h>class Location private:int X,Y;public:void init(int initX,int initY);int sumXY( );void Location:init(int
7、 initX,int initY) X=initX; Y=initY; int Location:sumXY( ) return X+Y; void main( )Location A1;int x,y;A1.init(5,3);x=A1.X;y=A1.Y;cout<<x+y<< " " <<A1.sumXY( )<<endl;/錯誤8、下面的程序有一處錯誤,請用下橫線標(biāo)出錯誤所在行并改正錯誤。# include<iostream.h> class Testpublic: static int x;int x=20
8、;/ 對類的靜態(tài)成員初始化錯誤void main ( ) cout<<Test:x;9、指出下面的程序中的兩處錯誤,并改正class Exam public:Exam(int y=10) data=y; int getdata() const return +data; /錯誤常成員函數(shù)不能修改對象/修改方法:刪除關(guān)鍵字conststatic int getcount() cout<<data;/ 錯誤靜態(tài)成員函數(shù)不能直接訪問非靜態(tài)成員/修改方法:刪除cout 這一行return +count ; private:int data;static int count;10
9、 、指出下面的程序中的錯誤,并改正char *string;string=new char free(string);/用 new 動態(tài)分配的內(nèi)存不能用 free 函數(shù)來釋放/修改方法:用delete 運(yùn)算符來釋放11 、指出下面的程序中的錯誤#include <iostream.h>#include <string.h>class Person public:Person(char* pN) cout <<"Constructing " <<pN <<endl;pName=new charstrlen(pN)+1
10、;if(pName!=0) strcpy(pName,pN);Person() cout <<"Destructing " <<pName <<endl; pName0='0'delete pName; protected:char* pName;void main() Person p1("Randy");Person p2=p1;/ 即 Person p2(p1);/對同一個空間的兩次釋放,修改方法:增加一個復(fù)制構(gòu)造函數(shù)Person:Person(Person& p) pName=new c
11、harstrlen(p.pName)+1;strcpy(pName,p.pName);五、寫出下面程序的執(zhí)行結(jié)果1 、 # include <iostream.h>class A int * a;public:A (int x) a = new int(x); cout<<"*a = "<<*a<<endl; delete a; ;void main() A x(3), *p;p = new A (5);delete p;輸出為:*a=3*a=52 、 # include <iostream.h>template
12、<class T, class Q>void f (T &x, Q &y) if (sizeof (T) > sizeof (Q) x = (T)y;else y = (Q)x;void main()double d;int i;d = 9999; i = 88;f(d,i);cout << "d=" << d << " i= " << i << endl;d = 88; i = 9999;f(i,d);cout << "d="
13、<< d << " i= " << i << endl;輸出為: d=88 i=88d=9999,i=99993 、 # include <iostream.h>class basepublic:virtual int func () return 0; ;class derived: public basepublic:int func() return 100; ;void main()derived d;base & b = d;cout << b.func() << endl
14、;cout << b.base:func() << endl;輸出為: 10004 、 # include <iostream.h>class Testprivate:static int val;int a;public:static int func();static void sfunc(Test &r);int Test:val = 20;int Test:func() val += val; return val; void Test:sfunc(Test &r)r.a = 25;cout << "Result
15、3 = " << r.a<<endl;void main() cout << "Result1 = " << Test:func() << endl;Test a;cout << "Result2 = " << a.func()<<endl;Test:sfunc(a);輸出為:Result1 =40Result2=80Result3=255 、 #include<iostream.h> template<class T> cl
16、ass Tclass private:T x,y;public:Tclass(T a,T b):x(a)y=b;Tclass(T a)y=(T)0,x=a; void pr( ) char c;c=(y>=(T)0 ? '+':'-');cout<< x << c <<(y>(T)0? y:-y)<< "i"<<endl; ;void main( )Tclass <double> a(10.5,-5.8);a.pr( );Tclass <int>
17、b(10);b.pr( );輸出為: 10.5-4.8i10+0i6 、 #include<iostream.h>class test public: test() cout <<" 調(diào)用構(gòu)造函數(shù)"<<endl; ;void f(int i) static test x;cout <<i<<endl; void main()f(10);f(20);輸出為: 調(diào)用構(gòu)造函數(shù)10207 、 include <iostream.h>void main( )int *a;int *&p=a;int b=1
18、0;p=&b;cout<<*a;輸出為: 108 、include <iostream.h>template<class T>Tf(T*a,T*b,int n)Ts=(T)0;for(int i=0;i<n;i+)s+=a i *b i ;return s;void main()double c 5 =1.1,2.2,3.3,4.4,5.5,d 5 =10.0,100.0,1000.0;cout<<f(c,d,5)<<endl;輸出為: 35319 、 #include <iomanip.h>void mai
19、n()for(int i=0;i<4;i+)cout<<endl<<setfill(' ')<<setw(4-i)<< '0' <<setfill('#')<<setw(i+i)<<(i>0?'0':' ' );輸出為:00#00#00#010 、運(yùn)行下面的程序,寫出當(dāng)輸入 25 , 60 時的輸出結(jié)果。#include <iostream.h>class goodsprivate:static int to
20、talWeight;int weight;public:goods(int w) weight=w; totalWeight+=w; goods(goods& gd) weight=gd.weight; totalWeight+=weight; goods() totalWeight-=weight; int getwg() return weight; static int getTotal() return totalWeight; ;int goods:totalWeight=0;void main() int w;cout<< "The initial w
21、eight of goods:"<<goods:getTotal()<<endl;cin>>w; / 輸入25goods g1(w);cin>>w; / 輸入60goods g2(w);cout<< "The total weight of goods:" <<goods:getTotal()<<endl;輸出為:The initial weight of goods:0The total weight of goods:8511 、 # include<iostream.h
22、>class A public:virtual void print( )cout<< " This is class A printing. " <<endl;class B:public A public:void print( ) ;class C:public B public:void print( )cout<< " This is class C printing. " <<endl; ;void show(A&a) a.print( ); void main( ) A a; B
23、 b; C c;show(a); show(b); show(c);輸出為: This is class A printing.This is class C printing.12 、 # include<iostream.h>class A public:A( )a=0;A(int i)a=i;void Print( )cout<<a<< " , " ;int Geta( )return a;private:int a;class B:public A public:B( )b=0;B(int i,int j,int k);void
24、Print( );private:int b;A aa;B:B(int i,int j,int k):A(i),aa(j)b=k;void B:Print( ) A:Print( );cout<<b<< " , " <<aa.Geta( )<<endl; void main( ) B bb2;bb0=B(1,2,5);bb1=B(3,4,7);for(int i=0;i<2;i+) bbi.Print( );輸出為: 1, 5, 23, 7, 413 、 #include<iostream.h>class T
25、est private:static int val;int a;public:static int func( );void sfunc(Test &r);int Test:val=200;int Test:func( ) return val+; void Test:sfunc(Test &r)r.a=125;cout<< "Result3=" <<r.a<<endl; void main( ) cout<< "Result1=" <<Test:func( )<<
26、endl;Test a;cout<< "Result2=" <<a.func( )<<endl; a.sfunc(a);輸出為:Result1 =200Result2=201Result3=12514 、 # include<iostream.h>class Location int X,Y;public:void init(int=0,int=0);void valueX(int val)X=val;int valueX( )return X;void valueY(int val)Y=val; int valueY( )r
27、eturn Y;void Location:init(int initX,int initY) X=initX; Y=initY; void main( ) Location A,B;A.init( );cout<<A.valueX( )<< A.valueX(5);cout<<A.valueX( )<<B.init(6,2);B.valueY(4);cout<<B.valueX( )<< 輸出為: 0 05 06 4"<<A.valueY( )<<endl;"<<A
28、.valueY( )<<endl;"<<B.valueY( )<<endl;15 、 #include <iostream.h>class Apublic:A( ) virtual void func( )cout<<"Destructor A" <<endl;A( ) func();class B:public Apublic:B( ) void func()cout<<"Destructor B" <<endl;B( ) func();void
29、main( )B b;A&a=b;Destructor BDestructor A16 、 include <iostream.h>class My Class public:int number;void set(int i);int number=3;void MyClass:set (int i)number=i;void main()MyClass my1;int number=10;my1.set(5);cout<<my1.number<<end1;my1.set(number);cout<<my1.number<<
30、endl;my1.set(:number);cout<<my1.number;輸出為:510317 、 #include<iostream.h>class AAAint A,B;public:AAA(int i,int j) A=i,B=j;cout<<"Cn" AAA()cout<<"Dn"void print( )cout<<A<<","<<B<<"n"void main()AAA *a1,*a2;a1=new AA
31、A(1,2);a2=new AAA(5,6);a1->print(); a2->print();delete a1; delete a2;輸出為:CC 1,2 5,6 D D18 、 #include <iostream.h> class b1 public:b1(int x) cout<<x<<"->An"b1() cout<<"Bn"class b2 public:b2(int x) cout<<x<<"->Cn" b2() cout&
32、lt;<"Dn"class AAA:public b2,public b1 public:AAA(int x,int y):b1(y),b2(x) cout<<"En"AAA()cout<<"Fn"void main() AAA obj(5,7);輸出為:5->C 7->A EFBD19 、 #include<iostream.h>class FATHERpublic:virtual void answer( ) cout<<"I am fathern&quo
33、t;class DAUGHTER:public FATHERpublic:virtual void answer( ) cout<<"I am daughtern" ;class SON:public FATHER public:void answer()cout<<"I am sonn" ;void main() FATHER father;DAUGHTER daughter;SON son;FATHER *who;who=&father;who->answer();who=&daughter;who-&g
34、t;answer();who=&son;who->answer();輸出為:I am fatherI am daughterI am son20 、 #include <iostream.h>class Base public:Base()cout<<"Generate Base"<<endl;Base()cout<<"Destroy Base"<<endl;class child:public Base public:child():Base()cout<<"
35、;Generate child"<<endl;child()cout<<"Destroy child"<<endl;void main() child c2;輸出為:Generate BaseGenerate childDestroy childDestroy Base21 、#include <iostream.h>#include <string.h>class StudentID public:StudentID(int id=0) value=id;cout <<"Assig
36、ning student id " <<value <<endl; StudentID() cout <<"Destructing id " <<value <<endl; protected:int value;class Student public:Student(char* pName="noName",int ssID=0) cout <<"Constructing student " <<pName <<endl;st
37、rncpy(name,pName,sizeof(name);namesizeof(name)-1='0'StudentID id(ssID);protected:char name20;StudentID id;void main() Student s("Randy",9818);Assigning student id 0Constructing student RandyAssigning student id 9818Destructing id9818Destructing id022、#include <iostream.h>clas
38、s MyClass public:MyClass();MyClass(int);MyClass();void Display();protected:int number;MyClass:MyClass() cout <<"Constructing normally.n" MyClass:MyClass(int m): number(m) cout <<"Constructing with a number: " <<number <<endl; void MyClass:Display() cout &l
39、t;<"Display a number: " <<number <<endl; MyClass:MyClass() cout <<"Destructing.n" void main() MyClass obj1;MyClass obj2(20);obj1.Display();obj2.Display();輸出為:Constructing normally.Constructing with a number: 20Display a number: 1 /此處為不確定的數(shù)Display a number: 20
40、Destructing.Destructing.23、#include <iostream.h>#include <string.h> class Student protected:char name40;public:Student(char* pName="no name") cout <<"Constructing new student " <<pName <<endl; strncpy(name,pName,sizeof(name);namesizeof(name)-1='0
41、' Student(Student& s) cout <<"Constructing copy of " << <<endl;strcpy(name,"copy of ");strcat(name,); Student() cout <<"Destructing " <<name <<endl; ;class Tutor protected:Student student;public:Tutor(Student& s):student(s)cout <<"Constructing tutorn"void fn(Tutor tutor)cout <<"In function fn()n"void main() Student randy("Randy");Tutor tutor(randy);c
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 出口碗碟采購合同范本
- 業(yè)務(wù)代表合同范本
- Unit 2 SectionA(1a~1e)教學(xué)設(shè)計- 2024-2025學(xué)年人教版(2024)七年級英語下冊
- 2024年招商銀行唐山分行社會招聘考試真題
- 出租稻田土地合同范本
- 2024年咸陽市實驗中學(xué)教師招聘筆試真題
- 借款公證合同范本
- 買車退款合同范本
- 住建備案合同范本
- 分包轉(zhuǎn)讓合同范本
- 2024-2030年鋰離子電池隔膜行業(yè)市場發(fā)展分析及發(fā)展趨勢與投資管理策略研究報告
- 四川省成都市金堂縣2023-2024學(xué)年八年級下學(xué)期期末考試語文試題
- CJJ101-2016 埋地塑料給水管道工程技術(shù)規(guī)程
- 大學(xué)生創(chuàng)新創(chuàng)業(yè)基礎(chǔ)教程(各類院校創(chuàng)新創(chuàng)業(yè)課程)全套教學(xué)課件
- 《民用無人駕駛航空器系統(tǒng)分類及分級》考試題庫(含答案)
- Y -S-T 1700-2024 銀礦采選業(yè)綠色工廠評價要求(正式版)
- 中職語文高教版(2023-2024)基礎(chǔ)模塊上冊二《風(fēng)景談》公開課一等獎創(chuàng)新教學(xué)設(shè)計
- (高清版)JTGT 5440-2018 公路隧道加固技術(shù)規(guī)范
- 簡單的痛(復(fù)雜的評估)-醫(yī)學(xué)評估
- 2024年時政試題庫(綜合卷)
- 第59講-熱重曲線分析(課件)
評論
0/150
提交評論