1. It is necessary to overloaded functions? Please explain why it necessary and specify the situation that need to overloaded functions. What kinds of functions can be overloaded?
- 有必要。
- 在要实现功能相似参数不同的函数时,程序员就不需要选择不同的函数名,这也增强程序的可读性。
- 普通函数和类的成员函数都可以被重载。
- They should either be in different namespaces, or differ in the parameter types so that the compilers give different names to them according to different parameters while compilin.
- Default arguments should be put in function declarations. There’s no need to put default arguments again on definitions, or else the compiler will generate compiling errors.
- The parameters in the last of parameter list.
- It will avoid confusion.
//添加前:
int add(int x,int y)
{
return x + y;
}
//添加后:
int add(int x, int y, int z = 0)
{
return x + y + z;
}
struct O
{
int i;
O (int i = 0) {}
O (int i, int j = 0) {}
};
int main ()
{
O o(6);
}
No.
When O(6) is called, there is two possible situations:
1) O(int) is called and the parameter is 6.
2) O(int,int) is called, the first parameter is 6 and the second is the default value 0.
Therefore, these codes have two different meanings, so it cannot pass the compilation.
-
The header guarding method:
- Define the macro when a header is included for the first time, and when it is about to be included for more times, the macro is checked such that all definitions won’t be included twice.
-
Why do we need it:
- When the header file is included twice, all definitions inside will be defined multiple times, which is unacceptable.
8. Explain why we should avoid using macros to define numbers? Please give the alternative grammars to replace using macros.
-
Reasons why we should avoid using macros to define numbers:
- Macros cannot be decided types.
- Macros cannot be controlled scopes.
- Macros cannot be controlled accessibilities.
-
We can use static const or enum to avoid using macros.
//Example: class abcd { static const int MAX_NUM = 100; //enum{MAX_NUM = 100}; }
9. In the following example, explain the reason to remove commented line for avoiding compile error and write down the output of the code below.
enum smallenum: std::int16_t {A, B, C };
enum color {red, yellow, green = 20, blue};
enum class altitude: char{ high='h',low='l',};
enum {d,e,f = e + 2};
int f1 (int a) {}
int f2 (char a) {}
int main ()
{
std::cout << sizeof(smallenum) << " " << A << " " << C << std::endl;
std::cout << sizeof(red) << std::endl;
std::cout << sizeof(d) << " " << d << std::endl;
f1 (d);
f1 (color::red);
//f2 (altitude::high);
f1 (A);
}
- c++11 defines a new concept, which makes enumerations both strongly typed and strongly scoped. So altitude::high won’t be transferred to “h”, and thus will cause compile error.
- Output:
2 0 2
4
4 0
10. Please explain why we can define a constant variable in the header file without causing compiling errors? What should be done for you to use a constant variable defined in another source file?
-
在预编译的时候如果在头文件定义了const变量,每一个包含该头文件的c文件都会将其展开,而在编译的时候不会报错,因为这符合语法规则,每一个包含这个头文件的*.c文件都会编译一次这个变量,分配一个新的地址,然后在链接的时候也不会报错,因为每个同名变量都有自己的地址空间,虽然程序运行不会带来问题,但是如果变量多了,会造成rom的大量浪费。
-
To use variables in another source file, we need to use the keyword extern as in the following code
// some function extern int c; cout <<c <<endl;
const int* a = s;
int const* b = s;
int* const c = s;
- the address a points to could be modified, but the value of the address a points to cannot be modified.
- same as a: b can be modified, but (*b) cannot.
- the address c points to cannot be modified.
-Using keyword static or constructor initializer list.
//Example:
class abcd
{
//static const int MAX_NUM = 100; //using keyword static
const int MAX_NUM;
abcd(int max_num):MAX_NUM(max_num) {}
//using initialization list
}
原则上我们可以使用const_cast,例如:
const int a = 1;
int &b = const_cast<int&> (a);
b = 2;
这段代码可以通过编译,但是很有可能运行错误,或者得到无法预计的结果(例如,在上述代码之后再开一个以a为长度的数组试试?)。C++标准规定:Modifying a const object through a non-const access path and referring to a volatile object through a non-volatile glvalue results in undefined behavior。
在设计良好的程序中,通常不应该使用const_cast。
14. Where should the keyword “const” be placed to declare or define a constant member function(常量成员函数)? Please give an example.
-
The keyword const should be placed at the end of the declaration.
//For example: void F(int) const; void Class::F(int) const {}
15. Please state the restrictions of constant member functions. Can we have any other alternative (i.e., access the members in other ways)?
- Constant member functions cannot modify any variable of the class. Notice that variables of a constant object can only be used by constant functions.