要使rb指向a,则rb应定义为____________。
6. 若只需要通过一个成员函数读取其数据成员的值,而不需要修改它们,则应在函数头的后面加上________关键字;若只需要读取引用参数的值,不需要对其修改,则应在该参数说明的开始使用__________关键字。
7.假定一个类对象数组为A[N],当定义该数组时,将自动调用该类的无参构造函数的次数为________次,当离开它的作用域时,将自动调用该类析构函数的次数为________次。
8.假定AB为一个类,则类定义体中的“AB(AB& x);”语句为该类______________的原型语句,而“operator=(AB& x);”为该类______________的原型语句。
9. 在定义一个派生类时,使用__________关键字表示为私有继承,使用__________关键字表示为公有继承。
10. 重载一个运算符时,该运算符的__________、__________以及操作符的个数不允许改变。
三、程序填充题,对程序、函数或类中划有横线的位置,根据题意按标号把合适的内容填写到程序后面的标号处。(每小题5分,共20分)
1. 在输出屏幕上打印出一个由字符’*’组成的等腰三角形,该三角形的高为5行,从上到下每行的字符数依次为1,3,5,7,9。
#include<iostream.h>
void main()
{
int i,j;
for(i=1;___(1)___;i++) {
for(j=1;j<=9;j++)
if(j<=5-i || ___(2)___) cout<<’ ’;
else ___(3)___;
cout<<endl;
}
}
(1) (2) (3)
2. 从一个字符串中删除所有同一字符后得到一个新字符串并输出。
#include<iostream.h>
const int len=20;
void delstr(char a[],char b[],char c);
void main() {
char str1[len],str2[len];
char ch;
cout<<"输入一个字符串:";
cin>>str1;
cout<<"输入一个待删除的字符:";
cin>>ch;
delstr(str1,str2,ch);
cout<<str2<<endl;
}
void delstr(char a[],char b[],char c)
{
int j=0;
for(int i=0; ___(1)___; i++)
if(___(2)___) b[j++]=a[i];
b[j]=___(2)___;
}
(1) (2) (3)
3. 已知一维数组类ARRAY的定义如下,ARRAY与普通一维数组区别是:其重载的运算符[ ]要对下标是否越界进行检查。
class ARRAY{
int *v; //指向存放数组数据的空间
int s; //数组大小
public:
ARRAY(int a[], int n);
~ARRAY(){delete []v;}
int size(){ return s;}
int& operator[](int n);
};
___(1)___ operator[](int n) //[ ]的运算符成员函数定义
{
if(n<0 || ___(2)___) {cerr<<"下标越界!"; exit(1);}
return ___(3)___;
}
(1) (2) (3)
4. 一个类定义如下:
class Point
{
private:
int x, y;
public:
Point(){x=y=0;}
Point(int x0,int y0) {x=x0;y=y0;}
int GetX() {return x; }
int GetY() {return y; }
void Print(){cout<<"Point("<<x<<","<<y<<")"<<endl;}
___(1)___; //友元函数声明
___(2)___; //友元函数声明
};
Point operator+(Point& pt,int dd)
//加号操作符重载函数,实现Point类对象与整数的加法
{
Point temp=pt;
temp.x+=dd;
temp.y+=dd;
return temp;
}
Point operator+(Point& pt1,Point& pt2)
//加号操作符重载函数,实现两个Point类对象的加法
{
Point temp=pt1;
temp.x+=pt2.x;
temp.y+=pt2.y;
___(3)___;
}
(1) (2) (3)
四、理解问答题,写出前三小题的程序运行结果和指出后两小题的程序(或函数)所能实现的功能。(每小题6分,共30分)
1. #include<iostream.h>
const int B=2;
void main()
{
int p=1,s=1;
while(s<50) {
p*=B;
s+=p;
}
cout<<"s="<<s<<endl;
}
运行结果:
2. #include<iostream.h>
class CE {
private:
int a,b;
int getmin() {return (a<b? a:b);}
public:
int c;
void SetValue(int x1,int x2, int x3) {
a=x1; b=x2; c=x3;
}
int GetMin();
};
int CE::GetMin() {
int d=getmin();
return (d<c? d:c);
}
void main()
{
int x=5,y=12,z=8;
CE *ep;
ep=new CE;
ep->SetValue(x+y,y-z,10);
cout<<ep->GetMin()<<endl;
CE a=*ep;
cout<<a.GetMin()*3+15<<endl; }
运行结果:
3. #include<iostream.h>
class A {
int a[10]; int n;
public:
A(int aa[], int nn): n(nn) {
for(int i=0; i<n; i++) a[i]=aa[i]; }
int Get(int i) {return a[i];} int SumA(int nn) {
int s=0;
for(int j=0; j<nn; j++) s+=a[j]; return s;
}
};
void main() {
int a[]={2,5,8,10,15,20};
A x(a,4);
A y(a,6);
int d=1;
for(int i=0; i<4; i++) d*=x.Get(i); int f=y.SumA(5);
cout<<"d="<<d<<endl;
cout<<"f="<<f<<endl;
}
运行结果:
4. #include<iostream.h>
#include<stdlib.h>
#include<time.h>
const int N=10;
int ff(int x, int y) {
int z;
cout<<x<<'+'<<y<<'=';
cin>>z;
if(x+y==z) return 1; else return 0; }
void main()
{