Do all virtual functions need to be implemented in derived classes in c++?
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Answer:
The derived classes do not have to implement all virtual functions themselves. See the below example code,
Output:
print derived class
need an explanation for this answer? contact us directly to get an explanation for this answer