C++ Program to illustrate an example of Pure Virtual functions

By | 03.01.2017

Example of Pure Virtual functions


Write a C++ Program to illustrate an example of Pure Virtual functions. Here’s a Simple Program to illustrate an example of Pure Virtual functions in C++ Programming Language.


What is Polymorphism in C++ ?


The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

C++ polymorphism means that a call to a member function will cause a different function to be executed depending on the type of object that invokes the function.


Virtual Function : :


A virtual function is a function in a base class that is declared using the keyword virtual. Defining in a base class a virtual function, with another version in a derived class, signals to the compiler that we don’t want static linkage for this function.


Below is the source code for C++ Program to illustrate an example of Pure Virtual functions which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to illustrate an example of Pure Virtual functions */

#include<iostream>
 #include<graphics.h>
 #include<stdlib.h>
 using namespace std;


 /*************************************************************************///------------------------------  shape  --------------------------------///*************************************************************************/class shape
    {
       public:
        virtualvoid draw()=0;
    };

 /*************************************************************************///--------------------------  draw_rectangle  ---------------------------///*************************************************************************/class draw_rectangle:public shape
    {
       public:
        void draw()  { rectangle(225,225,180,180);  }
    };

 /*************************************************************************///---------------------------  draw_arc  --------------------------------///*************************************************************************/class draw_arc:public shape
    {
       public:
        void draw()  { arc(200,200,45,135,100);  }
    };

 /*************************************************************************///---------------------------  draw_circle  -----------------------------///*************************************************************************/class draw_circle:public shape
    {
       public:
        void draw()  { circle(200,200,60);  }
    };


 int main()
    {
       int driver=VGA;
       int mode=VGAHI;
       int error_code;

       initgraph(&driver,&mode,"..\\Bgi");

       error_code=graphresult( );

       if(error_code!=grOk)
          {
             restorecrtmode( );
             textmode(BW80);
             clrscr( );

             cout<<" \n Fatal Error  : Graphic Driver not initialized"<<endl;
             cout<<" Error Reason : "<<grapherrormsg(error_code)<<endl;
             cout<<" \n Press any key to exit...";

             getch( );
             exit(1);
          }

       shape *ptr;
       draw_rectangle r;
       draw_arc a;
       draw_circle c;

       ptr=&r;
       ptr->draw();

       ptr=&a;
       ptr->draw();

       ptr=&c;
       ptr->draw();

       getch();
       closegraph();
       return 0;
    }

Above is the source code and output for C++ Program to illustrate an example of Pure Virtual functions which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

3 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments