Multiple choice

What is the output of C++ program given below?

#include<iostream> using namespace std; class Parent { public: void func() { cout<<"function of Parent"<<endl; } }; class Child:public Parent { public: void func() { cout<<"function of Child"<<endl; } }; int main() { Child ch; ch.func(); }

  1. function of Parent

  2. function of Child

  3. function of Child function of Parent

  4. function of Child function of Parent

  5. Error in the program

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Given program involves method overriding. Method Overriding: Using this feature, sub-class or child class can provide specific implementation of the method that is already provided by the parent class. In this program, func() is the method overriden by the child class as parent class already contains same method with same siganture fun().

Hence child class func() method overrides parent class func() method which prints message inside func() of child class only but not parent class. This option is true.