You must Sign In to post a response.
  • Category: Programming

    C++ program to implement run time polymorphism.


    Preparing for IGNOU BCA exams? Having difficulty in writing C++ programs for implementing run time polymorphism? Resolve your worries by scrolling trough this page where our ISC experts have provided suggestions and solutions.

    Following is a question that was asked in the question paper of BCA(December 2016)conducted by IGNOU, for subject Object Oriented Programming using C++(CS-72).

    This question is asked for 8 marks and hence requires some explanation along with the program code. Please let me know the solution.

    Q) Consider a publishing company that markets both books and audio-cassettes version of its works. Create a class Publication that stores the title and price of a publication. From this class derive two classes :book which adds a page count and tape which adds a playing time in minutes. Each of these three classes should have a getdata( ) function to get its data from the user and putdata( ) function to display its data. Also, write a main( ) function that uses an array of pointers to publication. [ 8 Marks ]
  • Answers

    1 Answers found.
  • Run time polymorphism is a great facility provided by Object Oriented Programming. It essentially requires inheritance. Also in this concept base class pointer is used to hold address of base class object as well as address of derived class object. To successfully implement the concept of polymorphism it is necessary that function prototyping in base as well as derived class must be identical with only one exception that base class function must be declared as virtual function.
    In such situation when base class pointer hold address of base class object then it invoke base class function and when base pointer hold address of derived class then it invokes the derived class function.
    The same concept is used in below program.
    Please include iostream.h, conio.h and stdio.h header files before declaration of class publication.
    //PROGRAM TO IMPLEMENT RUN TIME POLYMORPHISM
    #include
    #include
    #include
    class publication
    {
    private:
    char title[20];
    float price;
    public:
    virtual void getdata(void)
    {
    cout<<"Enter title and price\n";
    gets(title);
    cin>>price;
    }
    virtual void putdata(void)
    {
    cout<<"\nTitle is "< cout<<"\nPrice is "< }
    };
    class book:public publication
    {
    private:
    int pagecount;
    public:
    void getdata(void)
    {
    cout<<"\nEnter page count of book\n";
    cin>>pagecount;
    }
    void putdata(void)
    {
    cout<<"\nPage count is "< }
    };
    class tape:public publication
    {
    private:
    int playtime;
    public:
    void getdata(void)
    {
    cout<<"\nEnter the play time of tape\n";
    cin>>playtime;
    }
    void putdata(void)
    {
    cout<<"\nPlay time of tape is "< }
    };
    void main(void)
    {
    class publication *arr[3];
    class publication p;
    class book b;
    class tape t;
    clrscr();
    arr[0]=&p;
    cout<<"\nWe are collectiong comman details of publication\n";
    arr[0]->getdata();
    arr[1]=&b;
    cout<<"\nNow we are collecting book data\n";
    arr[1]->getdata();
    arr[2]=&t;
    cout<<"\nNow we are collecting tape data\n";
    arr[2]->getdata();
    clrscr();
    cout<<"\nBOOK DETAILS ARE AS FOLLOWS\n";
    arr[0]->putdata();
    arr[1]->putdata();
    cout<<"\n\n**********************************************************\n";
    cout<<"\nTAPE DETAILS ARE AS FOLLOWS\n";
    arr[0]->putdata();
    arr[2]->putdata();
    cout<<"\n\n**********************************************************\n";
    getch();
    }


  • Sign In to post your comments