Encapsulation is a fundamental concept in object-oriented programming (OOP) that focuses on bundling data and methods that operate on that data into a single unit or class. It also involves restricting direct access to some of an object's components, which is a means of preventing unintended interference and misuse. Encapsulation helps in hiding the internal state and requiring all interactions to be performed through an object's methods, thus providing a controlled and safe interface to the object.
• Class: A class in C++ is a blueprint for creating objects. It defines data members (variables) and member functions (methods) that operate on the data. Classes provide the mechanism for encapsulating data and functions.
• Object: An object is an instance of a class. It holds data and uses member functions to operate on that data.
Public: Members declared as public can be accessed from outside the class. They define the interface for interacting with the class.
Private: Members declared as private can only be accessed from within the class itself. They are hidden from outside classes and functions.
Protected: Members declared as protected can be accessed by derived classes, but not by functions or classes that are not derived from the class.
By default, members of a class are private unless specified otherwise.
• Accessor Functions: These functions are used to get the values of private data members. They do not modify the data and are typically called getters or accessors.
• Mutator Functions: These functions are used to set or modify the values of private data members. They are also known as setters or mutators.
• hiding is achieved by declaring the data members of a class as private. Access to these data members is controlled through public member functions, which provide a controlled way of accessing or modifying the data.
Here is a basic example that demonstrates encapsulation using a Person class:
<#include <iostream>>
<#include <string>>
class Person {
private:
// Private data members
std::string name;
int age;
public:
// Public member functions
// Accessor functions
std::string getName() const {
return name;
}
int getAge() const {
return age;
}
// Mutator functions
void setName(const std::string& newName) {
name = newName;
}
void setAge(int newAge) {
if (newAge > 0) { // Ensuring valid age
age = newAge;
}
}
};
int main() {
Person person;
// Using mutator functions to set values
person.setName("John Doe");
person.setAge(30);
// Using accessor functions to get values
std::cout << "Name: " << person.getName() << std::endl;
std::cout << "Age: " << person.getAge() << std::endl;
return 0;
}
Name: John Doe
Age: 30
Encapsulation allows changes to the internal implementation of a class without affecting the code that uses the class. This separation helps in maintaining and updating the code more easily.
By making data members private and controlling access through public methods, encapsulation protects the integrity of the data and prevents unauthorized access and modification.
Encapsulation helps in designing modular code that can be reused across different parts of a program or even different programs.
Encapsulation provides a layer of security by ensuring that sensitive data is not directly accessible from outside the class. Only authorized methods have access to modify the data.