C++ Interview Questions on Virtual Function and Abstract Class
Last Updated :
01 Nov, 2024
Diving into the depths of C++ programming requires a solid grasp of some of its most important topics, such as virtual functions and abstract classes. These concepts are not only fundamental to mastering the language but are also frequent subjects in technical interviews.
In this interview preparation guide, you will explore a comprehensive list of C++ interview questions specifically focused on virtual functions and abstract classes. These questions are designed to test and expand your knowledge of C++’s object-oriented programming capabilities, from basic definitions to complex scenario-based queries. Perfect for both beginners and experienced developers.
List of CPP Interview Questions on Virtual Function and Abstract Class
A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration. See the following example.
CPP
// An abstract class
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
2. What is abstract class?
A class which contains atleast one pure virtual function, is known as abstract class. see the following example
CPP
// An abstract class
class Test {
// Data members of class
public:
// Pure Virtual Function
virtual void show() = 0;
/* Other members */
};
In above example, Test is an abstract class because it has a pure virtual function.
Some interesting facts about abstract class
1) We can’t create an object of abstract class.
CPP
// pure virtual functions make a class abstract
#include <iostream>
using namespace std;
class Test {
int x;
public:
virtual void show() = 0;
int getX() { return x; }
};
int main(void)
{
Test t;
return 0;
}
Output :
Compiler Error: cannot declare variable 't' to be of abstract
type 'Test' because the following virtual functions are pure
within 'Test': note: virtual void Test::show()
2)These questions are designed to test and expand your knowledge of C++’s object-oriented programming capabilities, from basic definitions to complex scenario-based queriesWe can have pointers and references of abstract class type. For example the following program works fine.
CPP
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() = 0;
};
class Derived : public Base {
public:
void show() { cout << "In Derived \n"; }
};
int main(void)
{
Base* bp = new Derived();
bp->show();
return 0;
}
Output:
In Derived
3) If we do not override the pure virtual function in derived class, then derived class also becomes abstract class. The following example demonstrates the same.
CPP
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() = 0;
};
class Derived : public Base {
};
int main(void)
{
Derived d;
return 0;
}
output:
Compiler Error: cannot declare variable 'd' to be of abstract type
'Derived' because the following virtual functions are pure within
'Derived': virtual void Base::show()
3. What is the output of this program?
CPP
#include <iostream>
using namespace std;
class Test {
protected:
int width, height;
public:
void set_values(int a, int b)
{
width = a;
height = b;
}
virtual int area(void) = 0;
};
class r : public Test {
public:
int area(void)
{
return (width * height);
}
};
class t : public Test {
public:
int area(void)
{
return (width * height / 2);
}
};
int main()
{
r rect;
t trgl;
Test* ppoly1 = ▭
Test* ppoly2 = &trgl;
ppoly1->set_values(4, 5);
ppoly2->set_values(4, 5);
cout << ppoly1->area();
cout << ppoly2->area();
return 0;
}
output:
2010
Explanation:
In this program, we are calculating the area of rectangle and triangle by using abstract class.
4. What is the output of this program?
CPP
#include <iostream>
using namespace std;
class Base {
public:
virtual void print() const = 0;
};
class DerivedOne : virtual public Base {
public:
void print() const
{
cout << "1";
}
};
class DerivedTwo : virtual public Base {
public:
void print() const
{
cout << "2";
}
};
class Multiple : public DerivedOne, DerivedTwo {
public:
void print() const
{
DerivedTwo::print();
}
};
int main()
{
Multiple both;
DerivedOne one;
DerivedTwo two;
Base* array[3];
array[0] = &both;
array[1] = &one;
array[2] = &two;
for (int i = 0; i < 3; i++)
array[i]->print();
return 0;
}
output
212
Explanation:
In this program, We are executing these based on the condition given in array. So it is printing as 212.
5. What is the output of this program?
CPP
#include <iostream>
using namespace std;
class sample {
public:
virtual void example() = 0;
};
class Ex1 : public sample {
public:
void example()
{
cout << "GeeksForGeeks";
}
};
class Ex2 : public sample {
public:
void example()
{
cout << " is awesome";
}
};
int main()
{
sample* arra[2];
Ex1 e1;
Ex2 e2;
arra[0] = &e1;
arra[1] = &e2;
arra[0]->example();
arra[1]->example();
}
Output:
GeeksForGeeks is awesome
Explanation:
In this program, We are combining the two statements from two classes and printing it by using abstract class.
Similar Reads
C++ interview questions on virtual function and abstract class
Diving into the depths of C++ programming requires a solid grasp of some of its most important topics, such as virtual functions and abstract classes. These concepts are not only fundamental to mastering the language but are also frequent subjects in technical interviews. In this interview preparati
5 min read
Pure Virtual Functions and Abstract Classes in C++
Sometimes implementation of all functions cannot be provided in a base class because we don't know the implementation. Such a class is called an abstract class.For example, let Shape be a base class. We cannot provide the implementation of function draw() in Shape, but we know every derived class mu
6 min read
C++ Interview questions based on constructors/ Destructors.
1. What is destructor? Ans. Destructor is a member function which is called when an object is deleted/destroyed or goes out of scope. class String { private: char* s; int size; public: String(char*); // constructor ~String(); // destructor }; 2. What is the purpose of using a destructor in C++? Ans.
3 min read
Top C++ STL Interview Questions and Answers
The Standard Template Library (STL) is a set of C++ template classes that are used to implement widely popular algorithms and data structures such as vectors, lists, stacks, and queues. It is part of the C++ Language ISO standard. STL is a popular topic among interviewers, so it is useful for both f
15+ min read
Can Virtual Functions be Inlined in C++?
Virtual functions are member functions that are declared in the base class using the keyword virtual and can be overridden by the derived class. They are used to achieve Runtime polymorphism or say late binding or dynamic binding. Inline functions are used to replace the function calling location wi
2 min read
Can Static Functions Be Virtual in C++?
In C++, a static member function of a class cannot be virtual. Virtual functions are invoked when you have a pointer or reference to an instance of a class. Static functions aren't tied to the instance of a class but they are tied to the class. C++ doesn't have pointers-to-class, so there is no scen
1 min read
C# Interview Questions and Answers
C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications. He
15+ min read
C++ Interview Questions and Answers (2025)
C++ - the must-known and all-time favourite programming language of coders. It is still relevant as it was in the mid-80s. As a general-purpose and object-oriented programming language is extensively employed mostly every time during coding. As a result, some job roles demand individuals be fluent i
15+ min read
Commonly Asked C++ Interview Questions | Set 1
Refer to the article C++ Interview Questions and Answers for the latest data. 1. What are the differences between C and C++? C++ is a kind of superset of C, most C programs except for a few exceptions (See this and this) work in C++ as well. C is a procedural programming language, but C++ supports b
5 min read
Commonly Asked C++ Interview Questions | Set 2
Q. Major Differences between Java and C++ There are lot of differences, some of the major differences are: Java has automatic garbage collection whereas C++ has destructors, which are automatically invoked when the object is destroyed.Java does not support pointers, templates, unions, operator overl
8 min read