一、填空题
1. 在定义类对象的语句执行时,系统在建立每个对象的过程中将自动调用该类的_构造函数_____使其初始化。
2. 当一个类对象被撤消时将自动调用该类的__析构函数_________。
3. 对基类数据成员的初始化是通过执行派生类构造函数中的__初始化表_____来实现的。
4. 对一个类中的数据成员的初始化可以通过构造函数中的_初始化表___实现,也可以通过构造函数中的__赋值语句________实现。
5. 在一个派生类中,对基类成员、类对象成员和非类对象成员的初始化次序的先基类成员,后类对象成员,最后非对象成员。
6. 当撤消一个含有基类和类对象成员的派生类对象时,将首先完成派生类本身的析构函数定义体的执行,接着完成类对象成员的析构函数定义体的执行,最后完成基类成员的析构函数定义体的执行。
7. 设PX是指向一个类动态对象的指针变量,则执行“delete px;”语句时,将自动调用该类的析构函数。
8. 当一个类对象离开它的作用域时,系统将自动调用该类的析构函数。
9. 假定一个类对象数组为A[N],当离开它的作用域时,系统自动调用该类析构函数的次数为N次。
10. 假定AB为一个类,则执行“AB a[10];”语句时,系统自动调用该类构造函数的次数为10次。
11. 假定拥护没有给一个名为AB的类定义构造函数,则系统为其隐含定义的构造函数为空构造函数。
12. 假定用户没有给一个名为AB的类定义析构函数,则系统为其隐含定义的析构函数为空析构函数。
13. 若需要把一个函数“void f(); ”定义为一个类AB的友元函数,则应在类AB的定义中加入一条语句:friend void f();。
14. 若需要把一个类AB定义为一个类CD的友元类,则应在类CD的定义中加入一条语句:friend class AB;。
15. 假定一个类AB中有一个静态整型成员bb,在类外为它进行定义并初始化为0时,所使用写法为AB:bb = 0;。
16. 假定类AB中有一个公用属性的静态数据成员bb,在类外不通过对象名访问该成员 bb的写法为AB:bb。
17. 当类中一个字符指针成员指向具有n个字节的储存空间时,它所能储存字符串的最大长度为n-1。
18. 假定AB为一个类,则该类的拷贝构造函数的声明语句为AB:AB(AB &)。
19. 对类对象成员初始化是通过执行构造函数中的初始化表完成的。
20. 对于类中定义的成员,其隐含访问权限为private,对于结构中定义的成员,其隐含访问权限为public。
21. 一个类的友元函数或友元类能够通过成员操作符访问该类的所有数据成员和函数成员。
22. 假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为:AB operator +(AB, AB);。
23. 在C++流类库中,根基类为ios。
24. 在C++流类库中,输入流类和输出流类的名称分别为istream和ostream。
25. 若要在程序文件中进行标准输入输出操作,则必须在开始的#inlude命令中使用iosteam.h头文件。
26. 若要在程序文件中进行文件输入输出操作,则必须在开始的#inlude命令中使用fstream.h头文件。
27. 当从字符文件中读取回车和换行两个字符时,被系统看作为一个换行符。
28. 当使用ifstream流类定义一个流对象并打开一个磁盘文件时,文件的隐含打开方式为 读取的文本文件,当使用ofstream 流类定义一个流对象并打开一个磁盘文件时,文件的隐含打开方式为写入的文本文件。
29. 当需要使用istrstream流类定义一个流对象并联系一个字符串时,应在文件开始使用#include命令,使之包含strstrea.h文件。
二.给出下列程序运行后的输出结果
1.#include<iostream.h>
class A{
int a, b;
public:
A( ) {a=b=0;}
A( int aa, int bb){
a=aa; b=bb;
cout<<a<<’ ’<<b<<endl;
}
};
void main( ){
A x,y(6,3), z(8,10);
}
6 3
8 10
2.#include<iostream.h>
class A
{
int a, b;
public:
A(int aa= 0, int bb= 0): a(aa),b(bb)
{
cout<<"Constructor!"<< a + b<<endl;
}
};
void main(){
A x, y(2,5), z(y);
}
Constructor!0
Constructor!7
3.#include<iostream.h>
class A{
int * a;
public:
A(int aa= 0)
{
a = new int(aa);
cout<<"Constructor!"<< * a<<endl;
}
};
void main()
{
A x[2];
A * p = new A(5);
delete p;
}
Constructor!0
Constructor!0
Constructor!5
4.#include<iostream.h>
class A
{
int a;
public:
A(int aa= 0): a(aa){}
~A(){cout<<"Destructor!"<< a <<endl;}
};
void main()
{
A x(5);
A * p = new A(10);
delete p;
}
Destructor!10
Destructor!5
5.#include<iostream.h>
class A {
int * a;
public:
A(int x)
{
a = new int(x);
cout<<"Constructor!"<< * a<<endl;
}
~A(){delete a;cout<<"Destructor!"<<endl;}
};
void main()
{
A x(9),* p;
p = new A(12);
delete p;
}
Constructor!9
Constructor!12
Destructor!
Destructor!
6.#include<iostream.h>
class A
{
int a;
public:
A(int aa= 0): a(aa)
{
cout<<"Constructor A!"<< a<<endl;
}
};
class B:public A
{
int b;
public:
B(int aa, int bb): A(aa), b(bb)
{
cout<<"Constructor B!"<< b<<endl;
}
};
void main()
{
B x(2,3),y(4,5);
}
Constructor A!2
Constructor B!3
Constructor A!4
Constructor B!5
7.#include<iostream.h>
class A
{
int a;
public:
A(int aa= 0){a = aa;}
~A(){cout<<"Destructor A!"<< a<<endl;}
};
class B:public A{
int b;
public:
B(int aa= 0, int bb= 0): A(aa){b = bb;}
~B(){cout<<"Destructor B!"<<b<<endl;}
};
void main()
{
B x(5),y(6,7);
}
Deconstructor B!7
Deconstructor A!6
Deconstructor B!0
Deconstructor A!5
8.#include<iostream.h>
#include<stdlib.h>
class A
{
int a,b;char op;
public:
A(int aa, int bb, char ch){a = aa;b = bb;op = ch;}
int Comp()
{
switch(op)
{
case '+' :return a + b;
case '-' :return a - b;
case '*' :return a * b;
case '/' :if(b!=0)return a/b;else exit(1);
case '%' :if(b!=0)return a%b;else exit(1);
default:exit(1);
}
}
void SetA(int aa, int bb, char ch)
{
a = aa;b = bb;op = ch;
}
};
void main(void)
{
A x(3,5,'*');
int a = x.Comp();
x. SetA(4,9, '+');
a += x.Comp();
x. SetA(13,8, '%');
a += x.Comp();
cout<<"a="<<a<<endl;
}
a=33
9.#include<iostream.h>
class B
{
int a,b;
public:
B(){a = b = 0;}
B(int aa, int bb){a = aa;b = bb;}
B operator +(B& x)
{
B r;
r . a = a + x . a;
r . b = b + x . b;
return r;
}
B operator -(B& x)
{
B r;
r . a = a - x . a;
r . b = b - x . b;
return r;
}
void OutB()
{
cout<< a << ' ' << b <<endl;
}
};
void main()
{
B x(6,5),y(13,3), z1, z2;
z1 = x + y;
z2 = x - y;
z1.OutB();
z2.OutB();
}
19 8
-7 2
10.#include<iostream.h>
template<class TT>
class FF{
TT a1,a2,a3;
public:
FF(TT b1, TT b2, TT b3){
a1 =b1;a2 =b2;a3 =b3;
}
TT Sum(){return a1 + a2 + a3;}
};
void main(){
FF< int > x(8,3,4),y(5,9,11);
cout<< x. Sum()<< ' ' << y. Sum()<<endl;
}
15 25
二、写出下列每个函数的功能
1.#include<iomanip.h>
#include<fstream.h>
#include<string.h>
void JA(char * fname)
/ /可以把fname所指字符串作为文件标识符的文件称为fname文件
{
ofstream fout(fname);
char a[20];
cin>> a;
whlie(strcmp(a,”end”)!=0){
fout<< a << endl;
cin>> a;
}
}
将键盘输入的字符串(上限为19个字符)写入到fname文件中。
2.#include<iomanip.h>
#include<fstream.h>
void JB(char * fname)
/ /可把以fname所指字符串作为文件标识符的文件称为fname文件
/ /假定该文件中保存着一批字符串,每个字符串的长度均小于20。
{
ifstream fin(fname);
char a[20];
int i = 0;
whlie(fin>> a){
cout<< a <<endl;
i + +;
}
fin.close();
cout<< ”i = ”<< i <<endl;
}
将fname文件输出到标准输出流(屏幕)上,最后显示字符串的数量。
3.#include<iomanip.h>
#include<fstream.h>
void JC(chat * fname, int n)
/ /可把以fname所指字符串作为文件标识符的文件称为fname文件
{
ofstream fout(fname, ios : : out | ios : : binary);
int x;
for(int i = 0;I<n;I + +){
cin>> x;
fout.write((char * )&x, sizeof(x));
}
fout.close();
}
从键盘输入n个数,并以二进制的方式写到fname文件中。
4.#include<iomanip.h>
#include<fstream.h>
void JD(char * fname)
/ /可把以fname所指字符串作为文件标识符的文件称为fname文件,
/ /假定该文件保存着一批整数。
{
ifstream fin(fname, ios : : in | ios : : nocreate | ios : : binary);
int x, s = 0, n = 0;
whlie(fin.read((char * )&x, sizeof(x))){
s + = x;n + +;
}
cout<< n << ’ ’ << s << ’ ’ <<float(s)/n<<endl;
fin.close();
}
输出fname文件中保存的整数的个数、总加值以及平均值。版权声明
声明:有的资源均来自网络转载,版权归原作者所有,如有侵犯到您的权益
请联系本站我们将配合处理!