c++ 经典面试问题及答案
The article cover’s most commonly asked interview questions in c++ and a brief explanation of each question.
本文介绍了c ++中最常问到的面试问题,并简要解释了每个问题。
1. What is OOPS?
1.什么是OOPS?
Object-oriented programming is a programming paradigm based on the concept of “objects” of the classes, Objects may contain data in the form of fields and associated code in the form of methods. The objects can access their own procedures and can modify their data fields.
面向对象编程是基于类的“对象”概念的编程范例,对象可以包含字段形式的数据和方法形式的关联代码。 对象可以访问自己的过程,并可以修改其数据字段。
2. What is function overloading, how did it make the lives of C++ programmer better than C programmer?
2.什么是函数重载,它如何使C ++程序员的生活比C程序员更好?
In C++ it’s allowed to make functions with the same name in the same scope. The functions with the same name are called overloaded functions. The difference that must exist between the two functions with the same name is the parameters that are being passed. Function overloading can be done by using different parameter types and changing the number of arguments. C, on the other hand, doesn’t support function overloading which reduces the code quality and readability.
在C ++中,允许在相同范围内使用相同名称创建函数。 具有相同名称的函数称为重载函数。 具有相同名称的两个函数之间必须存在的区别是要传递的参数。 函数重载可以通过使用不同的参数类型并更改参数数量来完成。 另一方面,C不支持函数重载,这会降低代码质量和可读性。
3. How is function overloading achieved?
3.如何实现函数重载?
The program below has “add” function with two different implementations. One for double data type numbers and another for integers. The function has the same name but different parameters so it’s a clear example of function overloading.
下面的程序具有“添加”功能,具有两种不同的实现方式。 一个用于双精度数据类型数字,另一个用于整数。 该函数具有相同的名称,但参数不同,因此是函数重载的一个清晰示例。
#include<bits/stdc++.h>
using namespace std;
int add(int a, int b){//Add Integers
return a+b;
}
double add(double a, double b){//Add Double
return a+b;
}
int main(){
cout<<"Integer Type Numbers after addition : "<<add(1,2)<<"\n";
cout<<"Double Type Numbers after addition : "<<add(1.1,2.2);
}Output:
Integer Type Numbers after addition : 3
Double Type Numbers after addition : 3.3
4. what is dynamic binding?
4.什么是动态绑定?
To Understand Dynamic binding let’s see what are virtual functions and what binding means.
要了解动态绑定,让我们看看什么是虚函数以及绑定的含义。
Binding refers to the process of converting identifiers such as variable and function calls into addresses by the compiler.
绑定是指编译器将诸如变量和函数调用之类的标识符转换为地址的过程。
Virtual Function in c++ is a function which is defined in“Base” class and overridden in “Derived” class. It is made by adding “virtual” keyword in the function definition.Dynamic Binding: In C++ when the compiler is not able to decide which function to call at compile time but computes the same at runtime then this type of binding is known as dynamic binding.An example of Dynamic Binding is when a virtual function is defined in “Base” class and overridden in “Derived” class, and this function is called with the help of pointer of the base class that stores the address of the object of “Derived Class”.
c ++中的虚函数是在“基础”类中定义并在“派生”类中覆盖的函数。 它是通过在函数定义中添加“ virtual”关键字来实现的。 动态绑定:在C ++中,当编译器无法决定在编译时调用哪个函数,但在运行时对其进行计算时,这种绑定称为动态绑定。 动态绑定的一个示例是在“基础”类中定义虚拟函数并在“派生”类中重写虚拟函数,然后在存储“派生类”对象地址的基类的指针的帮助下调用此函数”。
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void print(){
cout<<"Base Class";
}
};
class Derived : public Base{
public:
void print(){
cout<<"Derived Class";
}
};
int main(){
Base *ptr;
Derived d;
ptr=&d;
ptr->print();
}Output : Derived Class
In the above program when the compiler compiles the program a pointer named “ptr” is made which is initialized by the address of an object of a derived class and this address is only available at compile time. With this information, the compiler is able to know which print function is being referenced by the programmer so as a result derived class “print” function is called.
在上述程序中,当编译器编译程序时,将创建一个名为“ ptr”的指针,该指针由派生类的对象的地址初始化,并且该地址仅在编译时可用。 有了这些信息,编译器便能够知道程序员正在引用哪个打印功能,从而可以调用结果派生类“ print”功能。
5. What is an abstract class?
5.什么是抽象类?
Abstract class in c++ is a Class that has no object and that Class consists of at least one pure virtual function. We are not allowed to make instances of Abstract classes. This class as its name suggests provides us with a level of abstraction about the functionalities we are going to encounter in a program like the print functionality explained below.Example:
c ++中的抽象类是一个没有对象的类,并且该类至少包含一个纯虚函数。 我们不允许创建Abstract类的实例。 顾名思义,该类为我们提供了有关程序中将要遇到的功能(如下面说明的打印功能)的抽象级别。 例:
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void print()=0;
};
class Derived : public Base{
public:
void print(){
cout<<"Abstract Class print method defination.";
}
};
int main(){
ptr=&d;
d.print();
}
The pure virtual “print” function is declared in the Abstract class named “Base” and defined in “Derived” class, in this way the abstract class provided us with the abstraction to what we are going to encounter. If the Derived class inherits the abstract class and does not define the pure virtual functions of Base class then it also becomes an Abstract class.
纯虚拟“ print”函数在名为“ Base”的Abstract类中声明,并在“ Derived”类中定义,以这种方式,abstract类为我们提供了将要遇到的内容的抽象。 如果Derived类继承了Abstract类并且未定义Base类的纯虚函数,则它也将成为Abstract类。
6. What happens under the hood for virtual functions? (vPointer, vTable etc)
6.虚拟功能的幕后发生了什么? (vPointer,vTable等)
For understanding this part let’s go on to the example we considered while learning Dynamic Binding.
为了理解这一部分,让我们继续学习动态绑定时考虑的示例。
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
virtual void print(){
cout<<"Base Class";
}
};
class Derived : public Base{
public:
void print(){
cout<<"Derived Class";
}
};
int main(){
Base *ptr;
Derived d;
ptr=&d;
ptr->print();
}Output : Derived Class
As in the above program, ptr is a pointer to Base class but still ptr->print() calls the “print” function of Derived class.Let’s understand what internally happens here:
与上面的程序一样,ptr是指向基类的指针,但是ptr-> print()仍然调用Derived类的“ print”函数。让我们了解内部发生的事情:
- For every class that has a virtual function, compiler maintains a vTable that contains information about all virtual functions inside the class. 对于每个具有虚拟功能的类,编译器都会维护一个vTable,该表包含有关该类内部所有虚拟功能的信息。
- For every object of the class, the first 4 bytes contain the pointer to vTable of the class it belongs to, and this pointer is called vPointer. 对于该类的每个对象,前4个字节包含指向其所属类的vTable的指针,该指针称为vPointer。
The above two points are the reason that ptr in the above program even being the pointer of Base class calls the print function of the derived class, as ptr=&d where d is an object of the derived class and it’s first 4 bytes contain vPointer that point to vTable of Derived class which contains the print function.
以上两点是上述程序中的ptr甚至是基类的指针都调用派生类的打印函数的原因,因为ptr =&d其中d是派生类的对象,并且它的前4个字节包含vPointer指向包含打印功能的派生类的vTable。
7. What is the diamond problem?
7.什么是钻石问题?
The problem when two classes inherit a base class and those two classes are inherited by another class then the class that inherits both the classes has two copies of the methods and properties of the base class.
问题是当两个类继承一个基类,而这两个类又被另一个类继承时,则同时继承这两个类的类具有该基类的方法和属性的两个副本。
A
/ \
B C // B and C inherit A
\ /
D // D inherit both B and C so has A two times
The below program will give an error:
下面的程序将给出一个错误:
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
int a=10;
};
class Derived1 : public Base{
public:
int b=20;
};
class Derived2 : public Base{
public:
int c=30;
};
class Derived3 : public Derived1, public Derived2{
public:
int d=40;
};
int main(){
Derived3 d;
cout<<d.a;
}
The error is that a comes twice in Derived3 class and compiler finds ambiguity in data member a.To solve this problem we inherit Base class virtually in Derived1 & Derived2 which makes only one instance of data member a and thus solves the diamond problem.The right code will be:
错误是a在Derived3类中出现两次,并且编译器发现数据成员a中存在歧义。为解决此问题,我们实际上在Derived1和Derived2中继承了基类,该基类仅使数据成员a成为一个实例,从而解决了菱形问题。代码将是:
#include<bits/stdc++.h>
using namespace std;
class Base{
public:
int a=10;
};
class Derived1 : public virtual Base{
public:
int b=20;
};
class Derived2 : public virtual Base{
public:
int c=30;
};
class Derived3 : public Derived1, public Derived2{
public:
int d=40;
};
int main(){
Derived3 d;
cout<<d.a;
}
8. What is operator overloading?
8.什么是操作员超载?
Normally a “+” operator would be used to add two numbers, but if we want to add objects of two classes then it is also possible, for that operator overloading as a concept is used.
通常,将使用“ +”运算符将两个数相加,但是如果我们要添加两个类的对象,则也有可能,因为该运算符使用了概念重载。
Consider the below program:
考虑下面的程序:
#include<bits/stdc++.h>
using namespace std;
class A{
public:
int a=10;
};class B{
public:
int a=20;
};
int operator+(A obj1,B obj2){
return obj1.a+obj2.a;
}
int main(){
A obj1;
B obj2;
cout<<obj1+obj2;
}
Output: 30
In the above program, as we see the two objects get added, this is because we overloaded the + operator. For overloading, we defined int as the return value of “operator+” function which takes two arguments that are objects of the two classes. So with this way, we were able to add two objects using the + operator. Operator overloading works similarly for all overloadable operators.Non Overloadable operators are:
在上面的程序中,我们看到添加了两个对象,这是因为我们重载了+运算符。 对于重载,我们将int定义为“ operator +”函数的返回值,该函数采用两个参数作为两个类的对象。 因此,通过这种方式,我们能够使用+运算符添加两个对象。 运算符重载对于所有可重载运算符的工作方式都相似。
::
.
*.
?:
9. What is a namespace?
9.什么是名称空间?
A namespace is a declarative region that provides a scope to identifiers inside it. A Namespace helps to group elements in groups to provide collisions when multiple libraries are being used. Identifiers in one namespace are visible to each other without qualification, but for variables outside the same namespace needs to be specified with a fully qualified name for example: “std:: string”.
命名空间是一个声明性区域,它为其中的标识符提供范围。 命名空间有助于将元素分组,以在使用多个库时提供冲突。 一个命名空间中的标识符无需限定即可彼此可见,但是对于同一命名空间之外的变量,则需要使用完全限定的名称来指定,例如:“ std :: string”。
10. When and why will you use static functions in a class?
10.什么时候以及为什么要在类中使用静态函数?
A static function is used in a class when the objects of the class share a common function that doesn’t need to have a new copy for each object of that class.
当类的对象共享一个不需要为该类的每个对象创建新副本的通用函数时,该类中将使用静态函数。
#include<bits/stdc++.h>
using namespace std;
class A{
public:
static printName(){
cout<<"I am object of class A\n";
}
int a;
};
int main(){
A obj1;
A obj2;
obj1.printName();
obj2.printName();
}Output:
I am object of class A
I am object of class A
In the above program for each object “printName ”function will remain the same, so putting static keyword before the function definition makes only one shared function for each object of class A.
在上面的程序中,每个对象的“ printName”功能将保持不变,因此将static关键字放在函数定义之前,只会为A类的每个对象提供一个共享功能。
11. Do you have an idea about Standard Template Library In C++?
11.您对C ++中的标准模板库有想法吗?
Standard Template Library in c++ is a set of template classes that provides a direct implementation of common data structures like a stack, queue, list, set etc. STL has four major categories that are:
c ++中的标准模板库是一组模板类,它们提供对常见数据结构(如堆栈,队列,列表,集合等)的直接实现。STL具有四个主要类别:
- Algorithms 演算法
- Containers 货柜
- Functions 功能
- Iterators 迭代器
12. How are errors handled in C++?
12.如何用C ++处理错误?
In C++ exceptions are handled with the help “try ”and “catch ” blocks. The block of code that may throw an exception is kept under try block.Example:
在C ++中,异常通过帮助“ try”和“ catch”块进行处理。 可能引发异常的代码块位于try块下。示例:
#include<bits/stdc++.h>
using namespace std;
float divide(float a,float b){
try{
if(b!=0){
return a/b;
}
else{
throw "Divide By Zero Exception\n";
}
}
catch(const char* err){
cout<<err;
}
}
int main(){
cout<<divide(1.2,0);
}Output: Divide By Zero Exception
Some other must know Questions are:
其他一些必须知道的问题是:
Difference Between C & C++?
C和C ++之间的区别?
Do you have an idea what metaprogramming is? How is it achieved in C++?
您知道什么是元编程吗? 如何在C ++中实现?
How many kinds of castings are available in C++, how is it better than C?
C ++提供了几种类型的转换,它比C好吗?
翻译自: https://medium.com/analytics-vidhya/complete-c-interview-questions-answers-8dc1fbe5b06b
c++ 经典面试问题及答案
本文汇总了C++面试中常见的经典问题及详细答案,帮助应聘者准备C++相关的技术面试,涵盖语言特性、算法应用等多个方面。

1万+

被折叠的 条评论
为什么被折叠?



