C++ Interview Questions
C++ Interview Questions
C++ Interview Questions
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.
The main difference between C and C++ are provided in the table below:
C C++
C++ is an object-oriented programming
C is a procedure-oriented programming language.
language.
Data is hidden by encapsulation to ensure
C does not support data hiding. that data structures and operators are used
as intended.
C is a subset of C++ C++ is a superset of C.
Function and operator overloading are not Function and operator overloading is
supported in C supported in C++
Namespace is used by C++, which avoids
Namespace features are not present in C
name collisions.
Functions can be defined inside
Functions can not be defined inside structures.
structures.
calloc() and malloc() functions are used for new operator is used for memory
memory allocation and free() function is used for allocation and deletes operator is used for
memory deallocation. memory deallocation.
A class is a user-defined data type that has data members and member functions. Data
members are the data variables and member functions are the functions that are used to
perform operations on these variables.
An object is an instance of a class. Since a class is a user-defined data type so an object can
also be called a variable of that data type.
class A{
private:
int data;
public:
void fun(){
}
};
For example, the following is a class car that can have properties like name, color, etc. and
they can have methods like speed().
In C++ a structure is the same as a class except for a few differences like security. The
difference between struct and class are given below:
Structure Class
Members of the class are private by
Members of the structure are public by default.
default.
When deriving a struct from a class/struct, default access When deriving a class, default
specifiers for base class/struct are public. access specifiers are private.
For example -
The following code is for adding two complex number using operator overloading-
class complex{
private:
float r, i;
public:
complex(float r, float i){
this->r=r;
this->i=i;
}
complex(){}
void displaydata(){
cout<<”real part = “<<r<<endl;
cout<<”imaginary part = “<<i<<endl;
}
complex operator+(complex c){
return complex(r+c.r, i+c.i);
}
};
int main(){
complex a(2,3);
complex b(3,4);
complex c=a+b;
c.displaydata();
return 0;
}
Polymorphism in simple means having many forms. Its behavior is different in different
situations. And this occurs when we have multiple classes that are related to each other by
inheritance.
For example, think of a base class called a car that has a method called car brand(). Derived
classes of cars could be Mercedes, BMW, Audi - And they also have their own
implementation of a cars
Example:
class A{
private:
int val;
public:
A(int x){ //one argument constructor
val=x;
}
A(){ //zero argument constructor
}
}
int main(){
A a(3);
return 0;
}
8. Tell me about virtual function
Virtual function is a member function in the base class that you redefine in a derived class.
A virtual function is declared using the virtual keyword. When the function is made virtual,
C++ determines which function is to be invoked at the runtime based on the type of the
object pointed by the base class pointer.
The main difference between compile-time and runtime polymorphism is provided below:
return 0;
}
10. 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.
For example -
class A{
private:
int data_a;
public:
A(int x){
data_a=x;
}
friend int fun(A, B);
}
class B{
private:
int data_b;
public:
A(int x){
data_b=x;
}
friend int fun(A, B);
}
int fun(A a, B b){
return a.data_a+b.data_b;
}
int main(){
A a(10);
B b(20);
cout<<fun(a,b)<<endl;
return 0;
}
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.
For example-
int x=10;
int &ref=x; //reference variable
If we change the value of ref it will be reflected in x. Once a reference variable is initialized
it cannot refer to any other variable. We can declare an array of pointers but an array of
references is not possible.
Abstraction is the process of showing the essential details to the user and hiding the details
which we don’t want to show to the user or hiding the details which are irrelevant to a
particular user.
15. Is deconstructor overloading possible? If yes then explain and if no then why?
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.
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.
Example:
class A{
private:
int val;
public:
A(int x){
val=x;
}
A(){
}
~A(){ //destructor
}
}
int main(){
A a(3);
return 0;
}
19. What are the static members and static member functions?
When a variable in a class is declared static, space for it is allocated for the lifetime of the
program. No matter how many objects of that class have been created, there is only one
copy of the static member. So same static member can be accessed by all the objects of that
class.
A static member function can be called even if no objects of the class exist and the static
function are accessed using only the class name and the scope resolution operator ::
Inheritance is the process of creating new classes, called derived classes, from existing
classes. These existing classes are called base classes. The derived classes inherit all the
capabilities of the base class but can add new features and refinements of their own.
Example-
Inheritance in C++
Class Bus, Class Car, and Class Truck inherit the properties of Class Vehicle.
The most important thing about inheritance is that it permits code reusability.
21. What is the difference between virtual functions and pure virtual functions?
A virtual function is a member function in the base class that you redefine in a derived
class. It is declared using the virtual keyword.
Example-
class base{
public:
virtual void fun(){
}
};
Example-
class base{
public:
virtual void fun()=0;
};
Here, = sign has got nothing to do with the assignment, and value 0 is not assigned to
anything. It is used to simply tell the compiler that a function will be pure and it will not
have anybody.
22. If class D is derived from a base class B. When creating an object of type D in what
order would the constructors of these classes get called?
The derived class has two parts, a base part, and a derived part. When C++ constructs
derived objects, it does so in phases. First, the most-base class(at the top of the inheritance
tree) is constructed. Then each child class is constructed in order until the most-child class
is constructed last.
So the first Constructor of class B will be called and then the constructor of class D will be
called.
During the destruction exactly reverse order is followed. That is destructor starts at the
most-derived class and works its way down to base class.
So the first destructor of class D will be called and then the destructor of class B will be
called.
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.
For example-
class base{
private:
int value;
public:
base(int x){
value=x;
}
virtual void fun(){
}
}
class derived{
private:
int a;
public:
derived(int x, int y):base(x){
base *b;
b=this;
b->fun(); //calls derived::fun()
}
void fun(){
cout<<”fun inside derived class”<<endl;
}
}
24. 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.
For example-
void *ptr;
char *str;
p=str; // no error
str=p; // error because of type mismatch
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;
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 A{
private:
int value;
public:
void setvalue(int x){
this->value=x;
}
};
int main(){
A a;
a.setvalue(5);
return 0;
}
The new operator is used for memory allocation and deletes operator is used for memory
deallocation in C++.
For example-