c++程序分析题(20页).doc
《c++程序分析题(20页).doc》由会员分享,可在线阅读,更多相关《c++程序分析题(20页).doc(21页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-三 程序分析题阅读以下程序,写出其运行结果。1#include using namespace std;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 数组下标越界! endl; cout s = s = 10 ) throw i ; return ai ;2#include using namespace std;void f();clas
2、s T public: T() cout constructor endl; try throw exception; catch( char * ) cout exception2 endl; throw exception; 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 using namespace std;class Bcl
3、ass public: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(
4、) endl; Bclass & p2 = obj ; cout p2.fun() endl; cout p2.Bclass : fun() endl; Bclass *p3 = &obj; cout fun() endl;4#include using namespace std;class BASE public: virtual void getxy( int i,int j = 0 ) x = i; y = j; virtual void fun() = 0 ; protected: int x , y; ;class A: public BASE public:void fun()
5、coutx = xty = x * x = x*xendl; ;class B:public BASE public: void fun() cout x = x t y = y endl; cout y = x / y = x / y getxy( 10 ); pb - fun(); pb = &obj2; pb - getxy( 100, 20 ); pb - fun();5. #include using namespace std;class BASE1 public: BASE1( int i ) cout 调用基类BASE1的构造函数: i endl ; ;class BASE2
6、public: BASE2( int j ) cout 调用基类BASE2的构造函数: 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(d) cout 调用派生类A的构造函数: a+b+c+d endl; private: BASE1 b1; BASE2 b2; ; void main() A obj( 1, 2, 3, 4 ); 6. #include using namespace std;class T
7、 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 &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 t
8、empobj;void main() T obj1( 1,2,3 ), obj2( 5,5,5 ), obj3; int a , b , c; obj3 = obj1 * obj2; obj3.get( a, b, c ); cout ( obj1 * obj2 ): t a = a t b = b t c = c t endl; ( obj2 * obj3 ).get( a, b, c ); cout ( obj2 * obj3 ): t a = a t b = b t c = c t endl;7. #include using namespace std;class vector pub
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c+ 程序 分析 20
限制150内