Difference between Public and Private in C++ with Example
All the class members declared under public will be available to everyone. The data members and member functions declared public can be accessed by other classes too. The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class.
Example:
// C++ program to demonstrate public // access modifier #include <iostream> using namespace std; // class definition class Circle { public: double radius; double compute_area() { return 3.14 * radius * radius; } }; // main function int main() { Circle obj; // accessing public data member outside class obj.radius = 5.5; cout << "Radius is: " << obj.radius << "\n"; cout << "Area is: " << obj.compute_area(); return 0; } |
Radius is: 5.5 Area is: 94.985
In the above program, the data member radius is public so we are allowed to access it outside the class.
The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.
Example:
// C++ program to demonstrate private // access modifier #include <iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: void compute_area(double r) { // member function can access private // data member radius radius = r; double area = 3.14 * radius * radius; cout << "Radius is: " << radius << endl; cout << "Area is: " << area; } }; // main function int main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.compute_area(1.5); return 0; } |
Radius is: 1.5 Area is: 7.065
Difference between Public and Private
| Public | Private |
|---|---|
| All the class members declared under public will be available to everyone. | The class members declared as private can be accessed only by the functions inside the class. |
| The data members and member functions declared public can be accessed by other classes too. | Only the member functions or the friend functions are allowed to access the private data members of a class. |
| The public members of a class can be accessed from anywhere in the program using the direct member access operator (.) with the object of that class. | They are not allowed to be accessed directly by any object or function outside the class. |
Recommended Posts:
- Difference between Private key and Public key
- Difference between Private and Public IP addresses
- Difference Between Virtual Private Network (VPN) and Proxy
- Public vs Protected in C++ with Examples
- Difference between CPU and GPU
- Difference between LAN and MAN
- Difference between 1G and 2G
- Difference between LED and LCD
- Difference between TDM and FDM
- Difference between LAN and WAN
- What is the difference between GUI and CUI?
- Difference between CRT and LCD
- Difference between MAN and WAN
- Difference between RPC and RMI
- Difference between Blu-ray and DVD
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



