Q:

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

belongs to collection: C++ Polymorphism Solved Programs

0

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++ ?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C++ Program to Maintain Employee Database using Vi... >>
<< Write a C++ Program to illustrate Abstract Base Cl...