What does it mean to be a private member (c++ classes)? -


i little confused private data members in c++ classes. new coding , still in middle of 'classes' chapter might ahead of myself, feel missing piece of information:

let's have code:

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

and create 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 can't this:

myclock.hr = 5; 

because hr private data member , object myclock has access public members. isn't practically doing in settime function anyways? understand accessing myclock.hr through public member function. still struggling logic behind that. mean private data member then?

the main idea here encapsulation. differentiating between private data member , public setter method allow change implementation of clocktype class , not requiring uses recompile code. let's say, argument's sake, decide change clocktype implementation store number of seconds beginning of day. need reimplement settime method, , won't have touch 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); } 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -