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




C++ - GETTERS & SETTERS


So, as we have learnt in the previous tutorial that a Human being can have a name, speaks a language and eats some kind of food based on their choice.


So, we have the below class Human.


Example :



class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
		void eat()
		{
			cout << "Eats " << food;
		}

		void speak()
		{
			cout << "Speaks " << language;
		}
};



Just imagine, when God had created the Human Beings, God had prepared a blue print stating, all Humans will have a common behavior of speaking and eating.


Also we need to create objects out of Human class.


Human human;

So far it's ok. A Human being is created. But how do we set the details for the Human. In simple words, how do we create the Human Being whose name is John and he speaks English and eats Burger.


For that we have something called getters and setters.


Now, let us say, we create an object of Human class :


Human human;

and want to access the name property of Human,


human.name

Accessing name using human.name is not at all a good practice. Rather we need to use a method/behaviour to access the name property, popularly known as getter and setter methods.


Getter and Setter methods


The Setter method is used to set or assign values to a property(example:- name) of a class. And the Getter method is used to get the values of a property(example:- name) in a class.


Let us modify the Human class using getter and setter methods :


Example :



class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
	    void setName(string nme) {
            name = nme;
        }

        string getFood() {
            return food;
        }

        void setFood(string fd) {
            food = fd;
        }

        string getLanguage() {
            return language;
        }

        void setLanguage(string lang) {
            language = lang;
        }
	
		void eat()
		{
			cout << name << " eats " << food << endl;
		}

		void speak()
		{
			cout << name << " speaks " << language << endl;
		}
};




Getter for 'name'


public string getName() {
	return name;
}

Setter for 'name'


public void setName(string nme) {
	this.name = nme;
}

Now, let us look at the complete code.


How do we assign values to an object using Setter?


Example :



#include <iostream>
using namespace std;

class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
	    void setName(string nme) {
            name = nme;
        }

        string getFood() {
            return food;
        }

        void setFood(string fd) {
            food = fd;
        }

        string getLanguage() {
            return language;
        }

        void setLanguage(string lang) {
            language = lang;
        }
	
		void eat()
		{
			cout << name << " eats " << food << endl;
		}

		void speak()
		{
			cout << name << " speaks " << language << endl;
		}
};

int main() {	
	Human human;
	human.setName("John");
	human.setFood("Burger");
	human.setLanguage("English");
		
	human.eat();
	human.speak();
	
	return 0;
}


Output :



  John eats Burger
  John speaks English

So, if you look at the above code, we have a setter method for name,


human.setName("John");

And what happens is the setter method for the name attribute gets called,


void setName(string nme) {
	name = nme;
}

And the name gets assigned to the variable nme of the setter method,

java_Collections
java_Collections

Then in the next line, the name John gets assigned to the name attribute of human class.

java_Collections

Similarly, the setter methods for food and language gets the value assigned for food and language.


human.setFood("Burger");
human.setLanguage("English");

And we get the below object.

java_Collections

Now, when eat() method is called used human object,


human.eat();

The eat() method of Human class is called,


void eat()
{
	cout << name << " eats " << food << endl;
}

And C++ finds that the name and food attribute of the human object needs to be displayed.


So it goes to the human object and finds name and food has the values John and Burger.


And we get the output,


John eats Burger

Similarly, when speak() method is called,


human.speak();

It specifies the behaviors of what John speaks and speak() methods are called


void speak()
{
	cout << name << " speaks " << language << endl;
}

And we get the below output,


John speaks English

How do we get the values of a class property using Getter?


Let us modify the above code and see how getter method works.


Example :



#include <iostream>
using namespace std;

class Human
{
	private :
    	string name;
		string food;
		string language;
        
	public :
	   string getName() {
            return name;
        }
        
	    void setName(string nme) {
            name = nme;
        }

        string getFood() {
            return food;
        }

        void setFood(string fd) {
            food = fd;
        }

        string getLanguage() {
            return language;
        }

        void setLanguage(string lang) {
            language = lang;
        }
};

int main() {	
	Human human;
	human.setName("John");
	human.setFood("Burger");
	human.setLanguage("English");
		
	cout << "His name is " << human.getName() << endl;
	cout << "He eats " << human.getFood() << endl;
	cout << "His language is " << human.getLanguage() << endl;
	
	return 0;
}


Output :



  His name is John
  He eats Burger
  His language is English

So, to display the name attribute, we have called the getter method for name.


cout << "His name is " << human.getName() << endl;

And the getter method for name is called,


string getName() {
	return name;
}

And the value of name from human object is displayed.


His name is John

Similar logic applies for food and language.


cout << "He eats " << human.getFood() << endl;
cout << "His language is " << human.getLanguage() << endl;

It displays the values of food and language properties of Human class,


He eats Burger
His language is English