DEFINITION 17: PUBLIC PRIVATE PROTECTED - PRATMG/2143-OOP-Tamang GitHub Wiki

  • PUBLIC
  • PRIVATE
  • PROTECTED

These are types of access modifiers available in C++ that are used to implement an important aspect of Object-Oriented Programming known as Data Hiding.

NOTE: If we do not specify any access modifiers for the members inside the class then by default the access modifier for the members will be Private.

acessmod

PUBLIC:

Everyone will have access to all class members declared under the public specifier. Other classes and functions can access the data members and member functions that have been declared as public. A class's public members can be accessed from anywhere in the program by using the direct member access operator (.) with the class's object.

// 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 datamember outside class
	obj.radius = 5.5;
	
	cout << "Radius is: " << obj.radius << "\n";
	cout << "Area is: " << obj.compute_area();
	return 0;
}

PRIVATE:

Class members marked as private can only be accessed by the class's member functions. They may not be accessed directly by any object or function that is not a member of the class. Only member functions or friend functions have access to a class's private data members. However, we can access the private data members of a class indirectly using the public member functions of the class.

// 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;
}

PROTECTED:

Protected access modifier is similar to private access modifier in that it can only be accessed outside of its class with the help of a buddy class; but, class members defined as Protected can also be accessed by any subclass(derived class) of that class.

// C++ program to demonstrate
// protected access modifier
#include <bits/stdc++.h>
using namespace std;

// base class
class Parent
{
	// protected data members
	protected:
	int id_protected;
	
};

// sub class or derived class from public base class
class Child : public Parent
{
	public:
	void setId(int id)
	{
		
		// Child class is able to access the inherited
		// protected data members of base class
		
		id_protected = id;
		
	}
	
	void displayId()
	{
		cout << "id_protected is: " << id_protected << endl;
	}
};

// main function
int main() {
	
	Child obj1;
	
	// member function of the derived class can
	// access the protected data members of the base class
	
	obj1.setId(81);
	obj1.displayId();
	return 0;
}

Reference:

⚠️ **GitHub.com Fallback** ⚠️