2022cⅡ
2022c++(Ⅱ)
选择
I in C++ , There is a kind of class that it can only be inherited but cannot be initiated, we call that class as a ( ),to define that kind of class, it has to have at least a ( ) 。(A) virtual function (B) pure virtual function (C) abstract class (D)friend class
根据题意,应选b:纯虚函数及c:抽象类。
一个包含纯虚函数的类无法创建对象,相应的将一个抽象类作为一个数据成员的类也无法创建对象。
Assume that AB is a class, the declaration of copy constructor of that class should be ( )。(A)AB&(AB x) (B)AB(AB x) (C)AB(AB &x) (D)AB(AB *x)
根据题意,应为c
用该类的对象创建另一个对象,即复制,需要传引用,防止在生成一堆数据浪费数据
For the member of a structure, the default access modifier is ( )。
(A) public; (B) private; (C) protected; (D) static;
根据题意,应为a
类的默认属性使私有,而结构体默认属性是公有
Assume AB is a class, to execute statement “AB a(4),b[3],*p[2];”, how many times to invoke the constructor?( )。
(A)3 (B) 4 (C) 6 (D) 9
操作以左往右分别为,初始化的带参构造,默认构造的对象数组,未分配空间的指针数组
Which of the following is not correct ( )
The name of constructor is same as the name of the class
class constructor could have default parameters
the default return type of a constructor is of type int
the constructor can be overloaded
构造函数不能return,也吗,没有指明返回类型
Parameterized stream manipulator setfill specifies the fill character that is displayed when an output is displayed in a field wider than the number of characters or digits in the output. The effect of setfill applies:
- Only to the current value being displayed.
- Only to outputs displayed in the current statement.
- Until explicitly set to a different setting.
- Until the output buffer is flushed.
std::setfill
是一个操纵符,它用于设置输出流的填充字符。当你需要在输出中对齐数据时,setfill
可以帮助你指定一个字符,当使用 setw
函数来设定输出宽度时,这个字符会被用来填充不足的部分。若字符超出了,则不会截断。
Assuming the following is the beginning of the constructor definition for class Circle which inherits from class Point,Circle::Circle( double r, int a, int b ) : Point( a, b ) The second line:
- Invokes the Point constructor with values a and b.
- Causes a compiler error.
- Is unnecessary because the Point constructor is called automatically.
- Indicates inheritance.
用于给point中的可能私有数据成员赋值等操作,总之可用,不会报错
Which of the following assignments would be a compilation error?
1) Assigning the address of a base-class object to a base-class pointer.
2) Assigning the address of a base-class object to a derived-class pointer.
3) Assigning the address of a derived-class object to a base-class pointer.
4) Assigning the address of a derived-class object to a derived-class pointer.比如你能说狗是动物,但不能说动物是狗
Assume class T is declared as following,which of the following declaration of the function fF() is not correct( )。
class T
{ int i;
friend void fF(T&,int) ;
};1) void fF (T &objT,int k) { objT.i = k+1; }
2) void fF (T &objT,int k) { k = objT.i+1; }
3) void T::fF (T &objT,int k) { k += objT.i; }
4) void fF (T &objT,int k) { objT.i += k; }
显而易见
纠错
Find out errors in the following code, and correct them.
Following is the destructor function prototype in Time class definition:
void ~Time( int );
~Time( );
- Following is part of the Time class definition:
class Time { |
没眼看
模拟
Write the output of the following program.
|
Initailizing default |
此处float输出为0.
2.
|
A::A() called. |
继承实际是在子类内部开了一个属于父类的空间。
无论继承还是声明为类的数据成员,都是先于该类被创建的,当然也会最后被析构。
3.
|
a=3 b=24 |
因为输出是后面的事,此时赋值全部完成了
4.
|
B::b: 3 |
5.
|
setiosflags(ios::fixed|ios::showpoint) 会使输出以固定小数点表示法显示,而不是使用科学计数法,并且总是显示小数点后的零。这个操作符作用于后续的浮点数输出。 |
可能需要学习手写setw和setprecision的样式
完善程序
fill in the blanks according to the output
The following program defined a complex class stands for complex number. It has real part real and imaginary part img. It overloaded complex number addition by using friend function. Please finish it.
class complex
{
float [1], [2]; ---------------------------real img
public:
complex (float r=0,float i=0)
{ real=r; img=i; }
[3] [4] operator+(complex c1,complex c2);---------------------friend complex
};
[5] operator+(complex c1,complex c2)-------------------------------complex
{ complex temp;
[6]---------------------------------------------temp.real=cl.real+c2.real;
[7]---------------------------------------------同理
return temp;
}2. The following code has the screen output below, please fill in the blanks.
using namespace std;
class Power
{
public:
Power(int i)
{
x=i;
}
*__*(8)___void display(){ cout<<"x="\<\<x;} -------virtual
// definition of virtual function protected: int x;
};
class Square: public Power
{
public:
Square(int n):____(9)_________ { } //Constructor of Square void display()
-----------------------------------------------Power(0)
{ _______ *(10)* ___cout\<\<”\tx square=”\<\<x\*x\<\<endl;}------Power::dispaly();
};
class Cube:public Power { public:
Cube(int n):Power(n){ } void display()
{ _______ *(10)* ___; cout\<\<”\tx cube=”\<\<x\*x\*x\<\<endl;}-------------Power::dispaly();
};
void fun(Power &p) { p.display();}
void main()
{
Square squ(2);
Cube cub(3);
fun(squ); fun(cub);
}
手抄
Write program
1.Use function template to calculate average of arrays of different data type. Function main () is as following:
|
Program calculates average of an array of type integer and of type double. complete a function template to accomplish such function(功能) (6 points )
int average(int* arry,int _max) |
Rewrite class Student, overload operator >> and \<\< to substitute(代替) function input() and output() respectively. (8 points)
Add a constructor for class Student to initiate data members ;(3 points)
Modify function main() to get correct result. (3 points)
|
|
总结:
1.重载运算符
- 纯虚函数和抽象类
- 课本上不知名的函数,如此处使用的复制,剪切等
- 输出的样式
- 生成的顺序