[advancedc++]Professionalcoding04-IKM题库
资源
【常考题】
【百题】
nonmember operator
25. Which operator can be implemented as a nonmember operator?
A. =
B. ()
C. []
D. +
28. If the class name is X, what't the type of its "this" pointer (in a nonstatic, non-const member function) ?
B. X* const
29. Which is the most prefered way of throwing and handling exceptions?
A. Throw by value and catch by reference
31. The conditional compilation
C. It is compiling a program bad on a condition
39. Seek time is
B. Time taken by read/write head mechanism to position itlf over an appropriate cylinder
40. Latency time is
C. Time taken by the appropriate ctor to come under read/write head
字符串中找⼦串
(s.find("abcdefg")==string::npos)
如果存在包含关系find函数返回的就是主串与⼦串相匹配的下标,
如果不存在包含关系就返回 npos(⼀个常数,表⽰不存在)
类中的protected成员
Driver类是Bus类中的⼦类,被保护;代码中的driver不是类中的有⾎缘的成员,⾃然不能定义为不是Bus::Driver相关指针。
#include <iostream>
using namespace std;
class Bus {
protected:
class Driver; // forward declarations
public:
static Driver* createDriver() {
return new Driver;
}
// friend void uBus();
private:
int ats;
protected:
class Driver {
public:
string name;
};
};
class DeriveDriver : public Bus {};
void uBus()
{
Bus::Driver* driver = Bus::createDriver(); // 'Driver' is a protected member of 'Bus'
// DeriveDriver::Driver* driver = Bus::createDriver(); // 'Driver' is a protected member of 'Bus'
driver->name = "DRIVER";
}
int main(int argc, char* argv[])
{
return0;
}
模板中的变量
代码可正常编译,但其中的int duration有存在的必要么?
#include <iostream>
using namespace std;
template <typename T, int duration>
double power (double amplitude, int frequency = 1000) { return amplitude * frequency; }
int main(int argc, char* argv[])
{
double output;
output = power<int, 100>(3.0, 'a');
// output = power<500>(3.0); // No matching function for call to 'power'
output = power<int, 500>(3.0, 100);
// output = power<int>(3.0, 100); // No matching function for call to 'power'
// output = power<int, char>(3.0); // No matching function for call to 'power'
output = power<int, 500>(3.0);
cout << output << endl;
return0;
}
关键字noexcept
(1) ⼀个操作或者函数不可能抛出任何异常。
void swap(Type& x, Type& y) noexcept //C++11
{
x.swap(y);
}
(2) 如果操作x.swap(y)不发⽣异常,那么函数swap(Type& x, Type& y)⼀定不发⽣异常。
void swap(Type& x, Type& y) noexcept(noexcept(x.swap(y))) //C++11
{
x.swap(y);
}
拷贝构造函数 copy constructor
⾃定义类具有“赋值/拷贝”属性。写法参数如下:
//拷贝构造函数
CExample(const CExample & c)
{
a=c.a;
printf("copy constructor is called\n");
}
alignof() 和 alignas()
Q12. Which regarding the C++ 11 operators alignof() and alignas() are correct?
A. alignof () is ud to obtain the alignment of a specified variable or type.
B. alignas () is ud to specify custom alignment of variables and ur-defined types.
E. alignas () works on expressions. (整型常量表达式)
callable objects
C++语⾔有⼏种callable objects:函数、函数指针、lambda表达式、bind创建的对象以及重载了函数调⽤运算符()的类。
Ref:
Q14. When using the multi-threading library in the C++ standard library, which are NOT callable objects which may be run in a thread?
A. A class with an overridden operator()
B. A class member function
C. A lambda function
D. An executable file
E. A regular function
运算符重载
【需要再仔细整理⼀遍】
iota 符号
iota( ++quence.begin(), d(), 1 );
不同于c语⾔中的⽤到atoi()函数,是“字符串转为数字”。
这⾥是⽣成⼀个序列。
#include<iostream>
#include<numeric>
#include<vector>
using namespace std;
int main(void)
{
vector<int> v(10);
iota(v.begin(), v.end(), 2); // 最后⼀个参数是“起始数字”
vector<int>::iterator it = v.begin();
while(it != v.end())
{
cout<<*it++<<"";
}
return0;
}
其他类似的⽅法:
// 1 2 3 4 5 6 7 8 9 10 11 12
// iota( quence.begin(), d(), 1 );
// 0 1 1 0 1 5 8 13 21 34 55 89
// move( quence.begin(), quence.begin() + 2, quence.begin() + 3 );
/
/ 0 1 1 1 1 1 1 1 1 1 1 1
// for_each( quence.begin(), d(),
// []( int& elem ) { if ( elem > 1 ) elem = 1; } );
// 0 1 1 0 1 5 8 13 21 34 55 89
// copy_if( quence.begin (), quence.begin() + 2, quence.begin() + 3,
// []( int elem ) { return elem <= 1; } );
"多态"类型
Q23. Which are types of polymorphism in C++?
A. Bi-metric polymorphism
B. Hybrid polymorphism
C. Parametric polymorphism
D. Post processing polymorphism
E. Single polymorphism
The Four Polymorphisms in C++ :
【详见此链接】
Subtype polymorphism is also known as runtime polymorphism.
Parametric polymorphism is also known as compile-time polymorphism.
Ad-hoc polymorphism is also known as overloading.
Coercion is also known as (implicit or explicit) casting.
别名 a type alias
Q27. Which are valid ways to create a type alias?
A. template<class T> using vec = std::vector<T>; vec<float> incomes;
C. typedef int intAlias; intAlias count;
E. template<typename T> using myGenric = T; myGenric<int> age;
A和E何时会⽤到呢?
处理任何异常
需要参数位置写“省略号”。
try {
// 保护代码
} catch(...) {
// 能处理任何异常的代码
}
异常的执⾏顺序
对于atch捕获异常的形式来说,对于异常的捕获,可以有多个catch。对于try⾥⾯发⽣的异常,他会根据发⽣的异常和catch⾥⾯的进⾏匹配(怎么匹配,按照catch块从上往下匹配),当它匹配某⼀个catch块的时候,他就直接进⼊到这个catch块⾥⾯去了,后⾯在再有catch块的话,它不做任何处理,直接跳过去,全部忽略掉。
numeric_limits
在C/C++11中,std::numeric_limits为模板类,在库编译平台提供基础算术类型的极值等属性信息。
std::cout << "int has infinity: " << std::numeric_limits<int>::has_infinity << std::endl;
std::cout << "Minimum value for float: " << std::numeric_limits<float>::min() << std::endl;
继承与函数 overriding
Q34. Which are true about the declarations reprenting the C++ class hierachy below?
A. Level5::func2(int) will override any functionality defined in Level2::func2(int).
B. Level1::func1() functionality has been removed from Level2 but is still available to subquent levels.
C. Level5::func4(char) will produce a compiler error since it is not a true override.
D. Level5::func3(int) will produce a compiler error since virtual functions implemented elwhere cannot be deleted.
E. Level5 results in a compiler error since it cannot inherit from Levei3.
涉及的知识点
Ref:
有些时候我们希望限制默认函数的⽣成。典型的是禁⽌使⽤拷贝构造函数,以往的做法是将拷贝构造函数声明为private的并不提供实现,这样当拷贝构造对象时编译不能通过,C++11则使⽤delete关键字显式指⽰编译器不⽣成函数的默认版本。
Ref:
函数后跟着const:
表⽰成员函数隐含传⼊的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为隐含了对this指针的const引⽤);唯⼀的例外是对于mutable修饰的成员。
加了const的成员函数可以被⾮const对象和const对象调⽤,但不加const的成员函数只能被⾮const对象调⽤。
在声明需要重写的函数后,加关键字 override。
#include <iostream>
using namespace std;
class Level1 {
public:
void func1();
};
class Level2 : public Level1 {
public:
void func1() = delete;
int func2(char);
};
class Level3 final : public Level2 {
public:
virtual double func3(int);
};
class Level4 : public Level2 {
virtual double func4(int);
double func5(char a);
};
class Level5 : public Level3 { // Ba 'Level3' is marked 'final'
public:
double func2(char) override; // Only virtual member functions can be marked 'override'
virtual double func3(int) = delete;
virtual double func4(char) override; // 'func4' marked 'override' but does not override any member functions
};
int main(int argc, char **argv)
{
Level3 obj;
obj.func1(); // Attempt to u a deleted function
return0;
}
unordered associative containers
Q35. Which correctly describe C++11 unordered associative containers, for example std::unordered_t, std::unordered_map, std::unordered multit, std::unordered—multimap?
A. They perform their lookup bad on a hash function.
constexpr 关键字