出自:许昌学院面向对象程序设计
[分析题,4分] class my_ base{ int a,b; public: base(int x,int y) {a=x;b=y;} virtual void show(){ cout<<“base”; cout< } }; class my_class:public base{ int c: public: my_class(int x,int y,int z):my_base(x,y){c=z;} void show(){cout<<“my_class ”<<“c=”< } void main { my_base mb(50,50),*mp; my_class mc(10,20,30); mp=&mb; mp->show(); mp=&mc; mp->show(); }
[分析题,4分] #include ”iostream.h”class three{ int x,y,z; public: three(int a,int b,int c){ x=a;y=b;z=c;} friend ostream &operator<<(ostream &output, three ob); };ostream &operator<<(ostream &output, three ob){ output<<ob.x<<’,’<<ob.z<<’,’<<ob.y<<endl; return output;}main(){ three obj1(10,20,30); cout<<obj; three obj2(50,40,100);cout<<obj2; }
[分析题,4分] #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 << "数组下标越界!" << endl; } } cout << "s = " << s << endl;}int fun( int i ){ if ( i >= 10 ) throw i ; return a[i] ;}