C++ interview questions on virtual function and abstract class - GeeksforGeeks

01 Sep, 2018
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.
// An abstract classclass Test {    // Data members of classpublic:    // Pure Virtual Function    virtual void show() = 0;      /* Other members */};
2. What is abstract class?Ans. A class which contains atleast one pure virtual function, is known as abstract class. see the following example
// An abstract classclass Test {    // Data members of classpublic:    // 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 class1) We can’t create an object of abstract class.
// 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.We can have pointers and references of abstract class type.For example the following program works fine.
#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.
#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?
#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 andtriangle by using abstract class.
4. What is the output of this program?
#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?
#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.
cpp-inheritance
cpp-virtual
C++
CS - Placements
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Friend class and function in C++
Sorting a vector in C++
std::string class in C++
Applications of linked list data structure
Most asked Computer Science Subjects Interview Questions in Amazon, Microsoft, Flipkart
TCS Interview Questions
Count of substrings of length K with exactly K distinct characters
Hotel Management System