Q:

Do all virtual functions need to be implemented in derived classes in c++?

0

Do all virtual functions need to be implemented in derived classes in c++?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer:

The derived classes do not have to implement all virtual functions themselves. See the below example code,

#include<iostream>
using namespace std;
//Base class
class base
{
public:
    virtual void print()
    {
        cout << "print base class" << endl;
    }
    virtual void display()
    {
        cout << "print base class" << endl;
    }
};
//Child class
class derived: public base
{
public:
    void print()
    {
        cout << "print derived class" << endl;
    }
};
int main()
{
    //derive class object
    derived d;
    //Base class pointer
    base *b = &d;
    // virtual function, binded at runtime
    b->print();
    return 0;
}

Output:

print derived class

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Interview Questions For Experienced

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Do all pure virtual functions need to be implement... >>
<< Why can templates only be implemented in the heade...