Thread: basics

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    basics

    If I have private data members in base class, what about my derived class? How do I write methods that use these private base class members... I know that making them public would solve the problem, but then what would be the point of inheritance, right? So, it is not the solution.
    Like (sort of pseudo code)
    Code:
    class point {
    private:
       int x, y;
    public:
       int coord() { //whatever }
    };
    class point_shift : public point {
    public:
       int coord_shifted() { //how do I use x and y here??? }
    };

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    create protected member functions in the base class to access the private data members or make the data members protected in base class.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Something as simple as

    Code:
    x GetX()
    {
       return x;
    }
    
    // and then for y
    
    y GetY()
    {
       return y;
    }
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    BASE CLASS                                DERIVED CLASS
    
    private in base class        -->    not accessible in derived class
    
    protected in base class,
    which means the member behaves
    like a private member in base -->  accessible by functions in derived class, i.e. they
                                      have the same access as private members of derived
    Last edited by 7stud; 03-10-2006 at 12:18 AM.

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by Shamino
    Something as simple as

    Code:
    int GetX()
    {
       return x;
    }
    
    // and then for y
    
    int GetY()
    {
       return y;
    }
    Yea something like that

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    OK, thanks, I need some more reading on that.

  7. #7
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    lol my bad, I was thinking in object mode
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    OK, thanks, I need some more reading on that.
    Until then, substitute 'protected' for 'private', and you are good to go.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Just remember that substituting protected for private is usually not the best design.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Well, it is just an assignment and I am supposed to know how to access certain members and whether I can at all. Just an exercise.
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learned the basics, SO ?
    By TuxPayne in forum C Programming
    Replies: 4
    Last Post: 09-11-2005, 12:32 PM
  2. "Modern C++ and Basics of OO Programming" (e-learning course)
    By Erhard Henkes in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2004, 03:01 PM
  3. C++ Basics part two
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-22-2003, 11:34 AM
  4. Desire & Fear: Two Basics of Human..
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 11-16-2002, 11:11 PM
  5. The Basics
    By Granger9 in forum Windows Programming
    Replies: 5
    Last Post: 09-13-2002, 05:12 PM