0

For classes in C++: I don't understand when I should create private member variables and functions, I know that they can't be called from int main whereas public members can but I have no clue when I should define something as private and what the benefit is.

Can someone explain with a sample code what the advantage of using private members is and why it is useful? (does it prevent users from changing code that you made? how is this useful)?

new Q Open Wid
  • 2,225
  • 2
  • 18
  • 34
lbragile
  • 7,549
  • 3
  • 27
  • 64
  • possible duplicate of [What are access specifiers? Should I inherit with private, protected or public?](http://stackoverflow.com/questions/5447498/what-are-access-specifiers-should-i-inherit-with-private-protected-or-public) – набиячлэвэли Sep 14 '15 at 23:24
  • possible duplicate of [Should I use public or private variables?](http://stackoverflow.com/questions/14399929/should-i-use-public-or-private-variables) – NathanOliver Sep 14 '15 at 23:25
  • 1
    can you access you're car's engine as a driver? think about it this way – David Haim Sep 14 '15 at 23:43

3 Answers3

2

Private is really just for ease of use. For example, let's say I'm making a game and have a character class:

class character 
{
private:
    int level;
public:
    character()
    void setLevel(int newLevel);
};

This allows the level to be changed, but if I want there to be a maximum level I can just define it in the setLevel function. Now if someone wants to use my code, they can't make a character with level 3,000,000. They can only use the max level.

I guess my point is is helps keep variables acting as they should normally act within the class. The class gets to define how they change and such instead of anyone being able to change them however they want.

fuzzything44
  • 701
  • 4
  • 15
  • I thought `private` was to support the concepts of *data hiding* and *encapsulation* as well as to support loose coupling between classes. I could be wrong though; I guess it's for ease of use. – Thomas Matthews Sep 14 '15 at 23:58
  • 1
    And those concepts are mostly just for ease of use. – fuzzything44 Sep 15 '15 at 00:03
1

The two schools of thought I've seen are:

  1. All data is declared private. Functions for access to the data are declared public or protected depending if you want to create a subclass or not.
  2. Same as the first except you move the data to the protected section instead of creating functions.

Example:

class A {
    public:
        A() : m_a(0) { }
        int GetA() const { return m_a; }
        int GetA() { return static_cast<const A&>(*this).GetA(); }
        //...More code here
    protected:
        //I don't want external code to be able to modify m_a, only subclasses.
        void SetA(int a) { m_a = a; }
        //...More code here
    private:
        int m_a;
}

OR:

class A {
    public:
        A() : m_a(0) { }
        int GetA() const { return m_a; }
        int GetA() { return static_cast<const A&>(*this).GetA(); }
        //...More code here
    protected:
        int m_a;
    private:
}
Casey
  • 10,297
  • 11
  • 59
  • 88
1

There's much better resources than an answer typed up in a few minutes on stack overflow, but in short:

A public member is defining how you want the outside world to interact with your class. Generally, it's considered best practice to not have any variables public, and have the outside world interact with them with Set/Get functions.

Both protected and private hide members from the outside world, but also specify how a subclass can access them. Protected members are inherited; private members aren't.

Stylistically, I tend to think of protected and private as subroutines to by used by the class's public interface. If I make a collection of classes that are all "shapes", I factor out the common code of the public interfaces of "square", "circle", "triangle", and put them in protected functions in the base "shape" class. I tend not to use private very often. (Mostly because I do embedded work and I'm more interested in code sharing between classes, rather than interface encapsulation, and my code isn't ever used as a library to outside sources)

Russ Schultz
  • 2,545
  • 20
  • 22