I have to prepare a Calendar by using Constructor (Class), adding more conditions for date after 15 days, 1 month later and next year same date. I just wanna confirm, how to define my day, month year, in a class, whether it should be in public or private ???
Asked
Active
Viewed 75 times
-4
-
Don´t understand most of your post, but how about `protected`? – deviantfan Aug 26 '15 at 23:35
-
1I suggest you read up on some OOP concepts, there are several good online resources available freely for the same. Even if someone answers your question here it will be much more beneficial in the long run to understand OOP and what you're doing. – 7VoltCrayon Aug 26 '15 at 23:35
-
5possible duplicate of [Difference between private, public, and protected inheritance](http://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) – Mephy Aug 26 '15 at 23:35
-
Can say protected. Both are same, are not they? – Syed Uzair Hijazi Aug 26 '15 at 23:37
-
Could you rephrase the question (it is not clear what, exactly you intended to ask)? – Aug 26 '15 at 23:40
-
@SyedUzairHijazi No, protected and private are not the same. Not at all. – deviantfan Aug 26 '15 at 23:46
-
My gut feeling is that data (like day, month and year) should be private, and they should be accessible via public methods (basic set/get, and more complex ones). Of course, if you expect to have derived classes, protected can also make a lot of sense. It's really hard to tell without a full description of the requirements. – Fabio says Reinstate Monica Aug 27 '15 at 00:06
1 Answers
-2
Good practice is to make class members protected or private. That's implements encapsulation, by hiding implementation from user.
Then, if you need to get/set variable from outside, you create getter and setter function.

EvgeniyZh
- 898
- 9
- 21
-
Meh, overgeneralization IMO. Get your class design right. Many uses of getters and setters, especially in school/uni environments, should have been aggregates. – Baum mit Augen Aug 26 '15 at 23:48
-
-
That is correct (well you can also use `class {public:...`, does not actually change anything, but sure, `struct` is more appropriate for aggregates). But what does this have to do with your way to imprecise getter/setter advice? – Baum mit Augen Aug 27 '15 at 01:08