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 ca