C++ program to illustrate virtual function implementation - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Friday, September 1, 2017

C++ program to illustrate virtual function implementation

Write a C++ program to illustrate virtual function implementation.


Sample Input and Output:

bptr points to base
Show base
Display base
bptr points to derived
Show derived
Display derived

Program:

#include<iostream.h>
class base
{
public:
void display()
{cout<<"\nDisplay base";}
virtual void show(){cout<<"\nShow base.";}
};
class derived:public base
{
public:
void display(){cout<<"\nDisplay derived";}
void show(){cout<<"\nShow Derived.";}
};
int main()
{
base b;
derived d;
base *bptr;
bptr=&b;
cout<<"\nbptr points to base\n";
bptr->display();
bptr->show();
cout<<"\n\nbptr points to derived.";
bptr=&d;
bptr->display();
bptr->show();
return 0;

}

No comments:

Post a Comment