Inheritance problems

This is a discussion on Inheritance problems within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble with a homework assignment. This inheritance business is driving me crazy. In short, I'm trying to access ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    2

    Question Inheritance problems

    I'm having trouble with a homework assignment. This inheritance business is driving me crazy. In short, I'm trying to access data and functions from a parent class, through a subclass. The data I'm trying to arrange is best kept together, but I keep getting confused as to how to accomplish this. I made a couple of notes in red below to try clarify my problem a bit.

    Thanks,
    wheels

    Extract from my code:

    Code:
    class People
    {
     public:
      void display(void);
      void read_data(void);
    
     protected:
      StrType name;
      float pay_rate;
      float gross_pay;
      float inc_tax;
      float ss_tax;
      float net_pay;
      int hours;
    };
    
    
    class Police : public People
    {
      People cops; This is where I try to link the struct to class "People" 
      Police *left;
      Police *right;
    };
    
    
    Police* infile(Police*);
    Police* add(Police*, Police*);
    void display(Police*);
    void print_menu(void);
    
    void main()
    {
    
    
    //infile will read in data from a file specified by the user after checking
    //to ensure the file is valid.  infile will also perform the computations
    //necessary to populate the node then call the "add" function to insert the
    //newly populated node to the appropriate place in the tree.
    Police* infile(Police* root)
    {
     StrType filename;
     Police* buffer;
    
     cout << "Enter a filename: ";
     cin >> filename;
     cin.ignore(80,'\n');
     cout << endl << endl;
     fin.open(filename.c_str());
    
     //If file doesn't open, let user know
     if(fin.fail())
      {
       cout << "Couldn't open file " << filename << endl;
       cout << "Press any key to continue.";
       cin.ignore(80,'\n');
      } //end if
     else
      //Create new node to attach to tree
      {
       buffer = new Police;
       while(fin)
       {
        void Police.read_data();  I get an error here saying "Improper use of typedef Police"   
        buffer->left = NULL;  I get an error here and other places saying "'Police::left' is unaccessible (or something similar"  
        buffer->right = NULL;
        //Call "add" function to attach node to correct spot in the tree
        root = add(root,buffer);
        //Create new node and read in the next set of data
        buffer = new Police;
       }//end while
       //Delete instance of buffer before "infile" returns
       delete buffer;
      } //end else
     return root;
    } //end infile
    
    //"add" is a recursive function that will search through the tree for the
    //appropiate placement of new nodes being added to the tree.
    Police* add(Police* root, Police* buffer)
    {
     if(root)
     {
      if(buffer->name <= root->name)
       root->left = add(root->left,buffer);
      else
       root->right = add(root->right,buffer);
     }
     else
      root = buffer;
     return root;
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You have People both as a base and a member of Police. You should seriously think about this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    >>buffer->left = NULL;
    >>I get an error here and other places saying "'Police::left' is unaccessible (or something similar"
    The default scope for a class is private. If you want to be able to access left, right and cops from the outside I'd recommend you change your Police class to:
    Code:
    class Police : public People
    {
    public:
      People cops;
      Police *left;
      Police *right;
    };
    However, I'd reccomend having a struct instead of an inherited class.
    Code:
    struct Police
    {
            People cops;
            Police *left, *right;
    };
    and instead of using Police.read_data(), you use Police.cops.read_data(), but that's up to you.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    2

    Thanks

    Thanks for the advice. I'll give it a try.

    --Wheels165

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 12:26 AM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 12:45 PM
  3. Class inheritance problems
    By Carolina in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2006, 04:30 AM
  4. inheritance problems
    By generic83 in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 05:04 PM
  5. Inheritance problems
    By alkis_y3k in forum C++ Programming
    Replies: 5
    Last Post: 01-03-2003, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21