Home / Interview / C++ :: General Questions

Interview :: C++

31) Explain this pointer?

This pointer holds the address of the current object.

32) What does Scope Resolution operator do?

A scope resolution operator(::) is used to define the member function outside the class.

33) What is the difference between delete and delete[]?

Delete [] is used to release the array of allocated memory which was allocated using new[] whereas delete is used to release one chunk of memory which was allocated using new.

34) What is a pure virtual function?

The pure virtual function is a virtual function which does not contain any definition. The normal function is preceded with a keyword virtual. The pure virtual function ends with 0.

Syntax of a pure virtual function:

Let's understand this through an example:

Output:

 javaTpoint 
35) What is the difference between struct and class?
Structures class
A structure is a user-defined data type which contains variables of dissimilar data types. The class is a user-defined data type which contains member variables and member functions.
The variables of a structure are stored in the stack memory. The variables of a class are stored in the heap memory.
We cannot initialize the variables directly. We can initialize the member variables directly.
If access specifier is not specified, then by default the access specifier of the variable is "public". If access specifier is not specified, then by default the access specifier of a variable is "private".
The instance of a structure is a "structure variable".
Declaration of a structure:
 struct structure_name {    // body of structure; } ; 
Declaration of class:
 class class_name {    // body of class; }  
A structure is declared by using a struct keyword. The class is declared by using a class keyword.
The structure does not support the inheritance. The class supports the concept of inheritance.
The type of a structure is a value type. The type of a class is a reference type.
36) What is a class template?

A class template is used to create a family of classes and functions. For example, we can create a template of an array class which will enable us to create an array of various types such as int, float, char, etc. Similarly, we can create a template for a function, suppose we have a function add(), then we can create multiple versions of add().

The syntax of a class template:

Syntax of a object of a template class:

37) What is the difference between function overloading and operator overloading?

Function overloading: Function overloading is defined as we can have more than one version of the same function. The versions of a function will have different signature means that they have a different set of parameters.

Operator overloading: Operator overloading is defined as the standard operator can be redefined so that it has a different meaning when applied to the instances of a class.

38)

What is a virtual destructor?

A virtual destructor in C++ is used in the base class so that the derived class object can also be destroyed. A virtual destructor is declared by using the ~ tilde operator and then virtual keyword before the constructor.

Note: Constructor cannot be virtual, but destructor can be virtual.

Let's understand this through an example

  • Example without using virtual destructor