版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、C+期末復(fù)習(xí)(程序分析題) 一、程序改錯(cuò)題 1、指出下面程序段中的錯(cuò)誤,并說明出錯(cuò)原因。 class Aint 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); 語句出錯(cuò),因?yàn)闆]有單參數(shù)的構(gòu)造函數(shù) ( 或者 y(4) 少寫了一個(gè)參數(shù) ) 。2、指出并改正下面利用類模板的對(duì)象定義中的錯(cuò)誤。 template <class T> class Tany T x,y; public: Tany(T a,T b)x=a,y=b; T sum( )return x+y; ;
2、 void main()Tany (int) obj(10,100); 答:Tany(int) obj(10,100); 出錯(cuò),應(yīng)為 Tany<int> obj(10,100); 語句。 3、 指出下面程序段中的錯(cuò)誤,并說明出錯(cuò)原因。 class oneprivate: int a; public: void func(two&); ; class twoprivate: int b; friend void one:func(two&); ; void one:func(two& r) a=r.b; 答:void func(two&); 出錯(cuò), t
3、wo 尚未聲明 (應(yīng)在 class one 前加聲明語句 class two ; ) 。 4、指出下面程序段中的錯(cuò)誤,并說明出錯(cuò)原因。 include <iostream.h> class Apublic: 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 Bprivate:int b;
4、 public:void gun( ) cout<< c.gun <<endl; void hun( ) fun( ); ; 答:void hun()fun(); 出錯(cuò), C : fun() 有二義性。5、指出下面程序段中的錯(cuò)誤,并說明出錯(cuò)原因。 class Location int 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 i
5、nitY); int GetX( ); int GetY( ); ;答:int X,Y=20; 出錯(cuò),不能采用這種方式初始化。6、下面的程序類 B 的定義中有一處錯(cuò)誤,請(qǐng)用下橫線標(biāo)出錯(cuò)誤所在行并說明錯(cuò)誤原因。 # include<iostream.h> # include<string.h> class Apublic:A(const char *nm)strcpy(name,nm); private:char name80; ; class B:public Apublic:B(const char *nm):A(nm) void PrintName( )const;
6、 ; void B:PrintName( )const cout<< " name: " <<name<<endl; /錯(cuò)誤,name為私有成員 void main( )B b1( " wang li " ); b1.PrintName( ); 7、用下橫線標(biāo)出下面程序 main 函數(shù)中的錯(cuò)誤所在行,并說明錯(cuò)誤原因。 # include<iostream.h> class Locationprivate: int X,Y; public: void init(int initX,int initY); in
7、t sumXY( ); ; void Location:init(int 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; /錯(cuò)誤 8、下面的程序有一處錯(cuò)誤,請(qǐng)用下橫線標(biāo)出錯(cuò)誤所在行并改正錯(cuò)誤。# include<iostream.h&g
8、t; class Test public: static int x; ; int x=20; / 對(duì)類的靜態(tài)成員初始化錯(cuò)誤 void main ( ) cout<<Test:x; 9、指出下面的程序中的兩處錯(cuò)誤,并改正class Exampublic:Exam(int y=10) data=y; int getdata() const return +data; /錯(cuò)誤常成員函數(shù)不能修改對(duì)象/修改方法:刪除關(guān)鍵字conststatic int getcount()cout<<data;/ 錯(cuò)誤靜態(tài)成員函數(shù)不能直接訪問非靜態(tài)成員/修改方法:刪除cout這一行return
9、 +count ; private:int data;static int count; ;10、指出下面的程序中的錯(cuò)誤,并改正char *string;string=new charfree(string);/用new動(dòng)態(tài)分配的內(nèi)存不能用free函數(shù)來釋放/修改方法:用delete運(yùn)算符來釋放11、指出下面的程序中的錯(cuò)誤#include <iostream.h>#include <string.h>class Personpublic: Person(char* pN) cout <<"Constructing " <<pN
10、 <<endl; pName=new charstrlen(pN)+1; 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);/對(duì)同一個(gè)空間的兩次釋放,修改方法:增加一個(gè)復(fù)
11、制構(gòu)造函數(shù)Person:Person(Person& p) pName=new charstrlen(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; 輸
12、出為:*a=3*a=52、# include <iostream.h> template <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 <<
13、 endl; d = 88; i = 9999; f(i,d); cout << "d=" << d << " i= " << i << endl; 輸出為:d=88 i=88d=9999,i=99993、# include <iostream.h> class base public: virtual int func () return 0; ; class derived: public base public: int func() return 100; ; void ma
14、in() derived d; base & b = d; cout << b.func() << endl; cout << b.base:func() << endl; 輸出為:10004、# include <iostream.h> class Test private: static int val; int a; public: static int func(); static void sfunc(Test &r); ; int Test:val = 20; int Test:func() val +=
15、val; return val; void Test:sfunc(Test &r) r.a = 25; cout << "Result3 = " << r.a<<endl; void main() cout << "Result1 = " << Test:func() << endl; Test a; cout << "Result2 = " << a.func()<<endl; Test:sfunc(a); 輸出為:R
16、esult1 =40Result2=80Result3=255、#include<iostream.h> template<class T> class Tclassprivate: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"&
17、lt;<endl; ; void main( ) Tclass <double> a(10.5,-5.8); a.pr( ); Tclass <int> b(10); b.pr( ); 10+0i6、#include<iostream.h> class testpublic: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ù)1
18、0207、include <iostream.h> void main( ) int *a; int *&p=a; int b=10; 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,10
19、00.0; cout<<f(c,d,5)<<endl; 輸出為:3531 9、#include <iomanip.h> void main() for(int i=0;i<4;i+) cout<<endl<<setfill(' ')<<setw(4-i)<< '0' <<setfill('#')<<setw(i+i)<<(i>0?'0':' ' ); 輸出為: 0 0#0 0#0 0#
20、0 10、運(yùn)行下面的程序,寫出當(dāng)輸入 25 , 60 時(shí)的輸出結(jié)果。 #include <iostream.h> class goods private: static int totalWeight; 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 getTot
21、al() return totalWeight; ; int goods:totalWeight=0; void main() int w; cout<< "The initial weight of goods:"<<goods:getTotal()<<endl; cin>>w; / 輸入 25 goods g1(w); cin>>w; / 輸入 60 goods g2(w); cout<< "The total weight of goods:" <<goods:ge
22、tTotal()<<endl; 輸出為: The initial weight of goods:0 The total weight of goods:8511、# include<iostream.h> 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(
23、)cout<< " This is class C printing. " <<endl; ; void show(A&a) a.print( ); void main( ) A a;B b;C c; show(a);show(b);show(c); 輸出為:This is class A printing.This is class C printing.12、# include<iostream.h> class Apublic: A( )a=0; A(int i)a=i; void Print( )cout<<a
24、<< " , " ; int Geta( )return a; private:int a; ; class B:public Apublic:B( )b=0; B(int i,int j,int k); void 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 ma
25、in( ) 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 Testprivate: 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)
26、r.a=125; cout<< "Result3=" <<r.a<<endl; void main( ) cout<< "Result1=" <<Test:func( )<<endl; Test a; cout<< "Result2=" <<a.func( )<<endl; a.sfunc(a); 輸出為:Result1 =200Result2=201Result3=12514、# include<iostream.h&g
27、t; class Locationint 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( )return Y; ; void Location:init(int initX,int initY) X=initX; Y=initY; void main( ) Location A,B; A.init( ); cout<<A.valueX( )<<" "
28、<<A.valueY( )<<endl; A.valueX(5); cout<<A.valueX( )<<" "<<A.valueY( )<<endl; B.init(6,2); B.valueY(4); cout<<B.valueX( )<<" "<<B.valueY( )<<endl; 輸出為:0 05 06 415、#include <iostream.h> class A public: A( ) virtual v
29、oid func( )cout<<"Destructor A" <<endl; A( ) func(); ; class B:public A public: B( ) void func()cout<<"Destructor B" <<endl; B( ) func(); ; void main( ) B b; A&a=b; 輸出為:Destructor B Destructor A16、 include <iostream.h> class My Class public: int
30、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<<endl; my1.set(:number); cout<<my1.number; 輸出為:5 10 3 17、#include<iostream.h>
31、class AAA int 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 AAA(1,2); a2=new AAA(5,6); a1->print(); a2->print(); delete a1; delete
32、 a2; 輸出為:CC1,25,6 DD 18、#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<<"Dn" ; class AAA:public b2,public b1 public: AAA(in
33、t x,int y):b1(y),b2(x) cout<<"En" AAA()cout<<"Fn" ;void main() AAA obj(5,7); 輸出為:5->C7->AEFBD19、#include<iostream.h> class FATHER public: virtual void answer( ) cout<<"I am fathern" ; class DAUGHTER:public FATHER public: virtual void answer
34、( ) cout<<"I am daughtern"class SON:public FATHERpublic: void answer()cout<<"I am sonn" ;void main() FATHER father; DAUGHTER daughter; SON son; FATHER *who; who=&father; who->answer(); who=&daughter; who->answer(); who=&son; who->answer(); 輸出為:I a
35、m fatherI am daughterI am son20、#include <iostream.h>class Basepublic: Base()cout<<"Generate Base"<<endl; Base()cout<<"Destroy Base"<<endl; ; class child:public Base public:child():Base()cout<<"Generate child"<<endl; child()cout
36、<<"Destroy child"<<endl; ; void main()child c2;輸出為:Generate BaseGenerate childDestroy childDestroy Base21、#include <iostream.h>#include <string.h>class StudentIDpublic: StudentID(int id=0) value=id; cout <<"Assigning student id " <<value <&l
37、t;endl; StudentID() cout <<"Destructing id " <<value <<endl; protected: int value;class Studentpublic: Student(char* pName="noName",int ssID=0) cout <<"Constructing student " <<pName <<endl; strncpy(name,pName,sizeof(name); namesizeof
38、(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 id 022、#include <iostream.h>class MyClasspublic: MyClass(); MyC
39、lass(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 <<"Display a number
40、: " <<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: 20Destructing.Destructing.23
41、、#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' Student(Student&am
42、p; s) cout <<"Constructing copy of " << <<endl; strcpy(name,"copy of "); strcat(name,); Student() cout <<"Destructing " <<name <<endl; ;class Tutorprotected: 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
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 石河子大學(xué)《水資源規(guī)劃及利用》2023-2024學(xué)年第一學(xué)期期末試卷
- 石河子大學(xué)《流行病學(xué)》2023-2024學(xué)年第一學(xué)期期末試卷
- 石河子大學(xué)《教育電視節(jié)目編導(dǎo)與制作》2022-2023學(xué)年第一學(xué)期期末試卷
- 沈陽理工大學(xué)《陶瓷》2022-2023學(xué)年第一學(xué)期期末試卷
- 沈陽理工大學(xué)《面向?qū)ο蟪绦蛟O(shè)計(jì)及應(yīng)用》2022-2023學(xué)年期末試卷
- 沈陽理工大學(xué)《機(jī)械工程控制基礎(chǔ)》2023-2024學(xué)年期末試卷
- 沈陽理工大學(xué)《編譯原理》2022-2023學(xué)年第一學(xué)期期末試卷
- 國(guó)企合同工工資標(biāo)準(zhǔn)
- 合同 確認(rèn)書 備忘錄
- 合同法案例教程
- 《員工心理健康》課件
- 微型站消防站課件培訓(xùn)
- 北京市豐臺(tái)區(qū)2023-2024學(xué)年七年級(jí)上學(xué)期期末數(shù)學(xué)試題
- 儲(chǔ)氣罐保養(yǎng)記錄表
- 計(jì)算思維與程序設(shè)計(jì)
- 主題漫展策劃方案
- 小學(xué)生自主學(xué)習(xí)能力培養(yǎng)及教師指導(dǎo)策略
- 財(cái)務(wù)管理的數(shù)字化轉(zhuǎn)型實(shí)施方案
- 線上廚藝大賽投票方案
- 家長(zhǎng)課程:怎樣提升孩子的自護(hù)技能
- 德語專業(yè)大學(xué)生職業(yè)生涯規(guī)劃書
評(píng)論
0/150
提交評(píng)論