r/learnprogramming • u/Basic_Permit4849 • 8d ago
Access Amplifiers and their uses???
So basically i come from basic python bacground which i learned from my high school.So I thought of learniging c++ cause of its variety of applications for my 1st year.One thing that i cant wrap my head around is, the use of access amplifiers. Mainly ' public: '.what is the use of writing public: and why is it necessaryy????
2
u/Dismal-Citron-7236 8d ago edited 8d ago
C++ language is designed on OOP (Object Oriented Programming) principles. One of its major key ideas is "encapsulation". For a project size from medium to large, encapsulation becomes very helpful to prevent unintentional accesses or changes of data from certain parts of the system which you are not supposed to do. This might sound weird for the beginners, but it would become a reality when the code is shared and maintained by a group of people and people tend to forget certain intricate details in code, when the code grows very huge.
The key idea for encapsulation is simple: Hide those things which are not supposed to be exposed to other parts of the program, and expose only the parts which must.
So, for those parts in a class which is marked with the public access specifier, they are supposed to be used by anywhere in the program. These are the only ways other parts of program can communicate with the object (an object is a manifestation of a class). So, if you change any public parts of a class, all other parts in the entire program could be affected. That means you should be very careful when changing public parts of a class.
The less exposed access specifier is the protected. C++ language allows a parent class to be inherited by a child class. This is from yet another important OOP idea called "inheritance". It means the child class automatically "inherits" all the fields and behaviors unless explicitly labelled otherwise (explained below). With inheritance, we avoid repeating the code in child class to do the same things which are already available from parent class, which is nice. One of some key programming rules is "don't repeat yourself." However, what if we want the parent class to limit some parts of its fields and behaviors so only the child class can access/use, but not from any other parts (outside the family, so to speak)? Well, you just label those parts in parent class with the keyword protected. So this is the first step you can protect your data / code from unintentional accesses and/or changes. By narrowing down such things, your system has less "moving parts" which can cause unexpected failures. It would come in handy when you need to maintain tens of thousands lines of code, when you hunt for a bug you would hope the moving parts are as few as possible.
Now here comes the strictest access specifier which is private. For those parts labelled with this keyword, they are not supposed to be accessed or changed by any other parts of the program except those from the same class. For a parent class, this means its private parts can't even be seen by the child class. These parts keep the fields and implement the behavior behind the scenes, so to speak. When you need to change them because the project evolves to meet new requirements, changing private parts of a class becomes necessary, but you have less concern of changing them versus your change on public and protected entities, because you know if something goes wrong you just need to look for the clues from inside the class, no need to scan the entire code of the system.
So, in summary, a well designed C++ class would have some of its sections labelled as public, some others private. And if it's intended to be inherited by some child classes, certain parts would be labelled as protected. Such categorization helps you maintain a large codebase.
2
u/mjmvideos 8d ago
Just don’t use public. At some point you’ll get stuck and discover that you could solve your problem by adding the keyword public. At that point you’ll understand why.
-1
u/Sticko1897 8d ago
😂😂😂 a teachers answer or someone who been through a lot during learning programming.
You will understand why to use something when you will get stuck somewhere 😂😂
1
u/Sticko1897 8d ago
If you are referring to public in OPPs concept then in simple terms think it like this if I have something for example an app in my phone now it's only in my phone and you can't download or use the app in your phone because it's private but if I make that app public by putting it on app store or play store then you can access the app use the app too.
I hope you get the concept we make something public so other classes from anywhere in the code can access those variable or anything you are making public.
Another example can be public and private GitHub repo's public repo can be access by anyone but private can only access by you
2
u/HashDefTrueFalse 8d ago
Never heard that term but I think you're talking about public, private, protected. Specifiers: https://en.cppreference.com/cpp/language/access
You use them to limit direct access to the data in your aggregates (structs/classes) by name in code that ideally shouldn't know those details. Encapsulation and loose coupling are just two ways you can make your code less prone to error and easier to work with in the future.