Home >>Interview Questions >C++ Interview Questions
Bjarne Stroustrup is known as the founder and developer of the C++ language that is basically an object-oriented programming language. 1985 was the year in which it was released. In the starting phase the C++ language was called as”C with classes” because of the fact that this language is basically a superset of C with the major addition of classes in the C language. The idea of the existing name of the C++ language came from the C increment operator ++ and after a while this was renowned as the permanent name of the language that is C++.
C++ language has many features that are hard to enlist all at once. C++ language is known to maintain every aspects of the C language and to simplify memory management. Here are the most important advantages of the C++ language:
Here are the most common differences between C and C++:
C | C++ |
---|---|
Dennis Ritchie is known as the developer of the C language. | Bjarne Stroustrup is known as the developer and founder of the C++ language. |
C is generally known as a structured programming language. | C++ is generally known as the language that supports both structural and object-oriented programming language. |
C is generally referred as a subset of C++. | C++ is generally referred as a superset of C. |
The data and functions are the free entities in the C language. | The data and the functions are generally encapsulated together in the form of a project in the C++ language. |
The data hiding is not supported by the C language hence, the data can be used generally by the outside world. | Data hiding is generally supported by the C++ language hence, the data cannot be accessed by the outer world. |
Neither the function nor operator overloading is supported by the C language. | Both function and operator overloading is generally supported by the C++ language. |
The function cannot be implemented inside the structures in the C language. | The function can generally be implemented inside the structures in the C++ language. |
In the C language, reference variables are not supported. | The reference variables are generally supported by the C++ language. |
The virtual and friend functions are not supported by the C language. | The virtual and friend functions both are supported by the C++ language. |
scanf() and printf() are generally used for input/output in the C language. | Stream cin and cout are generally used in the C++ language in order to perform input and output operations. |
Here are the various concepts that are in the OOPs in the C++ language:
The class in C++ is basically a data type that is user-defined data type that generally defines the properties and functions of the class. For instance, Human being can be defined as a class. The properties of human being will be the body parts and the functions will be the actions that will be performed by the body parts. Any memory space is generally not occupied by the class. Hence, it can be said that the only representation of the data is the class.
Reusability is generally provided by the inheritance. By reusability it is meant that the functionalities of the existing class can be used by the users. It is generally known to eliminate the redundancy of the code. A technique that is used to derive a new class from the old class is known as inheritance, where the old class is called as the base class and the new class is referred as the derived class.
A run-time entity in C++ is basically known as an Object. The instance of the class can also be referred as the object and they can be used in order to represent a person, place or any other item. Operations on both the data members and the member functions can be performed by an object. As we know that the class does not occupy any of the memory space and the space is allocated for the variable in a heap whenever an object is created generally by the use of a new keyword after this in the stack memory the starting address is stored. And whenever an object is created without the use of a new keyword, then the space is not allocated in the heap memory and hence the object consists of the null value that is in the stack.
The technique by which the wrapping of the data members and member functions is done in to a single unit is known as Encapsulation. It mainly does the work of binding the data within a class except any outside method that can access the data. The member function is only allowed to access the data if and only if the data member is private.
The technique by which only the essential details are shown without even representing the implementation details is known as the Abstraction. The members can be accessed from the outside also provided that the members are defined with a public keyword. The members cannot be accessed from the outside methods, if the members are defined with a private keyword
The process by which the application UI and business logic are banded is known as Data binding. The application UI will automatically reflect any changes that are made in the business logic.
Polymorphism basically means multiple forms. In other words, Polymorphism defines the fact of having more than one function that are having the same name and having very different functionalities. Polymorphism is basically of two types that are depicted below:
Polymorphism basically means multiple forms. In other words, Polymorphism defines the fact of having more than one function that are having the same name and having very different functionalities.
Polymorphism is generally of two types in the C++ programming language:
Runtime polymorphism is generally called as the dynamic polymorphism. In order to learn this in the layman’s terms, it can be said that the function overriding is basically an example of runtime polymorphism. Function overriding basically elaborates to the fact of the presence of the same method in the parent class that is contained by the child class. Because of it the child class will override the method of the parent class. Parent and child class both will contain the same function just with the different definitions, whenever there is a case of function overriding. Runtime polymorphism is basically the call to the function that is generally determined at the runtime.
Compile-time polymorphism is generally called as the static polymorphism in C++. Compile-time polymorphism is basically the polymorphism that is implemented at the time of compiling. In order to understand the compile-time polymorphism in layman’s terms it can be said that one of its example is Method overloading.
Method overloading: A technique that permits you to have more than one function that too with the exact same function name but the functionality will be different is known to be the Method Overloading.
The possibility of the Method overloading depends on the following basis:
A token in C++ programming language can be defined as a keyword, literal, identifier constant and symbol.
Bjarne Stroustrup is known as the creator of the C++ programming language.
Here are the operations that can be performed generally on the pointers in C++:
1. Incrementing or decrementing a pointer: Incrementing a pointer basically means that the programmer has the access to increment a particular pointer by the size of a data type towards which it is pointing.
Increment pointers are two types in C++:
a. Pre-increment pointer: The operand is generally incremented by 1 by the pre-increment operator, and the value of the expression takes the place of the resulting value of the incremented operand.
b. Post-increment pointer: The operand is generally incremented by 1 by the post-increment operator and the value of the expression becomes the value of the operand that is generally prior to the incremented value of the provided operand.
Std can generally be defined as the default namespace standard that is majorly used in C++programming language.
The disadvantages of C and the inability of it towards the object oriented programming led to the discovery of C++ programming language.
In order to release a unit of memory delete is used in the C++ programming language. Delete[] is also majorly used in order to release an array.
STL in C++ expands to Standard Template Library.
The instance of a class is generally known as the Object in the C++ programming language. In order to create an object from a class in C++, a blueprint for objects is generally provided by the class. The same declaration that is used by the programmers to declare variables of basic types in C++ is used to declare the objects of a class.
In order to define the process of accessing the functions and variables outside the class, the access specifiers are used in the C++ programming language.
There are generally three types of access specifiers that are present in the C++:
A methodology or paradigm that generally provides various concepts is known as OOP in C++ programming language. Here are the basic concepts that are included in the Object Oriented Programming is depicted below:
Here are the difference between the arrays and the list in the C++ programming language:
Here are the list of differences that are present between the new() and malloc() in the C++ programming language:
There are generally two ways by which a function can be exported from a DLL in C++ programming language:
There are three simple ways to answer this question:
As the name suggests, the friend function, generally behaves as a friend of the class in C++ programming language. The private and protected members of the class are generally accessed by the friend function. It is mandatory to list the friend function in the class definition even when we know for a fact that it is not even a member of the class. The private data of the class cannot be accessed by the non-member functions. But in various times it is seen that accessing the data becomes mandatory for the non-member functions. And as a fact, the friend function also belongs to the family of the non-member function and as discussed above it possesses the ability to access the data of the class that has been specified as private.
To make an outside function friendly to the class, we need A function is needed to be declared the function as a friend of the class, in order to make an outside function friendly to the class.
Here are the characteristics that are important for the friend function in C++:
In order to delete any extra resources that have been allocated by the object, the destructor is used in C++. Whenever the object goes out of the scope, the destructor function is called automatically.
Here are some of the rules of the destructor that are generally implemented:
Overflow error in C++ programming language is basically a type of arithmetical error that generally happens whenever the outcome of an arithmetical operation comes out to be greater than the actual space that has been provided by the system.
Overloading is basically a single object that behaves in numerous ways in C++. In simple words it can be understood as, a single object that has the exact same name instead of this it provides the different versions of the exact same function. The C++ programming language generally facilitates a privilege to the programmers by which then can specify more than one definition for a function name or an operator that is in the same scope and it is known as the function overloading and operator overloading respectively.
There are generally two types of overloading that are depicted below:
Here are the ways in which the operator overloading can be implemented:
Whenever a class is inherited into a derived class by the programmer and a definition is provided for one of the base class's function repeatedly inside the derived class, then this function is known to be the overridden function in the C++ programming language. And the mechanism that is responsible for driving all this is called as the function overriding.
Whenever there is a need to create one copy of each object only, even when the object makes the appearance more than once in the order of hierarchy in the C++ programming language then the Virtual inheritance is used.
A special method that is generally used to initialize an object is known as the Constructor. Please note that the name of the constructor must be the same as the class name.
In order to release the dynamic memory that has been created by the "new" operator, the “delete” operator is generally used in the C++ programming language.
The address of the current object is generally held by the ‘This pointer’ in C++ language.
In order to define the member function outside of the class, the scope resolution operator that is generally denoted by the double colon (::) is used.
In order to release the array of allocated memory that has been allocated by the use of new[] at the first place, Delete [] is used. And on the other hand delete is generally used to release only one chunk of memory that has been allocated by the use of new.
As the name suggests it is a virtual function that generally does not consists of any definition is known as the pure virtual function in C++ programming language. The pure virtual function generally ends with 0, whereas the normal function in C++ is preceded with a keyword virtual.
There are many differences that are present between the struct and class in the C++ programming language, here is the list of differences depicted below:
structures | Class |
---|---|
A user-defined data type that consists of the variables that are of dissimilar data types is known as the structure in C++. | A user-defined data type that generally consists of the member variables and the member functions is known as class in C++ language. |
The stack memory is the place where all the variables of a structure are stored. | The heap memory is the place where the variables of a class are stored. |
The variables cannot be initialized directly by the programmers. | The variables can be initialized directly by the programmers. |
The access specifier of the variable is set to "public" by default, in case the access specifier is not specified. | The access specifier of the variable is set to "private" by default, in case the access specifier is not specified. |
The "structure variable" is just an instance of the structure. | |
Here is how the structure is declared in C++: struct structure_name { // body of structure; } ; | Here is how the class is declared in C++: class class_name { // body of class; } |
By the use of a struct keyword, a structure is declared in the C++ language. | By the use of a class keyword, a class is declared in the C++ language. |
The inheritance is generally not supported by the structure. | The concept of inheritance is generally supported by the structure. |
A structure's type is basically a value type. | A class's type is basically a reference type. |
In order to create a family of classes and functions in the C++ programming language, a class template is used. For instance, a template of an array class can be created by the programmer that will enable them to create an array that will be of various types like int, float, char, etc. simultaneously, the programmer can create a template for a function, as for implementation, let’s suppose a programmer possesses a function add () and by that he/she can able to create the multiple versions of add ().
Function overloading: Function overloading is generally defined when the programmer has more than one version of the exact same function. The versions of a function can generally have a different set of parameters in other words it can be said that the versions of a function will have different signature every time.
Operator overloading: The standard operator in C++ can generally be redefined in order to have a very different meaning, whenever it gets applied to the instances of a class. This scenario in C++ programming language is known as the Operator overloading.
In order to destroy the derived class object, the virtual destructor in C++ programming language is generally used in the base class. There is a process of declaring the virtual destructor, It is generally declared by the use of tilde (~) followed by the virtual keyword before the constructor.
A user-defined data type is known as a Class in the C++ language. In order to declare a class you have to use the keyword class. The class in C++ generally contains the data members, and member functions and their access is generally defined by these three modifiers that are: private, public and protected. The type definition of the category of things is defined by the class in C++ language. The class in C++ is used to define a data type, but when it defining the data the class just specifies the structure of data. Programmers can create N number of objects from a class in C++.
Here are the major differences between reference and pointer:
Reference | Pointer |
---|---|
Reference are basically a temporary variable and hence they generally behaves like an alias for an existing variable. | A variable that stores the address of a variable is known as a pointer. |
Any indirection operator is not required by the reference in order to access the value. In order to directly access the value a reference variable is used. | An indirection operator is required by the pointer variable in order to access the value of a variable. |
In references, if the reference variable is assigned once then it cannot be reassigned with different address values. | The pointer variable is generally known as an independent variable that means, it can be reassigned to point towards the different objects. |
A null value cannot be get assigned to the reference variable. | A null value can generally be assigned to the reference variable. |
It is mandatory to initialize the variable just at the time of declaration. | It is not at all mandatory to initialize the variable just at the time of declaration. |
Virtual functions are basically called as per the type of the object pointed or referred instead of being called according to the type of pointer or reference and they are generally used with inheritance. In simple words, virtual functions are known to get resolved late basically at the runtime. In order to make a function virtual, the virtual function is used.
Here are the things that are necessary to write a C++ program with runtime polymorphism (also known as the use of virtual functions):