Looking for NEET / JEE Coaching? Enquire Now
You must Sign In to post a response.
  • Category: Programming

    Program to implement run time polymorphism using C++


    Facing doubts while preparing for Diploma Computer exams? Searching for a solution to a program to implement run time polymorphism using C++? Check out this page where our ISC experts shall provide answers for your query.

    Following is the question asked in MSBTE(Maharashtra State Board of Technical Education, Mumbai) Diploma in Computer Engineering question paper for Summer 2017 examination.
    This question is asked for 4 marks and hence requires some explanation along with program code. Please let me know the solution.
    Q) Create class shape. Derive two classes Triangle and Rectangle. Accept dimensions of Triangle and Rectangle with appropriate functions. Make area() function virtual which is common to all classes. With area function calculate area of triangle and rectangle. Display the result.[4 Marks]
  • Answers

    1 Answers found.
  • Inheritance is essential to perform polymorphism. It is a unique facility provided by OOP. For smooth operation of polymorphism it is necessary to have pointer variable of base class which is capable to hold the address of derived class object. For polymorphism it is necessary that the prototype of base class function and derived class version must be the same. Only difference is that base class function is having virtual keyword at its beginning.
    The keyword virtual when used with base class function, it take care to decide which function to be executed that is if base class object is under consideration the it will be get executed or if the derived class object is under consideration then derived class version of function get executed.
    Below is the program which implements the concept of polymorphism.
    Please include iostream.h and conio.h header files before declaration of class shape.

    //PROGRAM TO IMPLEMENT RUN TIME POLYMORPHISM.
    #include
    #include
    #include
    class shape
    {
    public:
    virtual void area(void){};
    };
    class triangle:public shape
    {
    private:
    float base,height,ar;
    public:
    void get(void)
    {
    cout<<"Enter base and height of triangle in\n";
    cin>>base>>height;
    }
    void area(void)
    {
    ar=0.5*base*height;
    cout<<"\nArea of triangle is "< }
    };
    class rectangle:public shape
    {
    private:
    float length,breadth,ar;
    public:
    void get(void)
    {
    cout<<"Enter length and breadth of rectangle\n";
    cin>>length>>breadth;
    }
    void area(void)
    {
    ar=length*breadth;
    cout<<"\nArea of rectangle is "< }
    };
    void main(void)
    {
    class shape * ptr;
    class triangle t1;
    class rectangle r1;
    clrscr();
    cout<<"\n1.Area of Triangle\n2.Area of Rectangle\n3.Exit";
    cout<<"\nEnter your choice\n";
    int ch;
    cin>>ch;
    switch(ch)
    {
    case 1:
    ptr=&t1;
    t1.get();
    ptr->area();
    break;
    case 2:
    ptr=&r1;
    r1.get();
    ptr->area();
    break;
    case 3:
    exit(0);
    break;
    default:
    cout<<"\nInvalid choice\n";
    }
    getch();
    }


  • Sign In to post your comments