Virtual Functions


12
Question 1
Which of the following is true about virtual functions in C++.
A
Virtual functions are functions that can be overridden in derived class with the same signature.
B
Virtual functions enable run-time polymorphism in a inheritance hierarchy.
C
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.
D
All of the above
Virtual Functions    
Discuss it


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;
}
A
In Base 
In Base 
B
In Base 
In Derived
C
In Derived
In Derived
D
In Derived
In Base 
Virtual Functions    
Discuss it


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;
}
A
In Base 
In Base 
B
In Base 
In Derived
C
In Derived
In Derived
D
In Derived
In Base 
Virtual Functions    
Discuss it


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.
A
Both 1 and 2
B
Only 1
C
Only 2
D
Neither 1 nor 2
Virtual Functions    
Discuss it


Question 5
#include<iostream>
using namespace std;

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

int main(void)
{
    Base b;
    Base *bp;
    return 0;
}
A
There are compiler errors in lines "Base b;" and "Base bp;"
B
There is compiler error in line "Base b;"
C
There is compiler error in line "Base bp;"
D
No compiler Error
Virtual Functions    
Discuss it


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;
}
A
Compiler Error: there cannot be an empty derived class
B
Compiler Error: Derived is abstract
C
No compiler Error
Virtual Functions    
Discuss it


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;
}
A
Compiler Error in line "Base &br = d;"
B
Empty Output
C
In Derived
Virtual Functions    
Discuss it


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;
}
A
Yes
B
No
Virtual Functions    
Discuss it


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;
}
A
Yes
B
No
Virtual Functions    
Discuss it


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;
}
A
Constructor: Base
Constructor: Derived
Destructor : Derived
Destructor : Base
B
Constructor: Base
Constructor: Derived
Destructor : Base
C
Constructor: Base
Constructor: Derived
Destructor : Derived
D
Constructor: Derived
Destructor : Derived
Virtual Functions    
Discuss it


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

Company Wise Coding Practice    Topic Wise Coding Practice