C++ programming is a general purpose programming language that was created by Bjarne Stroustrup. It is essential to know C++ if you want to work in the software development domain. C++ is an extension of C programming language.The blog will cover C++ Interview Questions for advanced levels, C++ programming interview questions, and more.
1. What is C++?
C++ is an object-oriented programming language created by Bjarne Stroustrup. It was released in 1985. As an extension of the C language
2. What are the advantages of C++?
- C++ is a highly portable language means that the software developed using C++ language can run on any platform.
- C++ is an object-oriented programming language which includes the concepts such as classes, objects, inheritance, polymorphism, abstraction.
- C++ has the concept of inheritance. Through inheritance, one can eliminate the redundant code and can reuse the existing classes.
- Data hiding helps the programmer to build secure programs so that the program cannot be attacked by the invaders.
- Message passing is a technique used for communication between the objects.
- C++ contains a rich function library.
3. What is the difference between C++ and Java?
The difference between c++ and java are as follows:
- C++ supports goto statements whereas Java does not.
- C++ is majorly used in system programming whereas Java is majorly used in application programming.
- C++ supports multiple inheritance whereas Java does not support multiple inheritance
- C++ supports operator overloading whereas Java does not support operator overloading.
- C++ has pointers which can be used in the program whereas Java has pointers but internally.
- C++ uses a compiler only whereas Java uses both compiler and interpreter.
- C++ has both call by value and call by reference whereas Java supports only call by value.
- C++ supports structures and joins whereas Java does not support structure and joins
- Java supports unsigned right shift operator (>>>) whereas C++ does not.
- C++ is interactive with hardware whereas Java is not that interactive with hardware.
4. What is the difference between C and C++?
C | C++ |
C language was developed by Dennis Ritchie. | C++ language was developed by Bjarne Stroustrup. |
C is a structured programming language. | C++ supports both structural and object-oriented programming language. |
C is a subset of C++. | C++ is a superset of C. |
In C language, data and functions are the free entities. | In the C++ language, both data and functions are encapsulated together in the form of a project. |
C does not support the data hiding. Therefore, the data can be used by the outside world. | C++ supports data hiding. Therefore, the data cannot be accessed by the outside world. |
C supports neither function nor operator overloading. | C++ supports both function and operator overloading. |
In C, the function cannot be implemented inside the structures. | In the C++, the function can be implemented inside the structures. |
Reference variables are not supported in C language. | C++ supports the reference variables. |
C language does not support the virtual and friend functions. | C++ supports both virtual and friend functions. |
5. What is namespace in C++?
If there are two or more functions with the same name defined in different libraries then how will the compiler know which one to refer to? Thus namespace came to picture. A namespace defines a scope and differentiates functions, classes, variables etc. with the same name available in different libraries. The namespace starts with the keyword “namespace”. The syntax for the same is as follows:
Syntax:
namespace namespace_name {
// code declarations
}
6. What is a class?
The class is a user-defined data type. The class is declared with the keyword class. The class contains the data members, and member functions whose access is defined by the three modifiers are private, public and protected. The class defines the type definition of the category of things. It defines a datatype, but it does not define the data it just specifies the structure of data.
7. Define namespace in C++?
- The namespace is a logical division of the code which is designed to stop the naming conflict.
- The namespace defines the scope where the identifiers such as variables, class, functions are declared.
- The main purpose of using namespace in C++ is to remove the ambiguity. Ambiquity occurs when the different task occurs with the same name.
- For example: if there are two functions exist with the same name such as add(). In order to prevent this ambiguity, the namespace is used. Functions are declared in different namespaces.
- C++ consists of a standard namespace, i.e., std which contains inbuilt classes and functions. So, by using the statement "using namespace std;" includes the namespace "std" in our program.
Syntax of namespace:
namespace namespace_name
{
//body of namespace;
}
Syntax of accessing the namespace variable:
namespace_name::member_name;
8. What are the different data types present in C++?
The 4 data types in C++ are given below:
- Primitive Datatype(basic datatype). Example- char, short, int, float, long, double, bool, etc.
- Derived datatype. Example- array, pointer, etc.
- Enumeration. Example- enum
- User-defined data types. Example- structure, class, etc.
9.What is template in C++?
A template in C++ is used to pass data types as parameters . These make it easier and more simpler to use classes and functions.
template <typename T>
int fun (T a,T b)
{
return (a+b);
}
int main(){
cout<<fun<int>(11,22);
}
10.What are the various OOPs concepts in C++?
Class:
The class is a user-defined data type which defines its properties and its functions. For example, Human being is a class. The body parts of a human being are its properties, and the actions performed by the body parts are known as functions. The class does not occupy any memory space. Therefore, we can say that the class is the only logical representation of the data.
class student
{
//data members;
//Member functions
}
Object:
An object is a run-time entity. An object is the instance of the class. An object can represent a person, place or any other item. An object can operate on both data members and member functions. The class does not occupy any memory space. When an object is created using a new keyword, then space is allocated for the variable in a heap, and the starting address is stored in the stack memory. When an object is created without a new keyword, then space is not allocated in the heap memory, and the object contains the null value in the stack.
class Student
{
//data members;
//Member functions
}
The syntax for declaring the object:
Student s = new Student();
Inheritance:
Inheritance provides reusability. Reusability means that one can use the functionalities of the existing class. It eliminates the redundancy of code. Inheritance is a technique of deriving a new class from the old class. The old class is known as the base class, and the new class is known as derived class.
Syntax
class derived_class :: visibility-mode base_class;
Encapsulation:
Encapsulation is a technique of wrapping the data members and member functions in a single unit. It binds the data within a class, and no outside method can access the data. If the data member is private, then the member function can only access the data.
Abstraction:
Abstraction is a technique of showing only essential details without representing the implementation details. If the members are defined with a public keyword, then the members are accessible outside also. If the members are defined with a private keyword, then the members are not accessible by the outside methods.
Data binding:
Data binding is a process of binding the application UI and business logic. Any change made in the business logic will reflect directly to the application UI.
Polymorphism:
Polymorphism means multiple forms. Polymorphism means having more than one function with the same name but with different functionalities. Polymorphism is of two types:
- Static polymorphism is also known as early binding.
- Dynamic polymorphism is also known as late binding.
11.What are the different types of polymorphism in C++?
Polymorphism: Polymorphism means multiple forms. It means having more than one function with the same function name but with different functionalities.
Runtime polymorphism:
Runtime polymorphism is also known as dynamic polymorphism. Function overriding is an example of runtime polymorphism. Function overriding means when the child class contains the method which is already present in the parent class. Hence, the child class overrides the method of the parent class. In case of function overriding, parent and child class both contains the same function with the different definition. The call to the function is determined at runtime is known as runtime polymorphism.
Compile time polymorphism:
Compile-time polymorphism is also known as static polymorphism. The polymorphism which is implemented at the compile time is known as compile-time polymorphism. Method overloading is an example of compile-time polymorphism.
Method overloading: Method overloading is a technique which allows you to have more than one function with the same function name but with different functionality.
Method overloading can be possible on the following basis:
- The return type of the overloaded function.
- The type of the parameters passed to the function.
The number of parameters passed to the function.
12.Define token in C++?
A token in C++ can be a keyword, identifier, literal, constant and symbol
13.Who was the creator of C++?
Bjarne Stroustrup
14. Which operations are permitted on pointers?
Following are the operations that can be performed on pointers:
Incrementing or decrementing a pointer: Incrementing a pointer means that we can increment the pointer by the size of a data type to which it points.
There are two types of increment pointers:
Pre-increment pointer: The pre-increment operator increments the operand by 1, and the value of the expression becomes the resulting value of the incremented. Suppose ptr is a pointer then pre-increment pointer is represented as ++ptr.
Post-increment pointer: The post-increment operator increments the operand by 1, but the value of the expression will be the value of the operand prior to the incremented value of the operand. Suppose ptr is a pointer then post-increment pointer is represented as ptr++.
15. Define 'std'.
Std is the default namespace standard used in C++.
16. Which programming language's unsatisfactory performance led to the discovery of C++?
C++was discovered in order to cope with the disadvantages of C
17. What are the C++ access specifiers?
The access specifiers are used to define how to functions and variables can be accessed outside the class.
There are three types of access specifiers:
Private: Functions and variables declared as private can be accessed only within the same class, and they cannot be accessed outside the class they are declared.
Public: Functions and variables declared under public can be accessed from anywhere.
Protected: Functions and variables declared as protected cannot be accessed outside the class except a child class. This specifier is generally used in inheritance.
18.How delete [] is different from delete?
Delete is used to release a unit of memory, delete[] is used to release an array.
19. What is the full form of STL in C++?
STL stands for Standard Template Library.
20. What is an object?
The Object is the instance of a class. A class provides a blueprint for objects. So you can create an object from a class. The objects of a class are declared with the same sort of declaration that we declare variables of basic types.
21. What is the difference between new() and malloc()?
- new() is a preprocessor while malloc() is a function.
- There is no need to allocate the memory while using "new" but in malloc() you have to use sizeof().
- "new" initializes the new memory to 0 while malloc() gives random value in the newly allotted memory location.
- The new() operator allocates the memory and calls the constructor for the object initialization and malloc() function allocates the memory but does not call the constructor for the object initialization.
- The new() operator is faster than the malloc() function as operator is faster than the function.
22. What is the difference between an array and a list?
An Array is a collection of homogeneous elements while a list is a collection of heterogeneous elements.
Array memory allocation is static and continuous while List memory allocation is dynamic and random.
In Array, users don't need to keep in track of next memory allocation while In the list, the user has to keep in track of next location where memory is allocated.
23. What are the methods of exporting a function from a DLL?
There are two ways:
- By using the DLL's type library.
- Taking a reference to the function from the DLL instance.
24. Define friend function?
Friend function acts as a friend of the class. It can access the private and protected members of the class. The friend function is not a member of the class, but it must be listed in the class definition. The non-member function cannot access the private data of the class. Sometimes, it is necessary for the non-member function to access the data. The friend function is a non-member function and has the ability to access the private data of the class.
Following are the characteristics of a friend function:
- The friend function is not in the scope of the class in which it has been declared.
- Since it is not in the scope of the class, so it cannot be called by using the object of the class. Therefore, friend function can be invoked like a normal function.
- A friend function cannot access the private members directly, it has to use an object name and dot operator with each member name.
- Friend function uses objects as arguments
25. What is a virtual function?
A virtual function is used to replace the implementation provided by the base class. The replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer.
A virtual function is a member function which is present in the base class and redefined by the derived class.
When we use the same function name in both base and derived class, the function in base class is declared with a keyword virtual.
When the function is made virtual, then C++ determines at run-time which function is to be called based on the type of the object pointed by the base class pointer. Thus, by making the base class pointer to point different objects, we can execute different versions of the virtual functions.
Rules of a virtual function:
- The virtual functions should be a member of some class.
- The virtual function cannot be a static member.
- Virtual functions are called by using the object pointer.
- It can be a friend of another class.
- C++ does not contain virtual constructors but can have a virtual destructor.
26.When should we use multiple inheritance?
You can answer this question in three manners:
- Never
- Rarely
- If you find that the problem domain cannot be accurately modeled any other way.
27. What is an overflow error?
It is a type of arithmetical error. It happens when the result of an arithmetical operation been greater than the actual space provided by the system.
28. What is a destructor?
A Destructor is used to delete any extra resources allocated by the object. A destructor function is called automatically once the object goes out of the scope.
Rules of destructor:
- Destructors have the same name as class name and it is preceded by tilde.
- It does not contain any argument and no return type.
29. What is overloading?
When a single object behaves in many ways is known as overloading. A single object has the same name, but it provides different versions of the same function.
C++ facilitates you to specify more than one definition for a function name or an operator in the same scope. It is called function overloading and operator overloading respectively.
Overloading is of two types:
Operator overloading: Operator overloading is a compile-time polymorphism in which a standard operator is overloaded to provide a user-defined definition to it. For example, '+' operator is overloaded to perform the addition operation on data types such as int, float, etc.
Operator overloading can be implemented in the following functions:
- Member function
- Non-member function
- Friend function
Syntax of Operator overloading:
Return_type classname :: Operator Operator_symbol(argument_list)
{
// body_statements;
}
Function overloading: Function overloading is also a type of compile-time polymorphism which can define a family of functions with the same name. The function would perform different operations based on the argument list in the function call. The function to be invoked depends on the number of arguments and the type of the arguments in the argument list.
30. What is function overriding?
If you inherit a class into a derived class and provide a definition for one of the base class's function again inside the derived class, then this function is called overridden function, and this mechanism is known as function overriding
31.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:
virtual void abc()=0; //pure virtual function
32. What is virtual inheritance?
Virtual inheritance facilitates you to create only one copy of each object even if the object appears more than one in the hierarchy.
33. What is the purpose of the "delete" operator?
The "delete" operator is used to release the dynamic memory created by "new" operator.
34. What is a constructor?
A Constructor is a special method that initializes an object. Its name must be same as class name.
35. Explain this pointer?
This pointer holds the address of the current object
36. What does Scope Resolution operator do?
A scope resolution operator(::) is used to define the member function outside the class.
37. 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.
38. 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".
|
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.
|
39. 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.
40. 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:
template<class T>
class classname
{
// body of class;
};
Syntax of a object of a template class:
classname<type> objectname(arg_list);
41. 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
42. Compare compile time polymorphism and
Runtime polymorphism?
The main
difference between compile-time and runtime polymorphism is provided below:
Compile-time
polymorphism
|
Run
time polymorphism
|
In this
method, we would come to know at compile time which method will be called.
And the call is resolved by the compiler.
|
In this
method, we come to know at run time which method will be called. The call is
not resolved by the compiler.
|
It provides
fast execution because it is known at the compile time.
|
It provides
slow execution compared to compile-time polymorphism because it is known at
the run time.
|
It is
achieved by function overloading and operator overloading.
|
It can be
achieved by virtual functions and pointers.
|
43.What do you know about friend class and friend function?
A friend class can access private, protected, and public members of other classes in which it is declared as friends.
Like friend class, friend function can also access private, protected, and public members. But, Friend functions are not member functions.
44. What are the C++ access specifiers?
In C++ there are the following access specifiers:
Public: All data members and member functions are accessible outside the class.
Protected: All data members and member functions are accessible inside the class and to the derived class.
Private: All data members and member functions are not accessible outside the class.
45. Define inline function?
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. One of the important advantages of using an inline function is that it eliminates the function calling overhead of a traditional function.
46. What do you mean by call by value and call by reference?
In call by value method, we pass a copy of the parameter is passed to the functions. For these copied values a new memory is assigned and changes made to these values do not reflect the variable in the main function.
In call by reference method, we pass the address of the variable and the address is used to access the actual argument used in the function call. So changes made in the parameter alter the passing argument
47. What is an abstract class and when do you use it?
A class is called an abstract class whose objects can never be created. Such a class exists as a parent for the derived classes. We can make a class abstract by placing a pure virtual function in the class
48. Can we call a virtual function from a constructor?
Yes, we can call a virtual function from a constructor. But the behavior is a little different in this case. When a virtual function is called, the virtual call is resolved at runtime. It is always the member function of the current class that gets called. That is the virtual machine doesn’t work within the constructor
49. What are void pointers?
A void pointer is a pointer which is having no datatype associated with it. It can hold addresses of any type.
example:
void *ptr;
char *str;
p=str;
str=p;
We can assign a pointer of any type to a void pointer but the reverse is not true unless you typecast it as
str=(char*) ptr;
50.What is this pointer in C++?
The member functions of every object have a pointer named this, which points to the object itself. The value of this is set to the address of the object for which it is called. It can be used to access the data in the object it points to.
Example
class Demo{
private:
int value;
public:
void setvalue(int x){
this->value=x;
}
};
int main(){
Demo d;
d.setvalue(5);
return 0;
}
51. what is type casting in C++?
Type casting in C is used to change the data type. They are of two types: Implicit Type Conversion: It is automatic. Explicit Type Conversion: It is user-defined
52. How to clear screen in C++?
One can clear screen using – clrscr() or system(“clear”).
53. Who developed C++?
Bjarne Stroustrup in 1998 at Bell Labs developed the lanuage C++.
54. How to compile and run C program in notepad++ ?
To compile and run c program in notepad++ follow the steps mentioned below:
Step-1: Download and install notepad++
Step-2: Download and install MinGw gcc along with gcc.
Step-3: Configure notepad++ for gcc. This step can be further divided into two sub-steps. A: Create C compiler tool in Notepad++
B: Creating C execution tool.
Step-4: Execute C program in Notepad++
55. How many keywords in C++ ?
There are 95 reserved keywords in C++ which are not available for re-definition or overloading.
56. Which operator cannot be overloaded in C++ ?
Some of the operators that cannot be overloaded are as follows:
- Dot operator- “.”
- Scope resolution operator- “::” .
- “sizeof” operator.
- Pointer to member operator- “.*” .
57. Why C++?
The use of C++ is varied such as:
- It is used in developing graphic user interface based applications like adobe photoshop.
- It is used in developing games as it overrides the complexity of 3D games.
- There are many animated softwares developed in C++
- Most of the compilers are written in C++.
- Google Chrome, Mozilla Firefox etc. web browser are developed using C++
58. What is stack in C++?
A linear data structure which implements all the operations (push, pop) in LIFO (Last In First Out) order. Stack can be implemented using either arrays or linked list.The operations in Stack are
- Push: adding element to stack.
- Pop: removing element from stack.
- isEmpty: returns true if stack is empty.
- Top: returns the top most element in stack.
59. What is exception in C++ ?
Runtime abnormal conditions that occur in the program are called exceptions. These are of 2 types:
Synchronous
Asynchronous
C++ has 3 specific keywords for handling these exceptions:
60. What is :: in C++?
:: is called a scope resolution operator which is used to access global variables with the same name as of local variables, for defining functions outside the class, for accessing static variables, and for referring to a class inside of another class.
61. What is conio.h in C++?
Conio.h is a header file used for console input and output operations and is used for creating text based user interfaces
62. What are the advantages of C++?
- Mid-level programming language.
- Portability.
- C++ has the concept of inheritance.
- Multi-paradigm programming language.
- Memory Management.
63. What is an expression in C++?
An expression is a combination of operators, constants and variables. There seven types of expressions for examples:
- Constant expressions: 89 +10/4.0 .
- Integral expressions: x * y.
- Floating expressions: 17.89.
- Relational expressions: a<=b.
- Logical expressions: a > b && a == 7.
- Pointer expressions: *ptr.
- Bitwise expressions: p << 5