面向?qū)ο蟪绦蛟O(shè)計離線作業(yè)答案_第1頁
面向?qū)ο蟪绦蛟O(shè)計離線作業(yè)答案_第2頁
面向?qū)ο蟪绦蛟O(shè)計離線作業(yè)答案_第3頁
面向?qū)ο蟪绦蛟O(shè)計離線作業(yè)答案_第4頁
面向?qū)ο蟪绦蛟O(shè)計離線作業(yè)答案_第5頁
已閱讀5頁,還剩56頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、浙江大學(xué)遠程教育學(xué)院面向?qū)ο蟪绦蛟O(shè)計課程作業(yè)答案第2章【2.3】 測試下面的注釋(它在C+風(fēng)格的單行注釋中套入了類似于C的注釋)是否有效。/this is a strange /*way to do a comment*/答:有效【2.4】 以下這個簡短的C+程序不可能編譯通過,為什么? #include<iostream>using namespace std;int main()int a,b,c; cout<<"Enter two numbers:" cin>>a>>b; c=sum(a,b); cout<<

2、"sum is:"<<c; return 0;sum(int a,int b) return a+b;答:不能sum使用前必須聲明【2.5】 回答問題。(1) 以下兩個函數(shù)原型是否等價:float fun(int a,float b,char *c);float fun(int,float,char * );等價(2) 以下兩個函數(shù)的第一行是否等價:float fun(int a,float b,char * c);float fun(int,float,char * );答:等價【2.6】 下列語句中錯誤的是(D )。Aint *p=new int(10);

3、Bint *p=new int10;Cint *p=new int; Dint *p=new int40(0);【2.7】 假設(shè)已經(jīng)有定義“const char * const name="chen"”下面的語句中正確的是( D )。A. name3='a' B. name="lin"C. name=new char5; D. cout<<name3;【2.8】 假設(shè)已經(jīng)有定義“char * const name="chen"”下面的語句中正確的是( A )。A. name3='q' B.

4、name="lin"C. name=new char5; D. name=new char('q');【2.9】 假設(shè)已經(jīng)有定義“const char * name="chen"”下面的語句中錯誤的是(A )。A. name3='q' B. name="lin"C. name=new char5; D. name=new char('q');【2.10】重載函數(shù)在調(diào)用時選擇的依據(jù)中,( B )是錯誤的。 A函數(shù)名字 B函數(shù)的返回類型 C參數(shù)個數(shù) D參數(shù)的類型【2.11】 在( A )情況

5、下適宜采用內(nèi)聯(lián)函數(shù)。 A函數(shù)代碼小,頻繁調(diào)用 B函數(shù)代碼多,頻繁調(diào)用 C函數(shù)體含有遞歸語句 D. 函數(shù)體含有循環(huán)語句【2.12】 下列描述中,( C)是錯誤的。 A. 內(nèi)聯(lián)函數(shù)主要解決程序的運行效率問題 B. 內(nèi)聯(lián)函數(shù)的定義必須出現(xiàn)在內(nèi)聯(lián)函數(shù)第一次被調(diào)用之前 C. 內(nèi)聯(lián)函數(shù)中可以包括各種語句 D. 對內(nèi)聯(lián)函數(shù)不可以進行異常接口聲明【2.13】 在C+中,關(guān)于下列設(shè)置默認參數(shù)值的描述中,( B )是正確的。 A不允許設(shè)置默認參數(shù)值 B在指定了默認值的參數(shù)右邊,不能出現(xiàn)沒有指定默認值的參數(shù) C只能在函數(shù)的定義性聲明中指定參數(shù)的默認值D. 設(shè)置默認參數(shù)值時,必須全部都設(shè)置 【2.14】 下面的類型聲

6、明中正確是( D )。 A. int &a4; B. int &*p; C.int &&q; D. int i,*p=&i;【2.15】 下面有關(guān)重載函數(shù)的說法中正確的是(C )。 A重載函數(shù)必須具有不同的返回值類型 B重載函數(shù)形參個數(shù)必須不同 C. 重載函數(shù)必須有不同的形參列表 D重載函數(shù)名可以不同 【2.16】 關(guān)于new運算符的下列描述中,( D )是錯誤的。 A,它可以用來動態(tài)創(chuàng)建對象和對象數(shù)組 B. 使用它創(chuàng)建的對象或?qū)ο髷?shù)組可以使用運算符delete刪除 C. 使用它創(chuàng)建對象時要調(diào)用構(gòu)造函數(shù) D. 使用它創(chuàng)建對象數(shù)組時必須指定初始值 【2.1

7、7】 關(guān)于delete運算符的下列描述中,(C )是錯誤的。 A它必須用于new返回的指針 B. 使用它刪除對象時要調(diào)用析構(gòu)函數(shù) C. 對一個指針可以使用多次該運算符 D指針名前只有一對方括號符號,不管所刪除數(shù)組的維數(shù)【2.18】 寫出下列程序的運行結(jié)果。 #include<iostream>using namespace std;int i=15;int main() int i; i=100; :i=i+1; cout<<:i<<endl; return 0;答:101【2.19】 寫出下列程序的運行結(jié)果。#include<iostream>

8、using namespace std;void f(int &m,int n) int temp;temp=m;m=n;n=temp;int main() int a=5,b=10; f(a,b); cout<<a<<" "<<b<<endl; return 0;答:10 10【2.20】 分析下面程序的輸出結(jié)果。#include<iostream>using namespace std;int &f(int &i) i+=10; return i;int main() int k=0;i

9、nt &m=f(k);cout<<k<<endl;m=20;cout<<k<<endl;return 0;答:1020【2.21】 舉例說明可以使用const替代define以消除define的不安全性。答:例如#include <iostream.h> #define A 2+4 #define B A*3 void main() cout<<B<<endl; 上面程序的運行結(jié)果是14而不是18但很容易被認為是18。用const替代#define就能得到正確結(jié)果從而消除了#define的不安全性。 #

10、include <iostream.h> const A=2+4; const B=A*3; void main() cout<<B<<endl; 運行結(jié)果為18?!?.22】 編寫一個C+風(fēng)格的程序,用動態(tài)分配空間的方法計算Fibonacci 數(shù)列的前20項,并存儲到動態(tài)分配的空間中。#include<iostream> using namespace std; int main() int i; int *p; p=new int20; p0=0; p1=1;cout<<*p<<" "<<

11、;*(p+1)<<" " for(i=2;i<20;i+) pi=pi-2+pi-1; cout<<*(p+i)<<" " delete p; return 0; 【2.23】 編寫一個C+風(fēng)格的程序,建立一個被稱為sroot()的函數(shù),返回其參數(shù)的二次方根。重載sroot()3次,讓它返回整數(shù)、長整數(shù)與雙精度數(shù)的二次方根(計算二次方根時,可以使用標準庫函數(shù)sqrt()。#include <iostream.h>#include <math.h> int sroot(int x) retu

12、rn (int)sqrt(x); float sroot(float x) return (float)sqrt(x); double sroot(double x) return (double)sqrt(x); void main() / test int i = 4; int resultI = sroot(i); float j = 4.0; float resultJ =sroot(j); double k = 4.0000; double resultK = sroot(k); 【2.24】 編寫一個C+風(fēng)格的程序,解決百錢問題:將一元人民幣兌換成1、2、5分的硬幣,有多少種換法?

13、#include <iostream> using namespace std; int main() int i,j,k; int sum=0; for(i=0;i<=100;i+) for(j=0;j<=50;j+) for(k=0;k<=20;k+) if(i*1+2*j+5*k=100) sum+; cout<<"總數(shù)為"<<sum<<endl;【2.25】 編寫一個C+風(fēng)格的程序,輸入兩個整數(shù),將它們按由小到大的順序輸出。要求使用變量的引用。#include<iostream> usin

14、g namespace std; void swap(int &x,int &y) int temp=x; x=y; y=temp; int main() int a,b; cin>>a>>b; if(a>b) swap(a,b); cout<<a<<b; return 0; 第三章:【3.7】 在下面有關(guān)對構(gòu)造函數(shù)的描述中,正確的是( B )。 A構(gòu)造函數(shù)可以帶有返回值 B構(gòu)造函數(shù)的名字與類名完全相同 C構(gòu)造函數(shù)必須帶有參數(shù) D構(gòu)造函數(shù)必須定義,不能默認【3.8】 在聲明類時,下面的說法正確的是(C )。 A可以在類的聲明

15、中給數(shù)據(jù)成員賦初值 B數(shù)據(jù)成員的數(shù)據(jù)類型可以是register Cprivate、public、protected可以按任意順序出現(xiàn) D沒有用private、public、protected定義的數(shù)據(jù)成員是公有成員【3.9】 在下面有關(guān)析構(gòu)函數(shù)特征的描述中,正確的是( C )。 A一個類中可以定義多個析構(gòu)函數(shù) B. 析構(gòu)函數(shù)名與類名完全相同 C. 析構(gòu)函數(shù)不能指定返回類型 D. 析構(gòu)函數(shù)可以有一個或多個參數(shù)【3.10】 構(gòu)造函數(shù)是在( B )時被執(zhí)行的。 A程序編譯 B. 創(chuàng)建對象 C. 創(chuàng)建類 D程序裝人內(nèi)存【3.11】 在下面有關(guān)靜態(tài)成員函數(shù)的描述中,正確的是( B )。 A在靜態(tài)成員函數(shù)

16、中可以使用this指針 B. 在建立對象前,就可以為靜態(tài)數(shù)據(jù)成員賦值 C. 靜態(tài)成員函數(shù)在類外定義時,要用static前綴 D. 靜態(tài)成員函數(shù)只能在類外定義【3.12】 在下面有關(guān)友元函數(shù)的描述中,正確的說法是A )。 A友元函數(shù)是獨立于當前類的外部函數(shù) B. 一個友元函數(shù)不能同時定義為兩個類的友元函數(shù) C. 友元函數(shù)必須在類的外部定義 D. 在外部定義友元函數(shù)時,必須加關(guān)鍵字friend【3.13】 友元的作用之一是(A)。 A提高程序的運行效率 B加強類的封裝性 C. 實現(xiàn)數(shù)據(jù)的隱藏性 D. 增加成員函數(shù)的種類【3.14】 以下程序運行的結(jié)果是( B )。#include<iostr

17、eam>using namespace std;class Bpublic:B()B(int i,int j) x=i; y=j;void printb()cout<<x<<","<<y<<endl;private:int x,y;class A public: A() A(int I,int j); void printa(); private: B c;A:A(int i,int j):c(i,j)void A:printa()c.printb();int main() A a(7,8);a.printa();ret

18、urn 0;A8,9 B. 7,8 C. 5,6 D. 9,10【3.15】 以下程序的運行結(jié)果是(A )。 #include<iostream>using namespace std;class A public:void set(int i,int j) x=i; y=j;int get_y()return y;private:int x,y;class box public: void set(int l,int w,int s,int p) length=1; width=w; label.set(s,p); int get_area() return length*wid

19、th; private: int length,width; A label;int main() box b;b.set(4,6,1,20);cout<<b.get_area()<<endl;return 0;A24 B. 4 C. 20 D. 6【3.16】 以下程序的運行結(jié)果是( B )。 #include<iostream>using namespace std;class Sample public: Sample(int i,int j) x=i; y=j; void disp() cout<<"disp1"<

20、;<endl; void disp() const cout<<"disp2"<<endl; private: int x,y;int main()const Sample a(1,2); a.disp(); return 0;A. disp1 B.disp2 C. disp1 disp2 D. 程序編譯出錯【3.17】 以下程序的運行結(jié)果是( B )。 #include<iostream>using namespace std;class R public: R(int r1,int r2) R1=r1; R2=r2; void

21、print(); void print() const; private: int R1,R2;void R:print()cout<<R1<<","<<R2<<endl;void R:print() constcout<<R1<<","<<R2<<endl;int main() R a(6,8);const R b(56,88);b.print();return 0; A. 6,8 B. 56,88 C. 0,0 D. 8,6【3.18】 寫出下面程序的運行

22、結(jié)果。 #include<iostream>using namespace std;class toy public: toy(int q,int p) quan=q; price=p; int get_quan() return quan; int get_price() return price; private:int quan,price;int main() toy op32= toy(10,20),toy(30,48), toy(50,68),toy(70,80), toy(90,16),toy(11,120),;for(int i=0;i<3;i+)cout&l

23、t;<opi0.get_quan()<<"," cout<<opi0.get_price()<<"n" cout<<opi1.get_quan()<<"," cout<<opi1.get_price()<<"n"cout<<endl;return 0;10,2030,4850,6870,8090,1611,120【3.19】 寫出下面程序的運行結(jié)果。 #include<iostream>using na

24、mespace std;class example public: example(int n) i=n; cout<<"Constructingn" example() cout<<"Destructingn"int get_i() return i;private:int i;int sqr_it(example o)return o.get_i()*o.get_i();int main() example x(10);cout<<x.get_i()<<endl;cout<<sqr_it(x

25、)<<endl;return 0;10100DestructingDestructing【3.20】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class aClass public: aClass() total+; aClass()total-;int gettotal() return total;private:static int total;int aClass:total=0;int main()aClass o1,o2,o3; cout<<o1.gettotal()<<&

26、quot;objects in existencen" aClass *p; p=new aClass; if(!p) cout<<"Allocation errorn" return 1; cout<<o1.gettotal(); cout<<"objects in existence after allocationn" delete p; cout<<o1.gettotal(); cout<<"objects in existence after deletionn&q

27、uot; return 0;3objects in existence4objects in existence after allocation3objects in existence after deletion【3.21】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class test public: test(); test();private:int i;test:test() i=25; for(int ctr=0;ctr<10;ctr+) cout<<"Counting at&q

28、uot;<<ctr<<"n" test anObject;int main() return 0;Counting at0 Counting at1 Counting at2 Counting at3 Counting at4 Counting at5 Counting at6 Counting at7 Counting at8 Counting at9【3.22】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class Aint a,b;public:A()a=0; b=0; c

29、out<<"Default constructor called.n"A(int i,int j)a=i; b=j; cout<<"Constructor: a="<<a<<",b="<<b<<endl;int main()A a3; A b3=A(1,2),A(3,4),A(5,6); return 0; Default constructor called. Default constructor called. Default constructor cal

30、led. Constructor: a=1,b=2 Constructor: a=3,b=4 Constructor: a=5,b=6【3.23】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class Test private: int val; public: Test() cout<<"default."<<endl; Test(int n) val=n; cout<<"Con."<<endl; Test(const Test&am

31、p; t) val=t.val; cout<<"Copy con."<<endl; ;int main()Test t1(6); Test t2=t1; Test t3; t3=t1; return 0;Con. Copy con. Default.【3.24】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class N private: int A; static int B; public: N(int a) A=a; B+=a; static void f1(N m);voi

32、d N:f1(N m)cout<<"A="<<m.A<<endl;cout<<"B="<<B<<endl;int N:B=0;int main() N P(5),Q(9); N:f1(P); N:f1(Q); return 0;A=5 B=14 A=9B=14【3.25】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class Mint x,y;public:M() x=y=0;M(int i,int j)x=i;

33、 y=j;void copy(M*m);void setxy(int i,int j)x=i; y=j;void print()cout<<x<<","<<y<<endl;void M:copy(M*m)x=m->x; y=m->y;void fun(M m1,M*m2)m1.setxy(12,15); m2->setxy(22,25);int main()M p(5,7),q; q.copy(&p); fun(p,&q); p.print(); q.print(); return 0;5,7

34、22,25【3.26】 寫出下面程序的運行結(jié)果。 #include<iostream>using namespace std;class Mint A;static int B;public:M(int a)A=a; B+=a; cout<<"Constructing"<<endl;static void f1(M m);M()cout<<"Destructing n"void M:f1(M m)cout<<"A="<<m.A<<endl; cout&

35、lt;<"B="<<B<<endl;int M:B=0;int main()M p(5),Q(10); M:f1(p); M:f1(Q); return 0;Constructing Constructing A=5 B=15 Destructing A=10 B=15 Destructing Destructing Destructing【3.27】 指出下列程序中的錯誤,并說明為什么。 #include<iostream>using namespace std;class Studentpublic:void printStu(

36、);private:char name10;int age;float aver;int main() Student p1,p2,p3; p1.age=30; . . . return 0; age 為private不能付值【3.28】 指出下列程序中的錯誤,并說明為什么。#include<iostream>using namespace std;class Studentint sno;int age; void printStu(); void setSno(int d);void printStu(); cout<<"nSno is"<

37、<sno<<"," cout<<"age is"<<age<<"."<<endl;void setSno(int s) sno=s;void setAge(int a) age=a;int main() Student lin; lin.setSno(20021); lin.setAge(20); lin.printStu(); 'class Student' 不存在一個叫 'setAge' 的方式【3.29】 指出下列程序中的錯誤,并說

38、明為什么。#include<iostream>using namespace std;class Pointpublic:int x,y;private:Piont()x=1;y=2;int main()Point cpoint; cpoint.x=2; return 0;構(gòu)筑函數(shù)錯誤【3.30】 下面是一個計算器類的定義,請完成該類成員函數(shù)的實現(xiàn)。class counterpublic:counter(int number); void increment(); /給原值加1void decrement(); /給原值減1int getvalue(); /取得計數(shù)器值int pr

39、int(); /顯示計數(shù)private:int value;counter:counter(int number) value = number; void counter:increment(int n) value += n; void counter:decrement() value -; int counter:getvalue() return value; void counter:print() printf("%dn",getvalue(); int main() int a,b; scanf("%d%d",&a,&b)

40、; counter ct(a); ct.increment(b); ct.decrement(); ct.print(); return 0; 【3.31】 根據(jù)注釋語句的提示,實現(xiàn)類Date的成員函數(shù)。#include<iostream>using namespace std;class Date public: void printDate(); /顯示日期 void setDay(int d); /設(shè)置日的值 void setMonth(int m); /設(shè)置月的值 void setYear(int y); /設(shè)置年的值 private: int day,month,year

41、;int Date:setyear(int y) year=y; return year; int Date:setmonth(int m) month=m; return month; int Date:setday(int d) day=d; return day;VoidDate:printdate() cout<<year<<"."<<month<<"."<<day<<endl;int main() Date testDay; testDay.setDay(5); testD

42、ay.setMonth(10); testDay.setYear(2003); testDay.printDate(); return 0;【3.32】 建立類cylinder,cylinder的構(gòu)造函數(shù)被傳遞了兩個double值,分別表示圓柱體的半徑和高度。用類cylinder計算圓柱體的體積,并存儲在一個double變量中。在類cylinder中包含一個成員函數(shù)vol,用來顯示每個cylinder對象的體積。#include<iostream>using namespace std; #define PI 3.14159 class Cylinder public: Cylin

43、der(doubleradius,doubleheight):radius(radius),height(height),volume(PI*radius*radius*height) total_vol += volume; double vol()const; friend void getParas(double& radius,double&height,double&vol,double& total_vol,const Cylinder& obj); private: double radius; /radius double height;

44、 /height double volume;/volume of current object static double total_vol;/total volume ; double Cylinder:total_vol = 0; double Cylinder:vol()const return this->volume; void getParas(double& radius,double& height,double& vol,double& total_vol,const Cylinder& obj) /get private m

45、ember of Cylinder radius = obj.radius; height = obj.height; vol = obj.volume; total_vol = obj.total_vol; int main() double rd = 0,hg = 0,vol = 0,total_vol = 0; Cylinder cld_1(2,3.5),cld_2(5.2,7),cld_3(3.9,4.0);/3 objects getParas(rd,hg,vol,total_vol,cld_3); cout<<"Cylinder 3:"<<

46、;endl; cout<<"radius:"<<rd<<endl; cout<<"height:"<<hg<<endl; cout<<"volume:"<<vol<<endl; cout<<"Total Volume:"<<total_vol<<endl; system("pause"); return 0;【3.33】 構(gòu)建一個類book,其中含有兩個

47、私有數(shù)據(jù)成員qu和price,將qu初始化為15,將price初始化為qu的10倍,建立一個有5個元素的數(shù)組對象。顯示每個對象數(shù)組元素的 qu*price值。#include <iostream.h> class book int qu, price; public: book(int q) qu=q; price=10*q; int get_qu() return qu; int get_price() return price; ; void main( ) book obj5=1,2,3,4,5; for(int i=0;i<5;i+) cout<<&quo

48、t;數(shù)組對象"<<i+1<<"的qu*price值 為:"<<obji.get_qu()*obji.get_price()<<endl; 【3.34】 修改習(xí)題3.33,通過對象指針訪問對象數(shù)組,使程序以相反的順序顯示每個對象數(shù)組元素的 qu*price值。#include <iostream.h> class book int qu, price; public: book(int q) qu=q; price=10*q; int get_qu() return qu; int get_price()

49、return price; ; void main( ) book obj5=1,2,3,4,5; book *p; p=&obj4; for(int i=0;i<5;i+) cout<<"數(shù)組對象"<<i+1<<"的qu*price值 為:"<<p->get_qu()*p->get_price()<<endl; p-; 【3.35】 構(gòu)建一個類 Stock,含字符數(shù)組stockcode及整型數(shù)據(jù)成員quan、雙精度型數(shù)據(jù)成員price。構(gòu)造函數(shù)含3個參數(shù):字符數(shù)組na

50、及q、p。當定義Stock的類對象時,將對象的第1個字符串參數(shù)賦給數(shù)據(jù)成員stockcode,第2和第3個參數(shù)分別賦給quan、price。未設(shè)置第2和第3個參數(shù)時,quan的值為1000,price的值為8.98。成員函數(shù)print沒有形參,需使用this指針,顯示對象數(shù)據(jù)成員的內(nèi)容。假設(shè)類Stock第1個對象的三個參數(shù)分別為:“600001”,3000和5.67,第2個對象的第1個數(shù)據(jù)成員的值是“600001”,第2和第3個數(shù)據(jù)成員的值取默認值。要求編寫程序分別顯示這兩個對象數(shù)據(jù)成員的值。#include <iostream> / const unsigned MAX_LEN

51、= 256; class Stock public: / 這里用的初始化形參列表 Stock(char na, int q=1000, double p=8.98) : stockcode(na), quan(q), price(p) void print(void) std:cout << this->stockcode << " " << quan << " " << price << std:endl; private: char *stockcode; / 或改為 char stockcodeMAX_LEN; / 其中 MAX_LEN 定義在前為一常量: const unsigned MAX_LEN = 256; int quan; double price; ; / main function int main(int argc, char *

溫馨提示

  • 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)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論