Encapsulation in c++?

01 Mar 2022 Balmiki Mandal 0 C++

Encapsulation in C++: Understanding Data Protection

Encapsulation in C++ is a process of binding data and functions together into a single unit, called a class. This allows you to control how the data is accessed and modified, and to protect it from outside interference.

Encapsulation is achieved by making the data members of a class private, and by providing public getter and setter methods to access and modify the data. This allows you to control the flow of data into and out of the class, and to ensure that the data is always in a valid state.

Encapsulation has a number of benefits, including:

  • Data protection: Encapsulation protects the data of a class from outside interference. This can be important for security reasons, or to prevent accidental data corruption.
  • Information hiding: Encapsulation allows you to hide the implementation details of a class from its users. This makes the class easier to use and maintain, and it also makes it more difficult to introduce errors.
  • Modularity: Encapsulation allows you to break down your code into smaller, reusable modules. This makes your code easier to understand and maintain, and it also makes it easier to add new features and functionality.

Example of encapsulation in C++:

C++
class Person {
private:
  std::string name;
  int age;

public:
  Person(std::string name, int age) : name(name), age(age) {}

  std::string getName() const { return name; }
  int getAge() const { return age; }

  void setName(std::string name) { this->name = name; }
  void setAge(int age) { this->age = age; }
};

int main() {
  Person person("Alice", 25);

  std::cout << "Hello! My name is " << person.getName() << std::endl;
  std::cout << "I am " << person.getAge() << " years old." << std::endl;

  person.setName("Bob");
  person.setAge(30);

  std::cout << "Hello! My name is " << person.getName() << std::endl;
  std::cout << "I am " << person.getAge() << " years old." << std::endl;

  return 0;
}

In this example, the name and age members of the Person class are private. This means that they can only be accessed by the class's own methods. To access or modify the name and age members, we must use the public getter and setter methods.

The main() function creates a new Person object with the name "Alice" and the age 25. Then, it calls the getName() and getAge() methods to print the person's name and age to the console. Next, it calls the setName() and setAge() methods to change the person's name to "Bob" and their age to 30. Finally, it calls the getName() and getAge() methods again to print the person's new name and age to the console.

 

Encapsulation is a fundamental concept in object-oriented programming. It helps us to write more secure, reliable, and maintainable code.

Further Reading:

For further information and examples, Please visit[ course in production]

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.