C++ Programming Language Technical Interview Questions Answers

What is the full form of OOPS?

Object Oriented Programming System.

What is a class?

Class is a blue print which reflects the entities attributes and actions. Technically defining a class is designing an user defined data type.

What is an object?

An instance of the class is called as object.

List the types of inheritance supported in C++.

Single, Multilevel, Multiple, Hierarchical and Hybrid.

What is the role of protected access specifier?

If a class member is protected then it is accessible in the inherited class. However, outside the both the private and protected members are not accessible.

What is encapsulation?

The process of binding the data and the functions acting on the data together in an entity (class) called as encapsulation.

What is abstraction?

Abstraction refers to hiding the internal implementation and exhibiting only the necessary details.

What is inheritance?

Inheritance is the process of acquiring the properties of the existing class into the new class. The existing class is called as base/parent class and the inherited class is called as derived/child class.

C++ supports multiple inheritance. What is the “diamond problem” that can occur with multiple inheritance? Give an example.

It means that we cannot create hybrid inheritance using multiple and hierarchical inheritance.

Let’s consider a simple example. A university has people who are affiliated with it. Some are students, some are faculty members, some are administrators, and so on. So a simple inheritance scheme might have different types of people in different roles, all of whom inherit from one common “Person” class. The Person class could define an abstract getRole() method which would then be overridden by its subclasses to return the correct role type.

But now what happens if we want to model the role of a Teaching Assistant (TA)? Typically, a TA is both a grad student and a faculty member. This yields the classic diamond problem of multiple inheritance and the resulting ambiguity regarding the TA’s getRole() method:

(Note the diamond shape of the above inheritance diagram, hence the name.)

Which getRole() implementation should the TA inherit? That of the Faculty Member or that of the Grad Student? The simple answer might be to have the TA class override the getRole() method and return newly-defined role called “TA”. But that answer is also imperfect as it would hide the fact that a TA is, in fact, both a faculty member and a grad student.

Is it possible to have a recursive inline function?

Although you can call an inline function from within itself, the compiler may not generate inline code since the compiler cannot determine the depth of recursion at compile time. A compiler with a good optimizer can inline recursive calls till some depth fixed at compile-time (say three or five recursive calls), and insert non-recursive calls at compile time for cases when the actual depth gets exceeded at run time.

When you should use virtual inheritance?

While it’s ideal to avoid virtual inheritance altogether (you should know how your class is going to be used) having a solid understanding of how virtual inheritance works is still important:

So when you have a class (class A) which inherits from 2 parents (B and C), both of which share a parent (class D)

Explain the volatile and mutable keywords.

The volatile keyword informs the compiler that a variable may change without the compiler knowing it. Variables that are declared as volatile will not be cached by the compiler, and will thus always be read from memory.

The mutable keyword can be used for class member variables. Mutable variables are allowed to change from within const member functions of the class.

What is a storage class?

A class that specifies the life and scope of its variables and functions is called a storage class.
In C++ following the storage classes are supported: auto, static, register, extern, and mutable.
Note, however, that the keyword register was deprecated in C++11. In C++17, it was removed and reserved for future use.

Explain what is the use of void main () in C++ language?

To run the C++ application it involves two steps, the first step is a compilation where conversion of C++ code to object code take place. While second step includes linking, where combining of object code from the programmer and from libraries takes place. This function is operated by main () in C++ language.

Explain what are the characteristics of Class Members in C++?

Data and Functions are members in C++,
• Within the class definition, data members and methods must be declared
• Within a class, a member cannot be re-declare
• Other that in the class definition, no member can be added elsewhere

Define basic type of variable used for a different condition in C++?

The variable used for a different condition in C++ are

• Bool: Variable to store boolean values (true or false)
• Char: Variable to store character types
• int : Variable with integral values
• float and double: Types of variables with large and floating point values

Explain what are Operators and explain with an example?

Operators are specific operands in C++ that is used to perform specific operations to obtain a result. The different types of operators available for C++ are Assignment Operator, Compound Assignment Operator, Arithmetic Operator, Increment Operator and so on.

Explain what is Polymorphism in C++?

Polymorphism in C++ is the ability to call different functions by using only one type of the function call. Polymorphism is referred to codes, operations or objects that behave differently in a different context.

For example, the addition function can be used in many contests like

• 5+5  Integer addition
• Medical+Internship  The same ( + ) operator can be used with different meaning with strings
• 3.14 + 2.27  The same ( + ) operator can be used for floating point addition

Explain what is C++ exceptional handling?

The problem that arises during execution of a program is referred as exceptional handling. The exceptional handling in C++ is done by three keywords.

• Try: It identifies a block of code for which particular exceptions will be activated
• Catch: The catch keyword indicates the catching of an exception by an exception handler at the place in a program
• Throw: When a problem exists while running the code, the program throws an exception

Explain what is multi-threading in C++?

To run two or more programs simultaneously multi-threading is useful. There are two types of

Process-based: It handles the concurrent execution of the program
Thread-based: It deals with the concurrent execution of pieces of the same program

Explain what is upcasting in C++?

Upcasting is the act of converting a sub class references or pointer into its super class reference or pointer is called upcasting.

Explain what is pre-processor in C++?

Pre-processors are the directives, which give instruction to the compiler to pre-process the information before actual compilation starts.

Can we have a recursive inline function in C++?

Even though it is possible to call an inline function from within itself in C++, the compiler may not generate the inline code. This is so because the compiler won’t be able to determine the depth of the recursion at the compile time.

Nonetheless, a compiler with a good optimizer is able to inline recursive calls until some depth fixed at compile time, and insert non-recursive calls at compile time for the cases when the actual depth exceed at run time.

Explain ‘this’ pointer?

The ‘this’ pointer is a constant pointer and it holds the memory address of the current object. It passes as a hidden argument to all the nonstatic member function calls. Also, it is available as a local variable within the body of all the nonstatic functions.

As static member functions can be called even without any object, i.e. with the class name, the ‘this’ pointer is not available for them.

Why do we need the Friend class and function?

Sometimes, there is a need for allowing a particular class to access private or protected members of a class. The solution is a friend class, which is capable of accessing the protected as well as the private members of the class in which it is declared as a friend.

Similarly to the friend class, a friend function is able to access private and protected class members. A friend function can either be a global function or a method of some class.

Some important points about friend class and friend function:

1. Friendship is not inherited
2. Friendship isn’t mutual i.e. if some class called Friend is a friend of some other class called NotAFriend then it doesn’t automatically become a friend of the Friend class
3. The total number of friend classes and friend functions should be limited in a program as the overabundance of the same might lead to a depreciation of the concept of encapsulation of separate classes, which is an inherent and desirable quality of object-oriented programming.

How is function overloading different from operator overloading?

Function overloading allows two or more functions with different type and number of parameters to have the same name. Operator overloading, on the other hand, allows for redefining the way an operator works for user-defined types.

Comparison between C++ and Java

1. C++ has destructors, which are invoked automatically when an object is destroyed. Java has something called automatic garbage collection
2. C++ supports multiple inheritance, operator overloading, pointers, structures, templates, and unions. Java doesn’t have any of them
3. Java has a Thread class that is inherited in order to create a new thread. C++ has no inbuilt support for threads
4. In C++, a goto statement offers a way to jump from a location to some labeled statement in the same function. There is no goto statement in Java
5. C++ run and compile using the compiler, which converts the source code into machine level language. Hence, it is platform-dependent. Java compiler, on the other hand, converts the source code into JVM bytecode, which is platform-independent.

What are the most important differences between C and C++?

1. C++ supports references while C doesn’t
2. Features like friend functions, function overloading, inheritance, templates, and virtual functions are inherent to C++. These are not available in C programming language
3. In C, exception handling is taken care of in the traditional if-else style. On the other hand, C++ offers support for exception handling at the language level
4. Mainly used input and output in C are scanf() and printf(), respectively. In C++, cin is the standard input stream while cout serves as the standard output stream
5. While C is a procedural programming language, C++ provides support for both procedural and object-oriented programming approaches.

Explain Virtual Functions and the concept of Runtime Polymorphism in C++ with a code example.

Any function when accompanying the virtual keyword exhibits the behavior of a virtual function. Unlike normal functions that are called in accordance with the type of pointer or reference used, virtual functions are called as per the type of the object pointed or referred.
In simple terms, virtual functions resolve at runtime, not anytime sooner. Use of virtual functions could also be understood as writing a C++ program leveraging the concept of runtime polymorphism. Things essential to writing a virtual function in C++ are:

A base class
A derived class
A function with the same name in both the classes i.e. the base class and the derived class
A pointer or reference of base class type that points or refers, respectively to an object of the derived class

What differences separate structure from a class in C++?

There are two important distinctions between a class and a structure in C++. These are:

When deriving a structure from a class or some other structure, the default access specifier for the base class or structure is public. On the contrary, default access specifier is private when deriving a class.
While the members of a structure are public by default, the members of a class are private by default

What does a Static member in C++ mean?

Denoted by the static keyword, a static member is allocated storage, in the static storage area, only once during the program lifetime. Some important facts pertaining to the static members are:

Any static member function can’t be virtual
Static member functions don’t have ‘this’ pointer
The const, const volatile, and volatile declaration aren’t available for static member functions

What is the ‘diamond problem’ that occurs with multiple inheritance in C++? Explain using an example.

The diamond problem in C++ represents the inability of the programming language to support hybrid inheritance using multiple and hierarchical inheritance.

Suppose we have a university with some faculty members and some graduate students. A simple inheritance scheme in this scenario might have different types of people in different roles. However, all of them inherit from the same Person class.

The Person class defines an abstract getRole() method that would then be overridden by its subclasses in order to return the correct role type. Things up till this point is simple, however, if we wish to model the role of a TA or Teaching Assistant then things get weird.

A Teaching Assistant is both a student and a faculty member. This will yield the diamond problem, as illustrated in the figure below:

The problem generates an inheritance diagram resembling a diamond, hence the name, diamond problem.

Which getRole() implementation should the Teaching Assistant inherit? Graduate Student or the Faculty Member? A potential answer might be to have the Teaching Assistant class override the getRole() method and return a newly-defined role, say TA.

However, such an answer would also be far from complete as it will hide the fact that a Teaching Assistant is someone who is both a faculty member as well as a graduate student.

Comment on Local and Global scope of a variable.

The scope of a variable is defined as the extent of the program code within which the variable remains active i.e. it can be declared, defined or worked with.

There are two types of scope in C++:

Local Scope: A variable is said to have a local scope or is local when it is declared inside a code block. The variable remains active only inside the block and is not accessible outside the code block.
Global Scope: A variable has a global scope when it is accessible throughout the program. A global variable is declared on top of the program before all the function definitions.

What is a Constant? Explain with an example.

A constant is an expression that has a fixed value. They can be divided into integer, decimal, floating point, character or string constants depending on their data type.

Apart from decimal, C++ also supports two more constants i.e. octal (to the base 8) and hexadecimal (to the base 16) constants.

Examples of Constants:

75 //integer (decimal)
0113 //octal
0x4b //hexadecimal
3.142 //floating point
‘c’ //character constant
“Hello, World” //string constant

Note: When we have to represent a single character, we use single quotes and when we want to define a constant with more than one character, we use double quotes.

What is the difference between equal to (==) and Assignment Operator (=)?

In C++, equal to (==) and assignment operator (=) are two completely different operators.

Equal to (==) is equality relational operator that evaluates two expressions to see if they are equal and returns true if they are equal and false if they are not.

The assignment operator (=) is used to assign a value to a variable. Hence, we can have a complex assignment operation inside the equality relational operator for evaluation.

What are Default Parameters? How are they evaluated in C++ function?

Default parameter is a value that is assigned to each parameter while declaring a function.

This value is used if that parameter is left blank while calling to the function. To specify a default value for a particular parameter, we simply assign a value to the parameter in the function declaration.

If the value is not passed for this parameter during the function call, then the compiler uses the default value provided. If a value is specified, then this default value is stepped on and the passed value is used.

What is an Inline function in C++?

Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword “inline”.

Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.

State the difference between delete and delete[].

“delete[]” is used to release the memory allocated to an array which was allocated using new[].

“delete” is used to release one chunk of memory which was allocated using new.

What is a Storage Class? Mention the Storage Classes in C++.

Storage class determines the life or scope of symbols such as variable or functions.

C++ supports the following storage classes:


Explain the purpose of the keyword volatile.

Declaring a variable volatile directs the compiler that the variable can be changed externally. Hence avoiding compiler optimization on the variable reference.

What is an inline function?

A function prefixed with the keyword inline before the function definition is called as inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros.

What is a storage class?

Storage class specifies the life or scope of symbols such as variable or functions.

Mention the storage classes names in C++.

The following are storage classes supported in C++ − auto, static, extern, register and mutable.

What is the role of mutable storage class specifier?

A constant class object’s member variable can be altered by declaring it using mutable storage class specifier. Applicable only for non-static and non-constant member variable of the class.

Distinguish between shallow copy and deep copy.

Shallow copy does memory dumping bit-by-bit from one object to another. Deep copy is copy field by field from object to another. Deep copy is achieved using copy constructor and or overloading assignment operator.

What is a pure virtual function?

A virtual function with no function body and assigned with a value zero is called as pure virtual function.

What is an abstract class in C++?

A class with at least one pure virtual function is called as abstract class. We cannot instantiate an abstract class.

What is a reference variable in C++?

A reference variable is an alias name for the existing variable. Which mean both the variable name and reference variable point to the same memory location. Therefore updating on the original variable can be achieved using reference variable too.

What is role of static keyword on class member variable?

A static variable does exist though the objects for the respective class are not created. Static member variable share a common memory across all the objects created for the respective class. A static member variable can be referred using the class name itself.

Explain the static member function.

A static member function can be invoked using the class name as it exists before class objects comes into existence. It can access only static members of the class.

Name the data type which can be used to store wide characters in C++.


What are/is the operator/operators used to access the class members?

Dot (.) and Arrow ( -> )

Can we initialize a class/structure member variable as soon as the same is defined?

No, Defining a class/structure is just a type definition and will not allocated memory for the same.

What is the data type to store the Boolean value?

bool, is the new primitive data type introduced in C++ language.

What is function overloading?

Defining several functions with the same name with unique list of parameters is called as function overloading.

What is operator overloading?

Defining a new job for the existing operator w.r.t the class objects is called as operator overloading.

Do we have a String primitive data type in C++?

No, it’s a class from STL (Standard template library).

Name the default standard streams in C++.

cin, cout, cerr and clog.

Which access specifier/s can help to achive data hiding in C++?

Private & Protected.

When a class member is defined outside the class, which operator can be used to associate the function definition to a particular class?

Scope resolution operator (::)

What is a destructor? Can it be overloaded?

A destructor is the member function of the class which is having the same name as the class name and prefixed with tilde (~) symbol. It gets executed automatically w.r.t the object as soon as the object loses its scope. It cannot be overloaded and the only form is without the parameters.

What is a constructor?

A constructor is the member function of the class which is having the same as the class name and gets executed automatically as soon as the object for the respective class is created.

What is a default constructor? Can we provide one for our class?

Every class does have a constructor provided by the compiler if the programmer doesn’t provides one and known as default constructor. A programmer provided constructor with no parameters is called as default constructor. In such case compiler doesn’t provides the constructor.

Which operator can be used in C++ to allocate dynamic memory?

‘new’ is the operator can be used for the same.

What is the purpose of ‘delete’ operator?

‘delete’ operator is used to release the dynamic memory which was created using ‘new’ operator.

Can I use malloc() function of C language to allocate dynamic memory in C++?

Yes, as C is the subset of C++, we can all the functions of C in C++ too.

Can I use ‘delete’ operator to release the memory which was allocated using malloc() function of C language?

No, we need to use free() of C language for the same.

What is a friend function?

A function which is not a member of the class but still can access all the member of the class is called so. To make it happen we need to declare within the required class following the keyword ‘friend’.

What is a copy constructor?

A copy constructor is the constructor which take same class object reference as the parameter. It gets automatically invoked as soon as the object is initialized with another object of the same class at the time of its creation.

Does C++ supports exception handling? If so what are the keywords involved in achieving the same.

C++ does supports exception handling. try, catch & throw are keyword used for the same.

Explain the pointer – this.

This, is the pointer variable of the compiler which always holds the current active object’s address.

What is the difference between the keywords struct and class in C++?

By default the members of struct are public and by default the members of the class are private.

Can we implement all the concepts of OOPS using the keyword struct?


What is the block scope variable in C++?

A variable whose scope is applicable only within a block is said so. Also a variable in C++ can be declared anywhere within the block.

What is the role of the file opening mode ios::trunk?

If the file already exists, its content will be truncated before opening the file.

What is the scope resolution operator?

The scope resolution operator is used to − Resolve the scope of global variables. To associate function definition to a class if the function is defined outside the class.

What is a namespace?

A namespace is the logical division of the code which can be used to resolve the name conflict of the identifiers by placing them under different name space.

What are command line arguments?

The arguments/parameters which are sent to the main() function while executing from the command line/console are called so. All the arguments sent are the strings only.

What is a class template?

A template class is a generic class. The keyword template can be used to define a class template.

How can we catch all kind of exceptions in a single catch block?

The catch block with ellipses as follows − catch(…) { }

What is keyword auto for?

By default every local variable of the function is automatic (auto). In the below function both the variables ‘i’ and ‘j’ are automatic variables. void f() { int i; auto int j; } NOTE − A global variable can’t be an automatic variable.

What is a static variable?

A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice. void f() { static int i; ++i; printf(“%d “,i); } If a global variable is static then its visibility is limited to the same source code.

What is the purpose of extern storage specifier?

Used to resolve the scope of global symbol − #include <iostream> using namespace std; main() { extern int i; cout<<i<<endl; } int i=20;

What is the meaning of base address of the array?

The starting address of the array is called as the base address of the array.

When should we use the register storage specifier?

If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable.

Can a program be compiled without main() function?

Yes, it can be but cannot be executed, as the execution requires main() function definition.

Where an automatic variable is stored?

Every local variable by default being an auto variable is stored in stack memory

What is a container class?

A class containing at least one member variable of another class type in it is called so.

What is a token?

A C++ program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol.

What is a preprocessor?

Preprocessor is a directive to the compiler to perform certain things before the actual compilation process begins.

What are command line arguments?

The arguments which we pass to the main() function while executing the program are called as command line arguments. The parameters are always strings held in the second argument (below in args) of the function which is array of character pointers. First argument represents the count of arguments (below in count) and updated automatically by operating system. main( int count, char *args[]) { }

What are the different ways of passing parameters to the functions? Which to use when?

Call by value − We send only values to the function as parameters. We choose this if we do not want the actual parameters to be modified with formal parameters but just used.
Call by address − We send address of the actual parameters instead of values. We choose this if we do want the actual parameters to be modified with formal parameters.
Call by reference − The actual parameters are received with the C++ new reference variables as formal parameters. We choose this if we do want the actual parameters to be modified with formal parameters.

What is reminder for 5.0 % 2?

Error, It is invalid that either of the operands for the modulus operator (%) is a real number.

Which compiler switch to be used for compiling the programs using math library with g++ compiler?

Opiton –lm to be used as > g++ –lm <file.cpp>

Can we resize the allocated memory which was allocated using ‘new’ operator?

No, there is no such provision available.

Who designed C++ programming language?

Bjarne Stroustrup.

Which operator can be used to determine the size of a data type/class or variable/object?


How can we refer to the global variable if the local and the global variable names are same?

We can apply scope resolution operator (::) to the for the scope of global variable.

What are valid operations on pointers?

The only two permitted operations on pointers are Comparision ii) Addition/Substraction (excluding void pointers)

What is recursion?

Function calling itself is called as recursion.

What is the first string in the argument vector w.r.t command line arguments?

Program name.

What is the maximum length of an identifier?

Ideally it is 32 characters and also implementation dependent.

What is the default function call method?

By default the functions are called by value.

What are available mode of inheritance to inherit one class from another?

Public, private & protected

What is the difference between delete and delete[ ]?

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

Does an abstract class in C++ need to hold all pure virtual functions?

Not necessarily, a class having at least one pure virtual function is abstract class too.

Is it legal to assign a base class object to a derived class pointer?

No, it will be error as the compiler fails to do conversion.

What happens if an exception is thrown outside a try block?

The program shall quit abruptly.

Are the exceptions and error same?

No, exceptions can be handled whereas program cannot resolve errors.

What is function overriding?

Defining the functions within the base and derived class with the same signature and name where the base class’s function is virtual.

Which function is used to move the stream pointer for the purpose of reading data from stream?


Which function is used to move the stream pointer for the purpose of writing data from stream?


Are class functions taken into consideration as part of the object size?

No, only the class member variables determines the size of the respective class object.

Can we create and empty class? If so what would be the size of such object.

We can create an empty class and the object size will be 1.

What is ‘std’?

Default namespace defined by C++.

What is the full form of STL?

Standard template library

What is ‘cout’?

cout is the object of ostream class. The stream ‘cout’ is by default connected to console output device.

What is ‘cin’?

cin is the object of istream class. The stream ‘cin’ is by default connected to console input device.

What is the use of the keyword ‘using’?

It is used to specify the namespace being used in.

If a pointer declared for a class, which operator can be used to access its class members?

Arrow (→) operator can be used for the same

What is difference between including the header file with-in angular braces < > and double quotes “ “?

If a header file is included with in < > then the compiler searches for the particular header file only with in the built in include path. If a header file is included with in “ “, then the compiler searches for the particular header file first in the current working directory, if not found then in the built in include path

S++ or S=S+1, which can be recommended to increment the value by 1 and why?

S++, as it is single machine instruction (INC) internally.

What is the difference between actual and formal parameters?

The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters.

What is the difference between variable declaration and variable definition?

Declaration associates type to the variable whereas definition gives the value to the variable.

Which key word is used to perform unconditional branching?


Is 068 a valid octal number?

No, it contains invalid octal digits.

What is the purpose of #undef preprocessor?

It will be used to undefine an existing macro definition.

Can we nest multi line comments in a C++ code?

No, we cannot.

What is a virtual destructor?

A virtual destructor ensures that the objects resources are released in the reverse order of the object being constructed w.r.t inherited object.

What is the order of objects destroyed in the memory?

The objects are destroyed in the reverse order of their creation.

What is a friend class?

A class members can gain accessibility over other class member by placing the class declaration prefixed with the keyword ‘friend’ in the destination class.

When to use “const” reference arguments in a function?

Using “const” reference arguments in a function is beneficial in several ways:

“const” protects from programming errors that could alter data.

As a result of using “const”, the function is able to process both const and non-const actual arguments, which is not possible when “const” is not used.

Using a const reference, allows the function to generate and use a temporary variable in an appropriate manner.

Difference between Class and Structure.

In C language, the structure is used to bundle different type of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name.

Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default all the members inside the class are private.

What is the difference between an Object and a Class?

Class is a blueprint of a project or problem to be solved and consists of variables and methods. These are called the members of the class. We cannot access methods or variables of the class on its own unless they are declared static.

In order to access the class members and put them to use, we should create an instance of a class which is called an Object. The class has an unlimited lifetime whereas an object has a limited lifespan only.

What is a Constructor and how is it called?

Constructor is a member function of the class having the same name as the class. It is mainly used for initializing the members of the class. By default constructors are public.

There are two ways in which the constructors are called:

Implicitly: Constructors are implicitly called by the compiler when an object of the class is created. This creates an object on a Stack.
Explicit Calling: When the object of a class is created using new, constructors are called explicitly. This usually creates an object on a Heap.

What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.

What is the role of Static keyword for a class member variable?

Static member variable shares a common memory across all the objects created for the respective class. We need not refer to the static member variable using an object. However, it can be accessed using the class name itself.

Explain the Static Member Function.

A static member function can access only the static member variable of the class. Same as the static member variables, a static member function can also be accessed using the class name.

Explain Function Overloading and Operator Overloading.

C++ supports OOPs concept Polymorphism which means “many forms”.

In C++ we have two types of polymorphism, i.e. Compile-time polymorphism, and Run-time polymorphism. Compile time polymorphism is achieved by using an Overloading technique. Overloading simply means giving additional meaning to an entity by keeping its base meaning intact.

C++ supports two types of overloading:

Function Overloading:
Function overloading is a technique which allows the programmer to have more than one function with the same name but different parameter list. In other words, we overload the function with different arguments i.e. be it the type of arguments, number of arguments or the order of arguments.
Function overloading is never achieved on its return type.

Operator Overloading:
This is yet another type of compile-time polymorphism that is supported by C++. In operator overloading, an operator is overloaded, so that it can operate on the user-defined types as well with the operands of the standard data type. But while doing this, the standard definition of that operator is kept intact.
For Example, Addition operator (+) that operates on numerical data types can be overloaded to operate on two objects just like an object of complex number class.

What is the difference between Method Overloading and Method Overriding in C++?

Method overloading is having functions with the same name but different argument list. This is a form of compile-time polymorphism.

Method overriding comes into picture when we rewrite the method that is derived from a base class. Method overriding is used while dealing with run-time polymorphism or virtual functions.

Name the Operators that cannot be Overloaded.

sizeof – sizeof operator
. – Dot operator
.* – dereferencing operator
-> – member dereferencing operator
:: – scope resolution operator
?: – conditional operator

What are the benefits of Operator Overloading?

By overloading standard operators on a class, we can extend the meaning of these operators, so that they can also operate on the other user-defined objects.

Function overloading allows us to reduce the complexity of the code and make it more clear and readable as we can have the same function names with different argument lists.

What are the advantages of Inheritance?

Inheritance allows code re-usability, thereby saving time on code development.

By inheriting, we make use of a bug-free high-quality software that reduces future problems.

What are Multiple Inheritances (virtual inheritance)? What are its advantages and disadvantages?

In multiple inheritances, we have more than one base classes from which a derived class can inherit. Hence, a derived class takes the features and properties of more than one base class.

For Example, a class driver will have two base classes namely, employee and a person because a driver is an employee as well as a person. This is advantageous because the driver class can inherit the properties of the employee as well as the person class.

But in the case of an employee and a person, the class will have some properties in common. However, an ambiguous situation will arise as the driver class will not know the classes from which the common properties should be inherited. This is the major disadvantage of multiple inheritance.

Explain the ISA and HASA class relationships. How would you implement each?

“ISA” relationship usually exhibits inheritance as it implies that a class “ISA” specialized version of another class. Example, An employee ISA person. That means an Employee class is inherited from the Person class.

Contrary to “ISA”, “HASA” relationship depicts that an entity may have another entity as its member or a class has another object embedded inside it.

So taking the same example of an Employee class, the way in which we associate the Salary class with the employee is not by inheriting it but by including or containing the Salary object inside the Employee class. “HASA” relationship is best exhibited by containment or aggregation.

What is Polymorphism?

The basic idea behind polymorphism is in many forms. In C++, we have two types of Polymorphism:

(i) Compile time Polymorphism
In compile time polymorphism, we achieve many forms by overloading. Hence, we have Operator overloading and function overloading. (We have already covered this above)

(ii) Run-time Polymorphism
This is the polymorphism for classes and objects. General idea is that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects.

This means, that an object reacts differently to the same function call. This type of polymorphism can use virtual function mechanism.

What are Virtual Functions?

A virtual function allows the derived classes to replace the implementation provided by the base class.

Whenever we have functions with the same name in the base as well as derived class, there arises an ambiguity when we try to access the child class object using a base class pointer. As we are using a base class pointer, the function that is called is the base class function with the same name.

To correct this ambiguity we use the keyword “virtual” before the function prototype in the base class. In other words, we make this polymorphic function Virtual. By using a Virtual function, we can remove the ambiguity and we can access all the child class functions correctly using a base class pointer.

What is a friend function?

C++ class does not allow its private and protected members to be accessed outside the class. But this rule can be violated by making use of the “Friend” function.

As the name itself suggests, friend function is an external function which is a friend of the class. For friend function to access the private and protected methods of the class, we should have a prototype of the friend function with the keyword “friend” included inside the class.

What is a friend class?

Friend classes are used when we need to override the rule for private and protected access specifiers so that two classes can work closely with each other.

Hence, we can have a friend class to be the friend of another class. This way, friend classes can keep private, inaccessible things in the way they are.

When we have a requirement to access the internal implementation of a class (private member) without exposing the details by making the public, we go for friend functions.

What is Exception Handling? Does C++ support Exception Handling?

Yes C++ supports exception handling.

We cannot ensure that code will execute normally at all times. There can be certain situations which might force the code written by us to malfunction, even though it’s error-free. This malfunctioning of code is called Exception.

When an exception has occurred, the compiler has to throw it so that we know an exception has occurred. When an exception has been thrown, the compiler has to ensure that it is handled properly, so that the program flow continues or terminates properly. This is called handling of an exception.

Thus in C++, we have three keywords i.e. try, throw and catch which are in exception handling.

The code that might potentially malfunction is put under the try block. When code malfunctions, an exception is thrown. This exception is then caught under the catch block and is handled i.e. appropriate action is taken.

What is a Standard Template Library (STL)? What are the various types of STL Containers?

Standard Template Library (STL) is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification. We have various types of STL containers depending on how they store the elements.

Queue, Stack – These are the same as traditional queue and stack and are called adaptive containers.
Set, Map – These are basically containers that have key/value pairs and are associative in nature.
Vector, deque – These are sequential in nature and have similarity to arrays.

Previous Post Next Post