0

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.

  • 2
    the fact that you can do this does not mean it is a good idea! There is a good reason why c# and java dont allow this. Please consider redesign – lordkain Oct 10 '13 at 11:30
  • 4
    Inheritance is supposed to model *is-a* relationships. But a book is not a main screen, and neither is a main screen a book. Inheritance is the wrong tool for the problem you are trying to solve. – us2012 Oct 10 '13 at 11:31
  • That's simply not possible! What do you want to achieve? – πάντα ῥεῖ Oct 10 '13 at 11:31
  • 1
    Inheritance models an *is-a* relationship. If a `book` is a `mainscreen` and the other way around, then they are one class. If that does not answer your question, then you need to explain better what problem you are trying to solve. – Björn Pollex Oct 10 '13 at 11:31
  • What error do you get on which line of your code (which I presume you haven't shown us since this is the declaration not the attempt at afunction call) – doctorlove Oct 10 '13 at 11:32
  • circular inheritance? Definitively is not a good idea – Manu343726 Oct 10 '13 at 11:32
  • In what world `A` _is-a_ `B` and `B` _is-a_ `A`? – Marius Bancila Oct 10 '13 at 11:34
  • I am using the above code for my school C++ project , not for production level .. How to do this ? .. main screen is like universal class for my program .. it controls what displays on screen .. so i should be to use function of other classes to know what to display – mohit gupta Oct 10 '13 at 11:36
  • 2
    Which compiler allows this `class book; class mainscreen:virtual public book`?? It is ill-formed as per [class.derived]/2 "The type denoted by a *base-type-specifier* shall be a class type that is not an incompletely defined class" (I'm inclined to say *throw that compiler away*) – dyp Oct 10 '13 at 11:36
  • Right: my father is my son. – Pete Becker Oct 10 '13 at 11:37
  • 3
    Just because it's homework doesn't mean you should do it wrong. – doctorlove Oct 10 '13 at 11:41
  • This isn't even your real code, since `mainscreen::menuhandler()` does not appear in either class definition. Using recursive function calls as an alternative to `goto` is a really bad idea. `fine_calc()` doesn't use any member data, and should be a standalone function. Adding a `//const` comment does not make a member function `const`, and this would be rather odd behavior for a constructor anyway. The attempted inheritance relationship is bizarre, as others have pointed out. – Crowman Oct 10 '13 at 11:57

1 Answers1

4

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 {
    ...
};
LihO
  • 41,190
  • 11
  • 99
  • 167