C++ Virtual Functions

Question 1
Which of the following is true about virtual functions in C++.
Cross
Virtual functions are functions that can be overridden in derived class with the same signature.
Cross
Virtual functions enable run-time polymorphism in a inheritance hierarchy.
Cross
If a function is \'virtual\' in the base class, the most-derived class\'s implementation of the function is called according to the actual type of the object referred to, regardless of the declared type of the pointer or reference. In non-virtual functions, the functions are called according to the type of reference or pointer.
Tick
All of the above


Question 1-Explanation: 
Question 2
Predict output of the following program
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() { cout<<" In Base \n"; }
};

class Derived: public Base
{
public:
    void show() { cout<<"In Derived \n"; }
};

int main(void)
{
    Base *bp = new Derived;
    bp->show();

    Base &br = *bp;
    br.show();

    return 0;
}
Cross
In Base 
In Base 
Cross
In Base 
In Derived
Tick
In Derived
In Derived
Cross
In Derived
In Base 


Question 2-Explanation: 
Since show() is virtual in base class, it is called according to the type of object being referred or pointed, rather than the type of pointer or reference.
Question 3
Output of following program
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() { cout<<" In Base \n"; }
};

class Derived: public Base
{
public:
    void show() { cout<<"In Derived \n"; }
};

int main(void)
{
    Base *bp, b;
    Derived d;
    bp = &d;
    bp->show();
    bp = &b;
    bp->show();
    return 0;
}
Cross
In Base 
In Base 
Cross
In Base 
In Derived
Cross
In Derived
In Derived
Tick
In Derived
In Base 


Question 3-Explanation: 
Initially base pointer points to a derived class object. Later it points to base class object,
Question 4
Which of the following is true about pure virtual functions? 1) Their implementation is not provided in a class where they are declared. 2) If a class has a pure virtual function, then the class becomes abstract class and an instance of this class cannot be created.
Cross
Both 1 and 2
Cross
Only 1
Tick
Only 2
Cross
Neither 1 nor 2


Question 5
#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() = 0;
};

int main(void)
{
    Base b;
    Base *bp;
    return 0;
}
Cross
There are compiler errors in lines \"Base b;\" and \"Base bp;\"
Tick
There is compiler error in line \"Base b;\"
Cross
There is compiler error in line \"Base bp;\"
Cross
No compiler Error


Question 5-Explanation: 
Since Base has a pure virtual function, it becomes an abstract class and an instance of it cannot be created. So there is an error in line \"Base b\". Note that there is no error in line \"Base *bp;\". We can have pointers or references of abstract classes.
Question 6
Predict the output of following program.
#include<iostream>
using namespace std;
class Base
{
public:
    virtual void show() = 0;
};

class Derived : public Base { };

int main(void)
{
    Derived q;
    return 0;
}
Cross
Compiler Error: there cannot be an empty derived class
Tick
Compiler Error: Derived is abstract
Cross
No compiler Error


Question 6-Explanation: 
If we don\'t override the pure virtual function in derived class, then derived class also becomes abstract class.
Question 7
#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)
{
    Derived d;
    Base &br = d;
    br.show();
    return 0;
}
Cross
Compiler Error in line \"Base &br = d;\"
Cross
Empty Output
Tick
In Derived


Question 7-Explanation: 
Question 8
Can a constructor be virtual? Will the following program compile?
#include <iostream>
using namespace std;
class Base {
public:
  virtual Base() {}   
};
int main() {
   return 0;
}
Cross
Yes
Tick
No


Question 8-Explanation: 
There is nothing like Virtual Constructor. Making constructors virtual doesn\'t make sense as constructor is responsible for creating an object and it can’t be delegated to any other object by virtual keyword means.
Question 9
Can a destructor be virtual? Will the following program compile?
#include <iostream>
using namespace std;
class Base {
public:
  virtual ~Base() {}   
};
int main() {
   return 0;
}
Tick
Yes
Cross
No


Question 9-Explanation: 
A destructor can be virtual. We may want to call appropriate destructor when a base class pointer points to a derived class object and we delete the object. If destructor is not virtual, then only the base class destructor may be called. For example, consider the following program.
// Not good code as destructor is not virtual
#include<iostream>
using namespace std;

class Base  {
public:
    Base()    { cout << \"Constructor: Base\" << endl; }
    ~Base()   { cout << \"Destructor : Base\" << endl; }
};

class Derived: public Base {
public:
    Derived()   { cout << \"Constructor: Derived\" << endl; }
    ~Derived()   { cout << \"Destructor : Derived\" << endl; }
};

int main()  {
    Base *Var = new Derived();
    delete Var;
    return 0;
}

Output on GCC:
Constructor: Base
Constructor: Derived
Destructor : Base
Question 10
#include<iostream>
using namespace std;
class Base  {
public:
    Base()    { cout<<"Constructor: Base"<<endl; }
    virtual ~Base()   { cout<<"Destructor : Base"<<endl; }
};
class Derived: public Base {
public:
    Derived()   { cout<<"Constructor: Derived"<<endl; }
    ~Derived()  { cout<<"Destructor : Derived"<<endl; }
};
int main()  {
    Base *Var = new Derived();
    delete Var;
    return 0;
}
Tick
Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base
Cross
Constructor: Base
Constructor: Derived
Destructor : Base
Cross
Constructor: Base
Constructor: Derived
Destructor : Derived
Cross
Constructor: Derived
Destructor : Derived


Question 10-Explanation: 
Since the destructor is vitrual, the derived class destructor is called which in turn calls base class destructor.
There are 14 questions to complete.


  • Last Updated : 28 Sep, 2023

Share your thoughts in the comments
Similar Reads