Runtime Polymorphism In C++
This
type of polymorphism is achieved by Function Overriding.
· Function Overriding
Fig: Runtime Polymorphism In C++ |
1.
Function overriding
Function overriding on the other hand occurs
when a derived class has a definition for one of the member functions of the
base class. That base function is said to be overridden.
Program:
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void print ()
{
cout<<
"print base class" <<endl;
}
void show ()
{
cout<< "show base class"
<<endl;
}
};
class derived:public
base
{
public:
void print ()
{
cout<< "print derived class"
<<endl;
}
void show ()
{
cout<< "show derived class"
<<endl;
}
};
void main()
{
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
getch();
}
Output:
Print derived class
Show base class
No comments:
Post a Comment