Min menu

Pages

VB

Encapsulation in C Plus | C++

Packaging in C-Plus Plus  

Packaging in C-Plus Plus (Encapsulation)It is a method that can be followed to hide the characteristics of the class(Global Variables)And making objects you create from it and other classes that include it are able to handle these properties only through functions created by the main programmer of the class.

So the benefit of encapsulation is that it allows you to set conditions for storing values ​​in properties, and it also allows you to get the values ​​the way you want them.


What is the method used in the packaging process? C++

Since the basic idea of ​​encapsulation is to hide the data on the one hand and make it possible to deal with it on the other hand.

The first thing to think of is that you must make the type of all properties(which variables will save the data)in the class privatebecause defining make it privatemeans that they can only be accessed from within the class they are in.

The second thing you have to think about is finding a way to access these properties from the outside.
To do this, you have to prepare functions of their type publicto handle these properties, because functions of their type publiccan be accessed from anywhere.

So to achieve the encapsulation principle , you have to set the type of properties privateand make the type of functions that are used to access thempublic.


concept of functionstheSetterAnd thetheGetter In C Plus Plus  C++

When dealing with any variable(or feature)You have two options, either to give it a new value, or to get the value in it.
Since functions must be built to deal with each of the properties in the class, it is recommended to adopt well-known names as follows:

  • Start the name of each function whose purpose is to give a value to the property with the wordsetThen the name of the property.

  • Start the name of each function whose purpose is to get the value of the property with the wordgetThen the name of the property.

Comprehensive examples of packaging inC++

Now we will create a class with its name Employeeand idea to store employee information such as namename,Salarysalary,

the ageage.
Then we will try to create an object from the class Employeein the function main()and deal with it.

Note: Here we have defined everything plain without applying any of the principles of encapsulation.

The first example is C-Plus Plus packaging

main.cpp
#include <iostream>
	
		using namespace std;
	
		// Contains 3 employee properties. Here we have defined a class named
		class employee {
	
		public:
		string name; // because it is a string whose type is the name
		int age; // because it is an int of its type age
		double salary; // Because it is a large number, it can contain a double comma. The salary is its type
	
		};
	
		// main() Here we have defined the function
		int main()
		{
		// e named Employee Here we created an object from the class
		employee e;
	
		// e Here we have given values ​​for the properties of the object
		e.name = "Mahamad";
		e.age = 21;
		e.salary = 950;
	
		// e Here we have shown the object properties values
		cout << "Name: " << e.name << "\n";
		cout << "Age: " << e.age << "\n";
		cout << "Salary: " << e.salary;
		}
  

We will get the following result when running.

Name: Mhamad
		Age: 21
		Salary: 950
  


Now we will repeat the previous example with the type of properties set privateand we will define functions of their type publicto deal with these properties.

Note: In this example we have applied the encapsulation principle.

The second example in C-Plus Plus packaging

main.cpp
#include <iostream>
	
		using namespace std;
	
		// Employee Here we have defined the class
		class employee {
	
		// Therefore, they can no longer be accessed directly from outside the private class. Note that we have set the type of properties
		private:
		string name;
		int age;
		double salary;
	
		// In order to be able to access from outside the public class, here we have defined all the functions through which we will treat properties as
		public:
		// name This function returns the value of the stored property
		string getName() {
		return name;
		}
	
		// age This function returns the value of the stored property
		int getAge() {
		return age;
		}
	
		// salary This function returns the value of the stored property
		double getSalary() {
		return salary;
		}
	
		// name This function is given a name and it is assigned to the property
		void setName(string n) {
		name = n;
		}
	
		// age this function we give it a number and it puts it in the property
		void setAge(int a) {
		age = a;
		}
	
		// salary for this function we give it a number and it puts it in the property
		void setSalary(double s) {
		salary = s;
		}
		};
	
		// main() Here we have defined the function
		int main()
		{
		// e named Employee Here we created an object from the class
		employee e;
	
		// Setter Through the functions of e here we set values ​​for the properties of the object
		e.setName("Mhamad");
		e.setAge(21);
		e.setSalary(950);
	
		// Getter Through the functions of e here we display the values ​​of the object's properties
		cout << "Name: " << e.getName() << "\n";
		cout << "Age: " << e.getAge() << "\n";
		cout << "Salary: " << e.getSalary();
		}
  

We will get the following result when running.

Name: Mhamad
		Age: 21
		Salary: 950
  


Now we will repeat the previous example with the addition of a condition to the function setName()that specifies that when entering the name it must consist of more than two characters.
And adding a condition to the function getName()to make it return Not definedif there is no name entered in the propertyname.

Note: We have marked the new terms and conditions in yellow.

The third example in C-Plus Plus packaging

main.cpp
#include <iostream>
	
		using namespace std;
	
		// Employee Here we have defined the class
		class employee {
	
		// private Note that we have kept the type of properties
		private:
		string name;
		int age;
		double salary;
	
		public:
		// name In case there is no name entered in the property "Not defined" to make it return the statement getName() here we put a condition in the function
		string getName() {
		if (name == "")
		{
		return "Not defined";
		}
		else
		{
		return "Mr." + name;
		}
		}
	
		// We have not made any modifications to this function
		int getAge() {
		return age;
		}
	
		// We have not made any modifications to this function
		double getSalary() {
		return salary;
		}
	
		// Only if the name we pass to it consists of more than two characters to make it accept storing the name in the property setName() here we put a condition in the function
		void setName(string n) {
		if (n.length() < 3)
		{
		cout << "Name is too short, name can't be less then 3 characters!\n";
		}
		else
		{
		name = n;
		}
		}
	
		// We have not made any modifications to this function
		void setAge(int a) {
		age = a;
		}
	
		// age this function we give it a number and it puts it in the property
		void setSalary(double s) {
		salary = s;
		}
		};
	
		// main() Here we have defined the function
		int main()
		{
		// e named Employee Here we created an object from the class
		employee e;
	
		e.setName("dj"); // as a noun because it consists of only two letters "dj" here will not accept us to enter
		e.setAge(21);
		e.setSalary(950);
	
		cout << "Name: " << e.getName() << "\n"; // Because the name we entered earlier was not accepted "Not defined" here the function is supposed to return a statement
		cout << "Age: " << e.getAge() << "\n";
		cout << "Salary: " << e.getSalary();
		}
  

We will get the following result when running.

Name is too short, name can't be less then 3 characters!
		Name: Not defined
		Age: 21
		Salary: 950
  

Note that the name we entered through the function was not accepted setName()because it is less than three letters, so the message that we prepared is printed if a name less than three letters is entered.
And since it did not put the name that we entered in the property name, notice that the function getName()returns the value Not definedthat we have prepared in the event that no name is entered in it.