Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

QUICK HELP-c++ - inheritance

Options
  • 13-12-2005 11:56am
    #1
    Closed Accounts Posts: 1,061 ✭✭✭


    I need a fast respose if possible please.
    Problem:
    Inheritance using virtual functions.
    It's part of a project.
    Code I have so far:

    #include <iostream.h>
    #include <string.h>
    
    const int MAX = 20;
    
    class details
          {
          protected:              //private: doesn't seem to work here.. more errors
                  char name[MAX];
                  int accNo;
          public:
    
                 virtual void setDets() {
                         cout << "Enter name: ";
                         gets(name);
                         cout << "Enter account  No. :";
                         cin >> accNo;
                         cin.ignore();
                         }
                 virtual void dispDets()
                 {
                         cout << "\tName: " << name;
                         cout << "\n\tAccount No: "<< accNo;
                         }
                 
          };
          
          class accounts : details {                //have also tried public details, makes no difference
                private:
                       float currentBal;
                       float savingsBal;
                       float specialBal;
                public:
                       void setDets()
                       {
                           currentBal = rand()%45 + rand()%22 + rand()%340;   //balance of 3 accounts set to random numbers
                           savingsBal = rand()%345 + rand()%12 + rand()%210;
                           specialBal = rand()%41 + rand()%82 + rand()%404;
                           }
                    
                      /* void setCuBal() { currentBal = rand()%45 + rand()%22 + rand()%340; }
                       void setSaBal() { savingsBal = rand()%345 + rand()%12 + rand()%210; }
                       void setSpBal() { specialBal = rand()%41 + rand()%82 + rand()%404; }
                       */
                       
                       void dispDets()
                       {
                            cout << "\t\tACCOUNT DETAILS: \n";
                            cout << "Name: " << name;
                            cout << "Account No.: " << accNo;
                            cout << "\tCurrent Balance: " << currentBal << endl;
                            cout << "\tSavings Balance: " << savingsBal << endl;
                            cout << "\tSpec. Svngs Bal: " << specialBal << endl;
                            cin.get();
                            }
                };
                       
          
          int main()
          {
              details person;
              details *p;
              
              accounts first_ob;
              
              p = &person;
              
              p->setDets();
              p->dispDets();
              
              p = &first_ob;
              p->setDets();
              p->dispDets();
              cin.get();
              cin.get();
              
              return 0;
              }
    

    but on compiling i get:
    `details' is an inaccessible base of `accounts'


    any ideas?
    thanks.


Comments

  • Registered Users Posts: 173 ✭✭happydude13


    I'm suprised that using public inheritance causes
    the same error.

    It would help if said/knew what line caused the problem.

    You should do a search for how inheritance works in c++

    The approach I would recommend is to remove lines until it works
    then re jig the problematic code, eg, doing the same thing
    over a few lines or doing the same thing in a different way
    and you'll quickly improve your grasp of the error.

    eoin


Advertisement