C++ Program to illustrate the use of Pure virtual function

By | 03.01.2017

Pure virtual function


Write a C++ Program to illustrate the use of  Pure virtual function. Here’s a Simple C++ Program to illustrate the use of  Pure virtual function in C++ Programming Language.


Abstract base class & pure virtual function


  • The class that is not used to create objects is called an abstract class.
  • The only purpose of an abstract class is to act as a base class.
  • An abstract class is required when the base class is unable to create a meaningful implementation for a member function.
  • For example, in the following figure, the class Shape represents the abstract concept for which objects cannot exist.
  • A Shape makes sense only as the base of some class derived from it.
  • This can be seen from the fact that it is not possible to provide sensible definitions for its virtual functions.
  • A better alternative is to declare the virtual function of the class Shape to be pure virtual functions.
  • A virtual function is made pure by the initializer, = 0. Thus, the virtual functions in the abstract base class can be made pure as:
  •   virtual void Enter_data( ) = 0 ;

  •   virtual void Area( ) = 0;

  • A class with one or more pure virtual function is an abstract, and no object of that abstract class can be created. An abstract class can be used only as an interface and as a base for other classes.

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


SOURCE CODE : :


/*  C++ Program to illustrate the use of  Pure virtual function  */

#include<iostream>
using namespace std;
class Shape   // Abstract Base Class
{
                public:
                        virtual void Enter_data( ) = 0;  // pure virtual function
                        virtual void Area( ) = 0 ;            // pure virtual function
};
class Rectangle : public Shape // First Derived class
{
                private:
                        float length ;
                        float breadth ;
                
                public:
                        void Enter_data(void)
                        {
                                cout << "\n Enter the data for the Rectangle........" ;
                                cout << "\n\t Enter the length of the rectangle: " ;
                                cin >> length ;
                                cout << "\t Enter the breadth of the rectangle: " ;
                                cin >> breadth ;
                        }
                        void Area(void)
                        {
                                cout << "\n\t The area of the rectangle = " << (length * breadth) ;
                        }
};

class Circle : public Shape // Second Derived class
{
                private:
                        float radius ;
                public:
                        void Enter_data(void)
                        {
                                cout << "\n\n Enter the data for the Circle..........." ;
                                cout << "\n\t Enter the radius of the circle: " ;
                                cin >> radius ;
                        }
                        void Area(void)
                        {
                                cout << "\n\t The area of the circle = " << (3.14 * radius * radius) ;
                        }
};
int main()
{
                Shape* shp ; // pointer to the object of the base class Shape
                Rectangle rec ; // object of class Rectangle
                shp = &rec ;
                shp->Enter_data( ) ;
                shp->Area( );
                Circle cir ;     // object of class Circle
                shp = &cir ;
                shp->Enter_data( );
                shp->Area( );
return 0;
}

OUTPUT : :


/*  C++ Program to illustrate the use of Pure virtual function  */

Enter the data for the Rectangle........
         Enter the length of the rectangle: 5
         Enter the breadth of the rectangle: 7

         The area of the rectangle = 35

 Enter the data for the Circle...........
         Enter the radius of the circle: 3

         The area of the circle = 28.26

Above is the source code and output for C++ Program to illustrate the use of Pure virtual function 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….

2.7 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments