Method Overriding in CPP

Method Overriding- When a function with the same name in the base and derived class is there then the base class function will be overridden(means ignored) and derived class function will be called.

#include<iostream.h>
#include<conio.h>

class demo1          // base class   // parent
{
public :

void add()
{
cout << "hi from demo1 class" << endl;
}
};

class demo2 : public demo1           // derived              // child
{
public :

void add()
{
cout << "hi from demo2 class" << endl;
}
};
main()
{
clrscr();
demo2 d2;
d2.add();
getch();
}

Output:
hi from demo2 class

Leave a Reply