Q:

Write a C++ program that describes function Overriding?

0

Write a C++ program that describes function Overriding?

All Answers

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

Answer:

Let see a program, in which base and child class have the same function Display that follows function overriding rule.

// Function Overriding
#include<iostream>
using namespace std;
//Base class
class BaseClass
{
public:
    virtual void Display()
    {
        cout << "In Base class\n";
    }
};
//child class
class DerivedClass : public BaseClass
{
public:
    // Overriding method - new working of
    // base class's display method
    void Display()
    {
        cout << "In Child class\n";
    }
};
int main()
{
    DerivedClass dr;
    BaseClass &bs = dr;
    bs.Display();
}

Output:

In Child class

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

total answers (1)

C++ Interview Questions and Answers(2022)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
What is the difference between function overloadin... >>
<< What is Overriding in c++?...