Search code examples
c++oopvirtual-functionsrun-time-polymorphism

Compile time errors while implement Virtual Functions and Run-time polymorphism in C++


I created the following program to implement Run-time Polymorphism in C++

/* Consider a book shop which sells both books and video-tapes. Create a class know as media that storea the title
and price of a publication.*/

#include <iostream>
#include <cstring>
using namespace std;

class media // defining base class
{
protected:
    char title[50];
    float price;

public:
    media(char *s, float a)
    {
        strcpy(title, s);
        price = a;
    }
    virtual void display() {}
};

class book : public media // defining derived class 'book' which is derived from 'media'
{
    int pages;

public:
    book(char *s, float a, int p) : media(s, a) // constructor of derived class
    {
        pages = p;
    }
    void display() {}
};

class tape : public media // defining derived class 'tape' which is derived from 'media'
{
    float time;

public:
    tape(char *s, float a, float t) : media(s, a) // constructor of derived class
    {
        time = t;
    }
    void display() {}
};

void book ::display() // function to display book details
{
    cout << "\n Title: " << title;
    cout << "\n Pages: " << pages;
    cout << "\n Price: " << price;
}

void tape::display() // function to display tape details
{
    cout << "\n Title: " << title;
    cout << "\n Playtime: " << time;
    cout << "\n Price: " << price;
}

int main()
{
    char *title = new char[30]; // creating a array of characters using 'new' for storing title
    float price, time;
    int pages;

    //Book Details

    cout << "\n Enter Book Details \n";
    cout << "Title: ";
    cin >> title;
    cout << "Price: ";
    cin >> price;
    cout << "Pages: ";
    cin >> pages;

    book book1(title, price, pages);

    //Tape Details

    cout << "\n Enter Tape Details \n";
    cout << "Title: ";
    cin >> title;
    cout << "Price: ";
    cin >> price;
    cout << "Play time (mins): ";
    cin >> time;

    tape tape1(title, price, time);

    media *list[2];
    list[0] = &book1;
    list[1] = &tape1;

    cout << "\n Media Details";

    cout << "\n.....Book....";
    list[0]->display(); // display Book details
    cout << "\n.....Tape....";
    list[1]->display(); // display Tape details
    return 0;
}

It uses Constructor, 'new' memory allocator operator and virtual functions for its purpose

The question is:

Consider a book shop which sells both books and videotapes. Create a class known as media that store the title and price of a publication.

But it ends up with the following errors


virtual_function.cpp:47:6: error: redefinition of ‘void book::display()’
   47 | void book ::display() // function to display book details
      |      ^~~~
virtual_function.cpp:32:10: note: ‘virtual void book::display()’ previously defined here
   32 |     void display() {}
      |          ^~~~~~~
virtual_function.cpp:54:6: error: redefinition of ‘void tape::display()’
   54 | void tape::display() // function to display tape details
      |      ^~~~
virtual_function.cpp:44:10: note: ‘virtual void tape::display()’ previously defined here
   44 |     void display() {}
      |          ^~~~~~~

I'm using GNU-GCC Compiler in VSCode on Ubuntu


Solution

  • The problem is instead of declaring the function display inside class book and tape you were defining them because you have curly braces {}. And then you again redefined them outside the class.

    To solve this, just replace:

    void display() {}
    

    with

    void display() ;
    

    in class book and tape. So your class book and tape would now look like:

    class book : public media 
    {
        int pages;
    
    public:
        book(char *s, float a, int p) : media(s, a) 
        {
            pages = p;
        }
        void display(); //a declaration. I removed the curly braces {} you had here
    };
    
    class tape : public media 
    {
        float time;
    
    public:
        tape(char *s, float a, float t) : media(s, a) 
        {
            time = t;
        }
        void display() ; //a declaration.I removed the curly braces {} you had here 
    };
    
    

    Note in the above code i have removed the braces {} and replaced them with a semicolon for the function display().

    The program now works(that is it no longer have the errors you mentioned) as can be seen here.