What does it mean to be a private member (c++ classes)?
Asked Answered
W

4

5

I am a little confused about private data members in C++ classes. I am new to coding and still in the middle of my 'Classes' chapter so I might be ahead of myself, but I feel like I am missing a piece of information:

Let's say I have this code:

class clocktype;
{ 
  public:
          void setTime(int,int,int);
          .
          .
          .
   private:
          int hr;
          int min;
          int sec;
     };

And I create a object myclock.

clocktype myclock;
myclock::setTime(hour,minute,min)
{
  if (0<= hour && hour < 24)
  hr = hour;

  if (0<= minute && minute <60)
  min = minute;

  if ( 0<= second && second<60)
  sec = second;
 }
 myclock.setTime(5,24,54);

My textbook says I can't do this:

myclock.hr = 5;

because hr is a private data member and object myclock has only access to the public members. But isn't that practically what I am doing in my setTime function anyways? I understand I am accessing myclock.hr through my public member function. But I am still struggling with the logic behind that. What does it mean to be a private data member then?

Whitefly answered 14/9, 2017 at 7:42 Comment(5)
Here's an analogy for access levels which should help you cseducators.stackexchange.com/a/1246Felicidad
Your question is answered here and in all good c++ textbooks.Sandell
Note that myclock.hr = 5 would let you set hr to anything. Your setTime function is placing restrictions on the values you can set it to. It's maintaining an invariant.Pericline
Just read a bit more in your book. :-)Peoria
@BoPersson Yep, it's starting to make more sense as I read more :) But some of the answers I got from here helped me visualize beyond the simple examples given in the textbook.Whitefly
D
3

The main idea here is encapsulation. Differentiating between a private data member and a public setter method will allow you to change the implementation of your clocktype class and not requiring everyone that uses it to recompile their code. Let's say, for argument's sake, that you decide to change the clocktype implementation to store the number of seconds from the beginning of the day. All you need to do is reimplement the setTime method, and you won't have to touch the public interface:

class clocktype;
{ 
    public:
        void setTime(int,int,int);
    private:
        int secFromMidnight;
};

clocktype myclock;
myclock::setTime(hour, minute, second)
{
    secFromMinight = second + (minute * 60) + (hour * 24 * 60);
}
Diller answered 14/9, 2017 at 7:49 Comment(0)
T
2

Class members in C++ are private by default that means you cannot access them by any outside object (in main). If you define[not just declare] the setTime function within your class, which is a friend function to the private members, you can initialize your private members using an object as well in main.

class clocktype;
{ 
 private:
          int hr;
          int min;
          int sec;

  public:
          void setTime(int hour,int minute,int min)
 {
  if (0<= hour && hour < 24)
  hr = hour;

  if (0<= minute && minute <60)
  min = minute;

  if ( 0<= second && second<60)
  sec = second;
  }
 };

Just declare an object (in main) and invoke setTime and it will work.

Travistravus answered 14/9, 2017 at 8:14 Comment(0)
D
1

setTime is part of the Class clocktype, so it would have access to the private members.

Private generally means, it is private to the class - meaning which, only members within the class have access to it.

Deepfry answered 14/9, 2017 at 7:46 Comment(0)
P
1

There are two things to do when writing a function or a class (an element/symbol) in programming languages:

  1. How to use elements, and this is called an interface, which is what is exposed to the programmer using it
  2. What elements do, and this is called implementation, which is not necessarily exposed, and can change, because it can generally have problems and can be developed over time (ideally speaking)

The idea is simply that the interface, something like your function setTime(), can do multiple things inside the class. Setting time can in the future entail more calls or more complicated things, besides changing some variables, or can be different for other operating systems. Now to the user of your class who received a new version of your library with a bug fix, it won't make a difference, because he's still calling the same function with the same parameters, and the only difference to him is that what that function does is fixed. So, he doesn't have to rework his code to fit your new fix, and you prevent him from playing with the variables of your class, because that's not what you planned in your design. You planned for him to use setTime() specifically.

So, basically, it's just something internal to programmers who exchange libraries. It's just a clean way of writing code. Eventually, for the final outcome when the program is running, it doesn't make a difference whether everything is public. In other words: It's the game of programming, and we play by the rules that we create, to guarantee clean code and long-term stability.

Pudgy answered 14/9, 2017 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.