C++ interview questions on virtual function and abstract class

1. What is a pure virtual function?

Ans. 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// An abstract class
class Test {
    // Data members of class
public:
    // Pure Virtual Function
    virtual void show() = 0;
  
    /* Other members */
};

chevron_right


2. What is abstract class?
Ans. A class which contains atleast one pure virtual function, is known as abstract class. see the following example



filter_none

edit
close

play_arrow

link
brightness_4
code

// An abstract class
class Test {
    // Data members of class
public:
    // Pure Virtual Function
    virtual void show() = 0;
  
    /* Other members */
};

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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.We can have pointers and references of abstract class type.
For example the following program works fine.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
class Base {
public:
    virtual void show() = 0;
};
  
class Derived : public Base {
};
  
int main(void)
{
    Derived d;
    return 0;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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;
}

chevron_right


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?

filter_none

edit
close

play_arrow

link
brightness_4
code

#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();
}

chevron_right


Output:

 GeeksForGeeks is awesome

Explanation: In this program, We are combining the two statements from two classes and printing it by using abstract class.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.