0

C++_Would this be a good example of using "Private" in a program.

I am learning and practicing using "Struct" and "Private" using C++. I wanted to get some feedback on this practice program I created. Would this be a good example of using the "Private" access identifier? https://code.sololearn.com/c9iNeQd1GBN2/?ref=app

24th Jul 2020, 10:27 PM
Raul Sanchez
Raul Sanchez - avatar
10 Answers
+ 4
Yes you can use it in this way but I would have preferred a class over here instead of a structure. NOTE :- If you don't know what a *class* is then just continue your C++ course and you will get to know about it soon🙂
25th Jul 2020, 12:35 AM
Arsenic
Arsenic - avatar
+ 3
Private data members can be accessed by protected and public methods ...đŸ€”
25th Jul 2020, 3:18 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
Yes that is a good example. But for now, take this into consideration. Your User class holds valuable information such as its name and last name. You do not want anyone who have access the main method to modify your User's name right? The only purpose of main method is to initiate all the classes and perform methods. It not must have something to do with setting the values for the user class. The only class that must be able to set the name of the user, must be the User class itself only. The main method must only display the given value by the User class. How can u set the properties such as name if you cannot modify it in the main class? By using getter and setter method. Setters and getters are public method used by the Class for the main method/ any other method to intantiate/call its value.
25th Jul 2020, 12:39 AM
Jay Gilbert Garzon
Jay Gilbert Garzon - avatar
+ 2
Thank you Jay Gilbert Garzon for your feedback. You are correct, the user's name is valuable information. And that would be bad practice if the name can be accessed through the main. Also True about the setters and getters, I am learning and praciticing more on that topic.
25th Jul 2020, 1:06 AM
Raul Sanchez
Raul Sanchez - avatar
+ 2
Raul Sanchez the main difference between structure and classes is that all the members in a class are private by default whereas for structures they are public by default. Also you can implement other object oriented programming concepts with classes like inheritance which makes it possible to re-use your code - generally Structures are good for small and isolated model objects whereas the classes are suitable for larger or complex objects.
25th Jul 2020, 6:04 AM
Arsenic
Arsenic - avatar
+ 1
Try to analyze this analogy. Lets say that you are a chef, and I am a waiter. In programming view, we are both different class. You, as a Chef class, knows how to cook. Everything about food preparations is all your comfort. Now,I as a Waiter class, does not know anything about cooking. The only thing that I can do is to serve your dishes. I dont care what you cook as long as the food is prepared, the only thing that I would do is to serve. Same scenario for you. You, as a chef class dont know how to serve. Your job is to cook. Lets go back to the programming view. The chef class must be the only one who can set the dishes. I as a waiter cant decide what you will cook because all I do is to serve. Classes must only liable to its own properties/fields. Outside classes cant modify its values. Classes must only provide the values necessary for the other classes. In our example, the finished dish is the only returned value of the chef class. The waiter class cannot set what dish to serve.
25th Jul 2020, 12:49 AM
Jay Gilbert Garzon
Jay Gilbert Garzon - avatar
+ 1
Thanks for your feedback Arsenic . I am currently learning both Classes and Structures. Can you inform me more on the difference between structures and classes. I learned that "Structures" are for small programs and "Classes" are used for bigger programs. Is there any more information, you can tell me, to expand my knowledge between these two.
25th Jul 2020, 12:55 AM
Raul Sanchez
Raul Sanchez - avatar
+ 1
Really great example Jay Gilbert Garzon. I see now how "public" and "private" does go hand in hand with "encapsulation". Really has great control of the program and what other functions or classes can and can't have access to.
25th Jul 2020, 1:17 AM
Raul Sanchez
Raul Sanchez - avatar
+ 1
Precisely. Setters are public methods that sets the value of the private variable. Since this method is within the scope of the class, it can modify the value of its field. Note that this method returns void since it only sets the value. Take this as an example: class User { //properties of the user class set to private private : string name; public: void setName(string name){ this.name = name; // note that the "this" operator pertains to current object, in this case, the user class. "this.name" points to the User's name variable. And the other "name" was the parameter we set in the setName method. This method basically is telling the program to set the value of the private variable class based on the value passed in the setName parameter. }
25th Jul 2020, 1:45 AM
Jay Gilbert Garzon
Jay Gilbert Garzon - avatar
+ 1
Getters on the other hand, is also a public method that returns a value. As continuation, string getName(){ //this method must return a string because we are providing the value of the "name" variable //as I said earlier, "this" operator points to the current object. Since this is still part of the User class, we are pointing to the private variable "name". For your understanding, "this.name" is somehow the same as "User.name" return this.name; }
25th Jul 2020, 1:50 AM
Jay Gilbert Garzon
Jay Gilbert Garzon - avatar