Pergunta

So I have 2 classes namely book and mainscreen, where book is publically inherited from mainscreen.

Now I want to use public member functions of class book via member functions of mainscreen.

Here is my code:

class book;

class mainscreen:virtual public book
{
  protected:
    int logged;

  public:
    void printmenu();
    void printhead();
    void auth();
    void logout();
    mainscreen();
};

class book:public mainscreen
{
  private: 
    int bookno, 
        days,
        late_days;
    long double price;

    float fine_calc(int a,float b)  // a -> late days , b -> fine per day
    { 
    return a*b;
    }

  public: 
    book();   //const
    void input();
    void display();
    void update();
};

The calling part :-

     void mainscreen::printmenu(){
     int i;
     printhead();
     cout<<"\n\n\t  Choose a Number to perform the corresponding task \n";
     cout<<"\n1.Enter a new Book ";
     cout<<"\n2.Issue a Book ";
     cout<<"\n3.Return a book" ;
     cout<<"\n4.Update Information for a book ";
     cout<<"\n5.Information About books ";
     cout<<"\n6.Logout ";

     cout<<"\nEnter your choice: ";
     cin>>i;

     menuhandler(i);

     }

     void mainscreen::menuhandler(int choice){  //the no of choice selected

     switch(choice)
     {
     case 1:        printhead();
        input();

     case 2:        printhead();
        issuebook();         

     case 3:        printhead();
        returnbook();              

     case 4:        printhead();
        update();

     case 5:        printhead();
        display();

     case 6:        logout();                     

     default:
        cout<<"Invalid Choice! Press Return to Try again. ";
        getch();
        printmenu();                        // Reset to menu



     }


     }

When I try to use public member functions of class book in a member function of mainscreen, I get the following error: call to undefined function.

Foi útil?

Solução

You have:

  • identified two entities (classes): Book and MainScreen
  • and then you identified common behavior and attributes of these classes

Yet you came with very wrong conclusion, that these classes should inherit from each other (i.e. provide some behavior / attributes to each other). What you should do instead is create a third class that these classes will inherit (whatever they are meant to have in common) from.

A possible interface-based approach could be for example:

class Displayable {
public:
    virtual void display();
};

class Book : public Displayable {
    ...
};

class MainScreen : public Displayable {
    ...
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top