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)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now