Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - ACCESS MODIFIERS


Access Modifiers provides certain level of restriction to the usage of the class members so that they are not accidentally modified.


Let us understand it in detail.


The main motto of using Classes and Objects is to maintain certain standards. So, that the data inside the objects do not get modified or deleted accidentally.


i.e. Let us consider the class Human.


Example :



class Human {

	private :
		int age;
	
	public: 
		int getAge() {
        	return age;
    	}

    	void setAge(int ag) {
        	age = ag;
    	}
};



The above class Human has an attribute called age. And we are using a setter method


void setAge(int ag) {
	age = ag;
}

to set some value to the age.


And we have a getter method


int getAge() {
	return age;
}

to get the age.


So, we do not access the data members (i.e. age) directly outside the class. We need to use a getter and setter method to do that.


And this is where Access Modifiers comes into picture. It puts a restriction on the variables.


Let us see each in detail :


Private Access Modifier


A Private access modifier will never allow a class member to be accessed outside the class.Usually, an attribute is marked as private(i.e. age in the above class). So, that they should not be accessed outside the class.


A private data member can be accessed only inside a class.


Example :



class Human {

	private :
		int age;
	
	public: 
		int getAge() {
        	return age;
    	}

    	void setAge(int ag) {
        	age = ag;
    	}
}; 



Public Access Modifier


A Public data member can be accessed outside the class. Usually, methods in a class are Public and attributes in a class are private.


Also, a class can be declared public, so that this class is accessible and visible to all the classes in all the packages in C++.


If you are declaring an attribute of a class public. You can access it from outside the class. No getter and setter method is needed for that. But that's never a good practice.


class Human {

	public:
		int age; // Not a good practice but allowed.
};

Protected Access Modifier


A Protected data member cannot be accessed outside the class. But it can be accessed by a child class inheriting the parent class.


Let us understand with the below example :


Example :



#include <iostream>
using namespace std;

class Animal {

	protected:
		void legs(){
			cout << "An animal has 4 legs." << endl;
		}
};

class Cat : Animal {

	public:
		void accessLegs() {
			legs();
		}
};


int main() {	

	Cat cat;
	cat.accessLegs();

	return 0;
}


Output :



  An animal has 4 legs.

So, in the above code, there is an Animal class.


class Animal {

	protected:
		void legs(){
			cout << "An animal has 4 legs." << endl;
		}
};

That has a protected method legs().


protected:
	void legs(){
		cout << "An animal has 4 legs." << endl;
	}

Now as per the definition of protected, a protected member can be accessed by it's child class.


And the child class of Animal is Cat.


class Cat : Animal {
	public:
		void accessLegs() {
			legs();
		}
};

And we called the legs() method of Animal class from the Cat class.