Professional C++ Virtual Functions Interview Preparation Guide
Download PDF

C++ Virtual Functions frequently Asked Questions in various C++ virtual functions job Interviews by interviewer. The set of questions here ensures that you offer a perfect answer posed to you. So get preparation for your new job hunting

30 C++ Virtual Functions Questions and Answers:

Table of Contents:

Professional  C++ Virtual Functions Job Interview Questions and Answers
Professional C++ Virtual Functions Job Interview Questions and Answers

1 :: What is the use of Vtable?

Vtables are used for virtual functions. Its a shortform for Virtual Function Table.
It's a static table created by the compiler. Compiler creates a static table per class and the data consists on pointers to the virtual function definitions. They are automatically initialised by the compiler's constructor code.

2 :: Explain virtual destructor?

If the destructor in the base class is not made virtual, then an object that might have been declared of type base class and instance of child class would simply call the base class destructor without calling the derived class destructor.

3 :: Explain data encapsulation?

The wrapping up of data and functions into a single unit (Class) is known as Encapsulation. By encapsulating the data inside the class; data is not accessible to the outside world.

4 :: Explain polymorphism?

Polymorphism means the ability to take more than one form. An operation may exhibit different behavior in different instances. The behavior depends on the types of data used in the operation.

5 :: Tell me can a pure virtual function have an implementation?

The quick answer to that question is yes! A pure virtual function can have an implementation in C++ - which is something that even many veteran C++ developers do not know. So, using the SomeClass class from our example above, we can have the following code:

class SomeClass {
public:
virtual void pure_virtual() = 0; // a pure virtual function
// note that there is no function body
};

/*This is an implementation of the pure_virtual function
which is declared as a pure virtual function.
This is perfectly legal:
*/
void SomeClass::pure_virtual() {
cout<<"This is a test"<<endl;
}< /endl;

6 :: Give example of a pure virtual function in C++?

class SomeClass {
public:
virtual void pure_virtual() = 0; // a pure virtual function
// note that there is no function body
};

7 :: Can you please explain the difference between using macro and inline functions?

A textual substitution is provided by a macro as a constant, where as an inline function is procedure which is called at each time. Although the macros have few advantages over inline functions, the disadvantages are numerous.

8 :: Explain object slicing in C++?

When a Derived Class object is assigned to Base class, the base class' contents in the derived object are copied to the base class leaving behind the derived class specific contents. This is referred as Object Slicing. That is, the base class object can access only the base class members. This also implies the separation of base class members from derived class members has happened.

class base
{
public:
int i, j;
};
class derived : public base
{
public:
int k;
};
int main()
{
base b;
derived d;
b=d;
return 0;
}
here b contains i and j where as d contains i, j& k. On assignment only i and j of the d get copied into i and j of b. k does not get copied. on the effect object d got sliced.

9 :: What is problem with overriding functions?

Overriding of functions occurs in Inheritance. A derived class may override a base class member function. In overriding, the function names and parameter list are same in both the functions.

10 :: Do you know what is overriding?

Defining a function in the derived class with same name as in the parent class is called overriding. In C++, the base class member can be overridden by the derived.

11 :: Do you know what are static and dynamic type checking?

Static type checking performs the type checking operation before the execution of the program. To perform this operation, the arguments, expressions, variables must be given a data type.

12 :: Tell me what are static member functions?

Static member functions are used to maintain a single copy of a class member function across various objects of the class. Static member functions can be called either by itself, independent of any object, by using class name and :: (scope resolution operator) or in connection with an object.

13 :: Can you please explain the difference between static and dynamic binding of functions?

Static Binding:
By default, matching of function call with the correct function definition happens at compile time. This is called static binding or early binding or compile-time binding. Static binding is achieved using function overloading and operator overloading. Even though there are two or more functions with same name, compiler uniquely identifies each function depending on the parameters passed to those functions.

Dynamic Binding:
C++ provides facility to specify that the compiler should match function calls with the correct definition at the run time; this is called dynamic binding or late binding or run-time binding. Dynamic binding is achieved using virtual functions. Base class pointer points to derived class object. And a function is declared virtual in base class, then the matching function is identified at run-time using virtual table entry.

14 :: What is general form of pure virtual function? Explain?

A pure virtual function is a function which has no definition in the base class. Its definition lies only in the derived class ie it is compulsory for the derived class to provide definition of a pure virtual function. Since there is no definition in the base class, these functions can be equated to zero.

The general form of pure virtual function is:

virtual type func-name(parameter-list) = 0;

Consider following example of base class Shape and classes derived from it viz Circle, Rectangle, Triangle etc.

class Shape
{
int x, y;
public:
virtual void draw() = 0;
};

class Circle: public Shape
{
public:
draw()
{
//Code for drawing a circle
}
};

class Rectangle: public Shape
{
Public:
void draw()
{
//Code for drawing a rectangle
}
};

class Triangle: public Shape
{
Public:
void draw()
{
//Code for drawing a triangle
}
};

Thus, base class Shape has pure virtual function draw(); which is overridden by all the derived classes.

15 :: Do you know what are pure virtual functions?

Pure virtual functions are also called 'do nothing functions'.
e.g. virtual void abc() = 0;
When a pure virtual fnction is declared in the base class, the compiler necessitates the derived classes to define those functions or redeclare them are pure virtual functions. The classes containing pure virtual functions cannot be used to declare objects of their own. Such classes are called as abstract base classes.

16 :: Explain the problem with overriding functions?

Overriding of functions occurs in Inheritance. A derived class may override a base class member function. In overriding, the function names and parameter list are same in both the functions.

17 :: What is Virtual base class uses?

When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class' name with the word virtual.

Consider following example:

class A
{
public:
int i;
};

class B : virtual public A
{
public:
int j;
};

class C: virtual public A
{
public:
int k;
};

class D: public B, public C
{
public:
int sum;
};

int main()
{
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << "Value of i is : "<< ob.i<<"n";
cout << "Value of j is : "<< ob.j<<"n"; cout << "Value of k is :"<< ob.k<<"n";
cout << "Sum is : "<< ob.sum <<"n";

return 0;
}.

18 :: What is a virtual base class?

An ambiguity can arise when several paths exist to a class from the same base class. This means that a child class could have duplicate sets of members inherited from a single base class.

19 :: What is virtual methods?

virtual methods are used to use the polymorphism feature in C++. Say class A is inherited from class B. If we declare say function f() as virtual in class B and override the same function in class A then at run time appropriate method of the class will be called depending upon the type of the object.

20 :: Explain the pure virtual functions?

A pure virtual function is a function which has no definition in the base class. Its definition lies only in the derived class ie it is compulsory for the derived class to provide definition of a pure virtual function. Since there is no definition in the base class, these functions can be equated to zero.

The general form of pure virtual function is:

virtual type func-name(parameter-list) = 0;

Consider following example of base class Shape and classes derived from it viz Circle, Rectangle, Triangle etc.

class Shape
{
int x, y;
public:
virtual void draw() = 0;
};

class Circle: public Shape
{
public:
draw()
{
//Code for drawing a circle
}
};

class Rectangle: public Shape
{
Public:
void draw()
{
//Code for drawing a rectangle
}
};

class Triangle: public Shape
{
Public:
void draw()
{
//Code for drawing a triangle
}
};

Thus, base class Shape has pure virtual function draw(); which is overridden by all the derived classes.

21 :: What is virtual base class?

When two or more objects are derived from a common base class, we can prevent multiple copies of the base class being present in an object derived from those objects by declaring the base class as virtual when it is being inherited. Such a base class is known as virtual base class. This can be achieved by preceding the base class’ name with the word virtual.

Consider following example:

class A
{
public:
int i;
};

class B : virtual public A
{
public:
int j;
};

class C: virtual public A
{
public:
int k;
};

class D: public B, public C
{
public:
int sum;
};

int main()
{
D ob;
ob.i = 10; //unambiguous since only one copy of i is inherited.
ob.j = 20;
ob.k = 30;
ob.sum = ob.i + ob.j + ob.k;
cout << “Value of i is : ”<< ob.i<<”\n”;
cout << “Value of j is : ”<< ob.j<<”\n”; cout << “Value of k is :”<< ob.k<<”\n”;
cout << “Sum is : ”<< ob.sum <<”\n”;

return 0;
}.

22 :: Do you know the use of Vtable?

Vtables are used for virtual functions. Its a shortform for Virtual Function Table.

It's a static table created by the compiler. Compiler creates a static table per class and the data consists on pointers to the virtual function definitions. They are automatically initialised by the compiler's constructor code.

Since virtual function pointers are stored in each instance, the compiler is
enabled to call the correct vrtual function at runtime.

23 :: What is Dynamic Binding?

C++ provides facility to specify that the compiler should match function calls with the correct definition at the run time; this is called dynamic binding or late binding or run-time binding. Dynamic binding is achieved using virtual functions. Base class pointer points to derived class object. And a function is declared virtual in base class, then the matching function is identified at run-time using virtual table entry.

24 :: What is Static Binding?

By default, matching of function call with the correct function definition happens at compile time. This is called static binding or early binding or compile-time binding. Static binding is achieved using function overloading and operator overloading. Even though there are two or more functions with same name, compiler uniquely identifies each function depending on the parameters passed to those functions.

25 :: Do you know the problem with overriding functions?

Overriding of functions occurs in Inheritance. A derived class may override a base class member function. In overriding, the function names and parameter list are same in both the functions. Depending upon the caller object, proper function is invoked.

Consider following sample code:

class A
{
int a;
public:
A()
{
a = 10;
}
void show()
{
cout << a;
}
};

class B: public A
{
int b;
public:
B()
{
b = 20;
}
void show()
{
cout << b;
}
};

int main()
{
A ob1;
B ob2;
ob2.show(); // calls derived class show() function. o/p is 20
return 0;
}

As seen above, the derived class functions override base class functions. The problem with this is, the derived class objects can not access base class member functions which are overridden in derived class.

Base class pointer can point to derived class objects; but it has access only to base members of that derived class.

Therefore, pA = &ob2; is allowed. But pa->show() will call Base class show() function and o/p would be 10.
C++ Virtual Functions Interview Questions and Answers
30 C++ Virtual Functions Interview Questions and Answers