五、程序分析题(本大题共4小题,每小题5分,共20分)
51.(给出下面程序的输出结果
#include<iostream.h>
class example
{
int a;
public:
example(int b){a=b++;}
void print( ){a=a+1;cout<<a<<" ";}
void print( )const{cout<<a<<"";}
};
void main( )
{
example x(3);
const example y(2);
x.print( );
y.print( );
}
52.给出下面程序的输出结果
#include<iostream.h>
class Sample
{
int x,y;
public:
Sample( ){x=y=0;}
Sample(int a,int b){x=a;y=b;}
~Sample()
{
if(x==y)
cout<<"x=y"<<endl;
else
cout<<"x!=y"<<endl;
}
void disp()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
void main()
{
Sample sl(2,3);
s1.disp();
}
53.给出下面程序的输出结果
#include<iostream.h>
class Sample
{
int n;
static int sum;
public:
Sample(int x){n=x;}
void add( ){sum+=n;}
void disp( )
{
cout<<"n="<<n<<",sum:"<<sum<<endl;
}
};
int Sample::sum=0;
void main( )
{
Sample a(2),b(3),c(5);
a.add( );
a.disp( );
b.add( );
b.disp( );
c.add( );
c.disp( );
}
54.给出下面程序的输出结果
#include<iostream.h>
class A{
private:
friend class B;
A(int val=0):data(val),use(1){}
int use,data;
};
class B{
public:
A*pa;
B(int val=0):pa(new A(val)){ }
~B( ){if( -- pa ->use==0)delete pa;}
B(B&rhs){ + + rhs ->use;pa=rhs.pa;}
B&operator=(B&rhs);
A*operator ->( ){return pa;}
void Display( ){
cout<<"data="<<pa ->data
<<",use="<<pa ->use<<endl;
}
};
B&B::operator=(B&rhs)
{
if(this = = &rhs)return*this;
if( -- pa ->use = =0)delete pa;
+ + rhs ->use;
pa=rhs.pa;
return * this;
}
int main( ){
B b1(1),b2(b1),b3(3);
b1.Display( );
b2.Display( );
b3.Display( );
b3=b1;
b1.Display( );
b3.Display( );
return 0;
六、程序设计题(本大题共1小题,每小题10分,共10分)
55.已知一个student类,包括学姓名,成绩和等级,不含任何成员函数.process为学生信息处理类.
class student{
char name[ 10 ];
int deg;
char level[ 7 ];
friend class process:
public:
student(char na[ ],int d){strcpy(name,na);deg=d;}
} ;
class process{
public:
void trans(student s[ ]){…}//根据学生成绩计算学生等级(优大于89分,良大于79分,中大于69分,及格大于59分,不及格小于60分).
void disp(student s[ ]){...}//根据成绩降序打印所有学生信息
};
要求实现process类中的两个函数: