C程序設(shè)計(jì)程序分析題_第1頁
C程序設(shè)計(jì)程序分析題_第2頁
C程序設(shè)計(jì)程序分析題_第3頁
C程序設(shè)計(jì)程序分析題_第4頁
C程序設(shè)計(jì)程序分析題_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余3頁可下載查看

下載本文檔

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

文檔簡介

1、!-三 程序分析題閱讀以下程序,寫出其運(yùn)行結(jié)果。1 #include <iostream.h>int a 10 = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ;int fun( int i );void main() int i ,s = 0;for( i = 0; i <= 10; i+ ) try s = s + fun( i ) ; catch( int ) cout << " 數(shù)組下標(biāo)越界! " << endl; cout << "s = " << s <&

2、lt; endl;int fun( int i ) if ( i >= 10 )throw i ; return ai ;2 #include <iostream.h>void f();class T public:T() cout << "constructor" << endl;try throw "exception" catch( char * ) cout << "exception2" << endl; throw "exception"

3、T() cout << "destructor" ;void main() cout << "main function " << endl;try f() ; catch( char * ) cout << "exception1" << endl; cout << "main function " << endl;void f() T t; 3 #include <iostream.h>class Bclass pu

4、blic:Bclass( int i, int j ) x = i; y = j; virtual int fun() return 0 ; protected:int x, y ;class Iclass:public Bclass public :Iclass(int i, int j, int k):Bclass(i, j) z = k; int fun() return ( x + y + z ) / 3; private :int z ;void main() Iclass obj( 2, 4, 10 );Bclass p1 = obj;cout << p1.fun()

5、<< endl;Bclass & p2 = obj ;cout << p2.fun() << endl;cout << p2.Bclass : fun() << endl;Bclass *p3 = &obj;cout << p3 -> fun() << endl;4 #include <iostream.h>class BASE public:virtual void getxy( int i,int j = 0 ) x = i; y = j; virtual void fun

6、() = 0 ;protected:int x , y; ;class A: public BASE public:void fun() cout<<"x = "<<x<<'t'<<"y = x * x = "<<x*x<<endl; ;class B:public BASE public:void fun() cout << "x = " << x << 't' << "y

7、 = " << y << endl; cout << "y = x / y = " << x / y << endl; ;void main() BASE * pb;A obj1;B obj2;pb = &obj1;pb -> getxy( 10 );pb -> fun();pb = &obj2;pb -> getxy( 100, 20 );pb -> fun();5. #include <iostream.h>class BASE1 public:&l

8、t;<BASE1( int i ) cout << " 調(diào)用基類 BASE1 的構(gòu)造函數(shù) :" << i endl ; ;class BASE2 public:<<BASE2( int j ) cout << " 調(diào)用基類 BASE2 的構(gòu)造函數(shù) :" << j endl ; ;class A: public BASE1, public BASE2 public:A( int a, int b, int c, int d ) : BASE2(b), BASE1(c), b2(a), b1(

9、d) cout << " 調(diào)用派生類 A 的構(gòu)造函數(shù) :" << a+b+c+d << endl; private:BASE1 b1;BASE2 b2; ;void main() A obj( 1, 2, 3, 4 ); 6. #include <iostream.h>class T public :T() a = 0; b = 0; c = 0; T( int i , int j , int k ) a = i; b =j ; c = k; void get( int &i , int &j , int &a

10、mp;k ) i = a; j = b; k = c; T operator * ( T obj ); private: int a , b , c;T T:operator * ( T obj ) T tempobj; tempobj.a = a * obj.a;tempobj.b = b * obj.b; tempobj.c = c * obj.c;return tempobj;void main() T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3;int a , b , c; obj3 = obj1 * obj2; obj3.get( a, b, c );cou

11、t << "( obj1 * obj2 ): t"<< "a = " << a << 't' << "b = " << b<< 't' << "c = " << c << 't' << endl;( obj2 * obj3 ).get( a, b, c ); cout << "( obj2 * obj3 ): t &

12、quot;<< "a = " << a << 't' << "b = " << b << 't' << "c = "<< c << 't' << endl;7. #include < iostream.h > class vector public:vector() vector(int i,int j) x = i ; y = j ; friend vec

13、tor operator + ( vector v1, vector v2 ) vector tempvector ; tempvector.x = v1.x + v2.x ; tempvector.y = v1.y + v2.y ; return tempvector ; void display() cout << "( " << x << ", " << y << ") "<< endl ; private:int x , y ;void main() ve

14、ctor v1( 1, 2 ), v2( 3, 4 ), v3 ;cout << "v1 = " ;v1.display() ;cout << "v2 = " ;v2.display() ;v3 = v1 + v2 ;cout << "v3 = v1 + v2 = " ;v3.display() ;8 #include < iostream.h >struct data int n ;int score ; ;void main() data a3 = 1000,83,1005,70,1

15、009,95 , *p = a ; cout << (p+)-> score << endl ; cout << (p+)-> score << endl ; cout << p-> score << endl ;cout << (*p). score + << endl ;9 #include < iostream.h >class A public :int f1();int f2();void setx( int m ) x = m ; cout <<

16、 x << endl; void sety( int n ) y = n ; cout << y << endl; int getx() return x ; int gety() return y ; private :int x, y ;int A:f1() return x + y ; int A:f2() return x - y ; void main() A a ;a.setx( 10 ) ; a.sety( 5 ) ;cout << a.getx() << 't' << a.gety() &l

17、t;< endl ; cout << a.f1() << 't' << a.f2() << endl ;10 #include < iostream.h > class T public :T( int x, int y ) a = x ; b = y ;cout << " 調(diào)用構(gòu)造函數(shù) 2." << endl ;cout << a << 't' << b << endl ; T( T &d ) c

18、out << " 調(diào)用構(gòu)造函數(shù) 1." << endl ;cout << d.a << 't' << d.b << endl ;T() cout << " 調(diào)用析構(gòu)函數(shù) ."<<endl; int add( int x, int y = 10 ) return x + y ; private :int a, b ; void main() T d1( 2, 4 ) ;T d2( d1 ) ;cout << d2.add( 8 ) &

19、lt;< endl ; 11 #include < iostream.h > class T public:T(int x) a=x; b+=x; static void display(T c) cout<<"a="<<c.a<<'t'<<"b="<<c.b<<endl; private: int a;static int b; ; int T:b=5; void main() T A(3),B(5);T:display(A);T:display(

20、B);12 #include < iostream.h > class A public :A() a = 25 ; void printa() cout << "A:a = " << a << endl ; private : int a ;friend class B ; ;class B public:void display1( A t ) t.a + ; cout << "display1:a = " << t.a << endl ; ; void displa

21、y2( A t ) t.a - ; cout << "display2:a = " << t.a << endl ; ; ;void main() A obj1 ;B obj2 ; obj1.printa() ; obj2.display1( obj1 ) ; obj2.display2( obj1 ) ; obj1.printa() ;13. #include <iostream.h> class A public:A( ) cout << "A" << endl; A( ) cout << "A" << endl; ;class B public:B( ) f( ); cout << "B" << endl; virtual void f( ) cout << "B:f" << endl; virtual B( ) cout << "B" << endl; ;class D: public B public:D( ) cout << "D" <<

溫馨提示

  • 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)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論