Why do we use private public and protected in c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Why do we use private public and protected in c++

Why do we use private or public As a programmer we have known everything about program or moreover we dont provide our main program to a user we provide complied version so no one can make any change in our program so what is the purpose of make thing private like salary ...like as a programmer we have a choice what to print on screen if we dont put salary on screen no one can see it then why public private or protected

30th Jul 2021, 8:03 AM
Atul Chourasiya
Atul Chourasiya - avatar
4 Answers
+ 3
Good question. Mostly it is not related to security but restricting the code can help you keep your code understandable and structured. For example if you are using an IDE with autocomplete feature it won't suggest you every function but only public ones in other class so instead of an english dictionary you will see only useful a few words. It also helps you to avoid hidden semantic errors that compiler can't detect like mixing two different variables with same name.
30th Jul 2021, 12:26 PM
▬▬▬
▬▬▬ - avatar
+ 3
Its for security purposes what if your phone or pc use by everyone this is called public but what if u gave permission for only your family members this is protected if u give permission only for specific person or no other can use rather then u this is private so each keywords have its own functionality .
30th Jul 2021, 8:08 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
NotHuman thanks bro this is exactly what i want
30th Jul 2021, 2:09 PM
Atul Chourasiya
Atul Chourasiya - avatar
0
Private, public and protected only make sense if the application has a logon system, but normally the data comes from a database with restrictions, and in that case, then private, public and protected don't make much sense, because it's already restricted from the database what the user has access to.
30th Jul 2021, 12:47 PM
Jan
Jan - avatar