Write a program of virtual function


 

/* Write a program of  virtual function*/

#include<iostream.h>

#include<conio.h>

class base

{

public:

virtual void dis()

{

cout<<“base class”;

}

};

class a:public base

{

public:

void dis()

{

cout<<“derived class a function”<<endl;

}

};

class b:public base

{

public:

void dis()

{

cout<<“derived class b function”;

}

};

void main()

{

clrscr();

base *ptr;

class a a1;

class b b1;

ptr=&a1;

ptr->dis();

ptr=&b1;

ptr->dis();

getch();

}

/* output:

derived class a function

derived class b function*/

Related Posts

Comments are closed.