Monday, June 19, 2023

VIRTUAL FUNCTIONS and POLYMORPHISM

VIRTUAL FUNCTION AND POLYMORPHISM 



EXAMPLE

#include<iostream.h>

#include<conio.h>

class base{

                        public:

virtual void show()

                 {

                 cout<<"base class";

                }

         };

class derived : public base

              {

              public:

              void show()

             {

            cout<<"Derived";

            }

            };

void main()

{

base *p;

p=new base();

p->show();

p=new derived();

p->show();

getch();

}

output

base class derived

 

Pure virtual function

syntax [=0]

class a{

             public:

            virtual void fun()=0;

           };

ABSTRACT CLASS

a class which consist atleast one pure virtual function is called an abstract base class. an abstract class is used as a base class for deriving purpose only .it could not be used for creating object.

 

 

 

0 comments:

Post a Comment