c++程序分析题_第1页
c++程序分析题_第2页
c++程序分析题_第3页
c++程序分析题_第4页
c++程序分析题_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、三程序分析题阅读以下程序,写出其运行结果。1. #includc <iostrcam>using namespace std;int a10= 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+ ) s = s + fun( i) ; catch( int) coui « ”数组下标越界! " vv cndl; cout << us ="<< s << cndl;int fun(

2、 int i ) if ( i >= 10) throw i ;return ai;2. #includc <iostrcam>using namespace std;void FO;class T public:TO coul << "constructor” << cndl;try throw "exception" catch( char * ) coul << ,rcxccption2n << cndl; throw "exception”;TO cout VV ''

3、;destructor" ;void main。 coul << "main function ” << cndl;try f。; catch( char * ) coul << "cxccptionl” << cndl; cout << "main function ” << cndl;void f() T l; 3. #includc <iostrcam>using namespace std;class Bclass public:Bclass( int i, in

4、t j) x = i; y = j; virtual int funQ return 0 ; protected:int x, y ;class lclass:public Bclass public :lclass(int i, int j, int k):Bclass(i, j) z = k;int funQ return (x + y + z) / 3; private :int z ;void main。 Iclass obj( 2, 4,10);Bclass pl = obj;cout << pl.funQ « cndl;Bclass & p2 = ob

5、j ;cout << p2.fun() << cndl;cout << p2.Bclass : fun。<< cndl;Bclass *p3 = &obj;cout << p3 -> funQ << cndl;4. #includc <iostrcam>using namespace std;class BASE public:virtual void gctxy( int i jnt j = 0) x = i; y = j; virtual void fun。= 0 ;protected:int

6、 x , y;class A: public BASE public:void fun。 cout<<Mx = M<<x<<,t,«Hy = x * x = ,r<<x*x«cndl; ;class B:public BASE public:void fun() coul « ”x = M « x « rf « Hy = M « y « cndl; cout << uy = x / y = M << x / y << rndl; ;

7、void main。 BASE * pb;A objl; B obj2; pb = &objl; pb -> gctx)r( 10);pb -> fun。; pb = &obj2; pb -> gctxy( 100, 20); pb -> fun。; 5. #includc <ic)strcam>using namespace std;class BASE1 public:BASE! (inti) cout « ”调用基类 BASE1 的构造函数:“ vv i « cndl;;class BASE2 public:BASE

8、2( int j) cout « ”调用基类 BASE2 的构造函数:“ vv j « cndl;;class A: public BASE1, public BASE2 public:A( int a, int b, int c, int d ) : BASE2(b), BASEl(c), b2(a), bl(d) cout <v ”调用派生类A的构造函数:"VV a+b+c+d « cndl; private:BASE1 bl;BASE2 b2;void main。Aobj(l,2, 3,4);6. #includc <ic)strcam

9、>using namespace std;class T public :TO a = 0; b = 0; c = 0; T( int i , int j , int k) a = i; b =j ; c = k; void gct( int &i, int &j , int &k ) i = a; j = b; k = c; T operator * (T obj );private:int a , b , c;T T:operalor * ( T obj ) T tcmpobj;tcmpobj.a = a * obj.a;tcmpobj上= b * obj.b

10、;tcmpobj.c = c * obj.c;return tempobj;void main。 T obj 1( 1,2,3 ),obj2( 5,5,5 ),obj3;int a , b , c;obj3 = objl * obj2;obj3.gct( a, b, c);coul << "( objl * obj2 ): tM« Ha 二" VV a «« "b = H « b« rf << Hc = M « c «<< cndl;(。囱2 * obj3 )

11、.gct( a, b, c);cout << "( obj2 * obj3 ): t "« Ha 二" vv a «« nb 二 " VV b « rf « Hc = n« c «« cndl;7. #includc <iostrcam>using namespace std; public:vector。 vcctor(int i,int j) X = i; y = j;friend vector operator + (vector vl, vec

12、tor v2 ) vector tempvector;tcmpvcctor.x = vl .x + v2.x ;tcmpvcctor.y = vl.y + v2.y ;return tempvector;void displayO cout « H(H « x « H, H « y « H) n« cndl; private:int x , y ;void main。 vector vl ( 1, 2), v2( 3,4), v3 ;cout« nvl ="vl .displayO ;cout << n

13、v2 =";v2.display0 ;v3 = vl + v2 ;cout << nv3 = vl + v2 = " ;v3.display0 ;8. #includc <iostrcam>using namespace std;struct data int n ;int score ;void main。 data a3 = 1000,83,1005,70,1009,95 , *p = a ;cout << (p+)-> score << ciidl ;cout << (p+)-> score &l

14、t;< cndl ;cout << p-> score << cndl ;cout << (*p), score + << cndl ;9. #includc <iostrcam>using namespace std;class Aint fl 0;int f2();void sctx( int m) x = m ; cout << x « cndl; void scty( int n) y = n ; cout << y << cndl; int gctxO rcturn

15、x ; int gctj'O return y ; private :int x, y;int A:fl() return x + y ; int return x - y ; void mainQ A a ;a.sctx( 10) ; a.scty( 5 );cout << a.gctxf) << 't << a.gctyr0 << cndl ;cout << a.fl() << 't' vv a.f2() << cndl;10. #includc <iostrcam&g

16、t;using namespace std;class T public : public :T( int x, int y ) a = x ; b = y ;cout << "调用构造函数2." vv cndl;cout << a << 't' << b << cndl;T(T&d) cout « ”调用构造函数1.” VV cndl;cout << d.a « 'l << d.b << cndl ;TO coutvv ”调用

17、析构函数Jvvcndl;int add( int x, int y = 10) return x + y ; private :int a, b ;void main()Td1(2,4);Td2(dl);cout << d2.add( 8) << cndl ;11. . #includc <ioslrcam>using namespace std;class T public:T(intx) a=x; b+=x;static void display(T c) cout«Ha=H«c.a«,t,«,b=M«c

18、.b«cndl; private:int a;static int b;int T:b=5;void main。 TA(3),B(5);T:display(A);T:display(B);12. . #includc <iostrcam>using namespace std;class A public :AO a = 25 ; void priniaO cout« HA:a = " << a << cndl ; using namespace std;private :int a ;friend class B ;class

19、 B public:void displayl (At) La + ; coul << ndisplay 1 :a = " << La << cndl; ;void display2( At) La ; cout << ndisplay2:a = " << La << cndl; ;void main。 Aobjl ;B obj2 ;objl.printaO ;obj2.displayl( objl );obj2.display2( objl );objl.printaO ;13. #includc <iostrcam>class A public:A() cout « MAn « cndl; A() cout« vv cndl; ;class B public:B() f( ); cout «« cndl;virtual void f() cout << HB:fr « cndl; virtual -B() cout << B" << cndl; ;class D: public B public:0() cout&#

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论