《面向對象方法與C++》題庫及答案_第1頁
《面向對象方法與C++》題庫及答案_第2頁
《面向對象方法與C++》題庫及答案_第3頁
《面向對象方法與C++》題庫及答案_第4頁
《面向對象方法與C++》題庫及答案_第5頁
已閱讀5頁,還剩7頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、面向對象方法與C+題庫及答案、填空題1 .若char *string= test ”;則如果要輸出指針值,正確的語句是 。2 .在重載“口 ”運算符時,必須使用 函數重載。3 .當用public繼承時,基類的public成員成為派生類的 成員,基類的protected 成員成為派生類的 成員。4 .可以賦給指針的唯一整數是 。5 .在重載“=”運算符時,必須使用 函數重載。6 .以下程序:int c=10;cout.flags(ios:hex|ios:showbase);cout XXXX;XXXX必須是一個變量名,而不能是一個任意表達式。()2 .以下聲明將不會導致編譯錯誤。()enum G

2、radeType A, B, C, D, E, F;3 .在C+理,派生類的構造函數先于其基類的構造函數執行。()4 . C+中的所有函數都是傳值調用。()5 .在類time中聲明如下的函數原型:void time();()6 .以下程序:class CA public:virtual void dis( )=0; ;class CB:public CA public:void set(int m) k=m; protected:int k;;void main() CB b; 是正確的。()7 .函數模板能夠定義一個在不同數據類型基礎上完成同一任務的函數。()8 .以下語句:char *st

3、ring= test ;delete string;是正確的 。()9 .一個有指針數據成員的類必須要包含的成員函數有:初始化構造函數,析構函數,拷貝構造函數和賦值運算符函數。()10 .抽象基類中的所有虛函數都要聲明為純虛函數。()11 .以下語句:char *string=new char10;strcpy(string, test );/ delete string;是正確的 。()12 .枚舉類型變量的值可以直接進行輸入、輸出。() 三、讀程題1. .下面程序在執行過程中,若順序輸入十個整數:1 2 3 4 5 6 7 8 9 10,其結果是什么。#include class stac

4、k;class nodeint data;node *prev;public:node(int d,node *n) data=d; prev=n;friend class stack;;class stacknode *top;public:stack( ) top=0;void push(int i)node *n=new node(i,top); top=n; int pop( )node *t=top;if(top)top=top-prev; int c=t-data;delete t; return c;return 0; ;void main( )int c; stack s;fo

5、r(int i=0;ic; s.push(c); for(i=0;i10;i+)cout s.pop( ) coutendl; 2.#include #include class Increment public:Increment(int=0,int=1);void addIncrement() count+=increment;void print( ) const;private:int count;const int increment; ;Increment:Increment(int c,int i):increment(i) count=c; void Increment:pri

6、nt( ) constcoutcount=setw(8)countendl;coutincrement=incrementendl; void main( )Increment object(65,7);coutbefore:endl;object.print();for(int j=0;j3;j+)object.addIncrement();coutafter:j+1endl;object.print( ); 3.#include class pointstatic int count ;float xcoord , ycoord ;public:point(float x=0 float

7、y=0 ) xcoord=x ;ycoord=y ;count+ ; static int getcount() return count ;point() count- ; ;int point:count=0;void main( )coutpoint:getcount( ); point *p ,a(32.98,-4.71) ,b ,c; coutpoint:getcount( ); coutb.getcount( );p=new point100;coutpoint:getcount( );delete p;coutpoint:getcount( )endl; 4.#include c

8、lass Teststatic int count;public:Test( ) +count;Test( ) -count; static int getCount( ) return count; ;int Test:count=0;void main( )coutTest:getCount();Test t,tab5,*p;couttTest:getCount();p=new Test10;couttTest:getCount();delete p;couttTest:getCount( )endl; 5.#include #include template class vector T

9、 *v; int sz;public:vector(int s=100) v=new Tsz=s; vector(const vector&vv)sz=vv.sz; v=new Tsz;vector&operator=(const vector&vv)if(&vv!=this)delete v;sz=vv.sz;v=new Tsz;for(int i=0;isz;i+) vi=vv.vi; return *this;vector() delete v; T & operator (int i) if(i=sz) cerrErrorn;exit(1); return vi; ;void main

10、( )vector v(10),u(v);for(int i=0;i10;i+)vi=3*i+1;u=v;for( i=0;i=10;i+) coutui )和插入符( )以支持I/O 操作。在 main函數中定義學生對象數組,對于學生對象數組從磁盤文件 “student.txt ”進行輸入,最后以學號n為參數在數組中查找學號為n的學生,并顯示該生的全部信息。#include #include #include class student 2; char name20,sex; int age;float score;public:student( ) student(int nu,char

11、* na,char se,int ag,3 sc) _num=nu; strcpy(name,na);sex=se; age=ag; score=sc; int getn( )return num;friend istream&operator(istream& s,student& st)st.sexst.agest.score;return 4; friend ostream&operator(5 s,const student& st) sst.num 6 st.sex st.age st.scores_9_;_int n,flag=1;coutEnter n

12、:;10;for(int j=0;ji & flag;j+)if(sj.getn( )=n) flag=0;cout找到了 :sj;if(flag=1) cout沒找到。n;2 .請完成以下復數類的設計,復數類對象的創建及使用。#include #include #include class complexdouble rpart,ipart;double abs( )const;double norm( )const;public:complex(double r=0,double i=0)2; ipart=i; complex operator-();friend complex oper

13、ator+(const complex&,const complex&);friend complex operator-(const complex&,const complex&);friend complex operator*(const complex&,const complex&);friend complex operator/(const complex&,const complex&);friend 3 operator(const complex&,const complex&);friend int operator=(const complex&,const comp

14、lex&);friend int operator (const complex&,const complex&);friend int operator(istream& si,complex& c)sic.rpartc.ipart;return si;friend ostream& operator( ostream&so,const complex&c) so(c.rpart,c.ipart)n;4; ;double complex:abs( )constreturn sqrt(rpart*rpart +5);_double complex:norm( )constreturn (rpa

15、rt*rpart+ipart*ipart);complex complex: operator-( )return complex(-rpart,-ipart);complex operator+(const complex&c1,const complex&c2)return complex(c1.rpart+c2.rpart),c1.ipart+c2.ipart);complex operator-(const complex&c1,const complex&c2)return complex(6 ,c1.ipart-c2.ipart);complex operator*(const c

16、omplex&c1,const complex&c2)return complex(c1.rpart*c2.rpart-c1.ipart*c2.ipart,c1.rpart*c2.ipart+c1.ipart*c2.rpart); complex operator/(const complex&c1,const complex&c2)complex res;double d=c2.norm();if(d!=0)res.rpart=(c1.rpart*c2.rpart+c1.ipart*c2.ipart)/d;res.ipart=(c1.ipart*c2.rpart-c1.rpart*c2.ip

17、art)/d; else cerr(const complex&c1,const complex&c2)return c1.abs( )c2.abs( );int operator=(const complex&c1,const complex&c2)return 8; int operator(const complex&c1,const complex&c2)return c1.abs( )c2.abs( ); int operator=(const complex&c1,const complex&c2)return c1.abs( )c3; coutc3;c3=-c1*2+c2/c3*

18、c1-6;coutc2) coutc1;else coutc1c2c3;coutc1c2c3; 3 .有一家醫院的門診記錄如下所示,已知該記錄存放在一個patient.dat ”文件中,請編寫一個程序,幫助醫生計算一下每一位病人平均血壓。病人的編號病人血壓的測量次數病人血壓各次測量的結果10015100 120 90 110 10010022100 1201003390 120 1301004480 70 90 10010053120 135 1104 .請完成以下集合類的設計,集合類對象的創建及使用。#include const int maxcard=20;class setint ele

19、msmaxcard,card;public:set( ) card=1;friendbool operator&(int,set);friendbool operator=(set,set);friend 2operator!=(set,set);friendsetoperator+(set,set);friendsetoperator+(set,int);friendsetoperator-(set,int);3 setoperator*(set,set);friendbool operator(set,set);friendbool operator=(set,set);friendost

20、ream& operator(ostream&out,constset&s) outendl;for(int i=0;is.card;i+)outs.elemsi;return 4; _;bool operator&(int e ,set s)for(int i=0;is.card;i+)if(s.elemsi=e)return true;return 5;bool operator=(set s1,set s2)if(s1.card!=s2.card) return false;for(int i=0;is1.card;6)_if(!(s1.elemsi&s2) return false;r

21、eturn true;bool operator!=(set s1,set s2) return !(s1=s2)?true:false;set operator+(set s,int e) set res=s;if(s.cardmaxcard)if(!(e&s)res.elemsres.card+=e;return res;set operator+(set s1,set s2) set res=s1;for(int i=0;is2.card;i+)res=res+s2.elemsi;return res;set operator-(set s,int e) set res=s;if(!(e

22、&s)return res;for(int i=0;is.card;i+)if(s.elemsi=e)for(;is.card-1;i+) res.elemsi=res.elemsi+1;7;return res;set operator*(set s1,set s2)8;for(int i=0;is1.card;i+)for(int j=0;js2.card;j+)if(s1.elemsi=s2.elemsj) res.elemsres.card+=9; break; return res;bool operators2.card)return false;for(int i=0;is1.c

23、ard;i+)if(!(s1.elemsi&s2)return false;return true;bool operator(set s1,set s2)return s1.cards2.card&s1=s2?true:false;void main( ) set 10 ;for(int i=0;i100;i+) s1=s1+i;couts1;if(s1!=s2) coutnTruen; else coutnFalsen;s2=s1; couts2;if(s1=s2) coutnTruen; else coutnFalsen; for(i=0;i20;i+) s1=s1-i;couts1;s

24、3=s1*s2; couts3;if(s1s2) coutnTruen; else coutnFalsen;if(s1+s2=s3)coutnTruen; else coutnFalsen; 5.完成以下程序。#include #include double function(double x)return 4/(1+x*x); class inte_algo protected:double a,b,n,h,sum;public:inte_algo(double left,double right,double steps)a=1,b=right,n=steps,h=(b-a)/n,sum=

25、0;virtual void integrate( )=2; ; _ class rectangle:3 public:rectangle(double left,double right,double steps): inte_algo(left,right,steps) 4; ;void rectangle:integrate( ) double a1=a;for(int i=0;in;i+)sum+=function(a1),a1+=h;sum*=h;coutSum=sumendl; class ladder: public inte_algo public:ladder(double

26、left,double right,double steps): inte algo(left,right,steps) void integrate( ); ;void ladder:integrate( ) double a1=a;sum=(function(a)+function(b)/2;for(int i=1;in;i+)a1+=h,sum+=function(a1);sum*=h;coutSum=sumendl; class 5 : public inte_algo public:simpson(double left,double right,double steps):6 vo

27、id integrate( ); ;void simpson:integrate( ) sum=function(a)+function(b);double s=1,a1=a;for(int i=1;in;i+)a1+=h, sum+=(3+s)*function(a1),s=-s;sum*=h/=3;_7_; void main( ) rectangle r(0,1,10); ladder l(0,1,10);simpson 8(0,1,10); inte_algo 9=&r;coutintegrate( ); p=&l;p-integrate( ); p=&s;10; 第12頁共14頁面向對象方法與C+作業參

溫馨提示

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

評論

0/150

提交評論