Encapsulation
is one of the fundamental principles of Object-Oriented Programming (OOP), and it refers to the bundling of data and methods that operate on that data within a single unit, known as a class. In C++, encapsulation is primarily achieved through the use of access specifiers and the organization of class members.
Access Specifiers:
1. Public (public)
Members declared as public are accessible from anywhere in the program.
Public members form the interface of the class, defining how external code can interact with the class.
class
MyClass
{
public:
int
publicVar;
void
publicMethod
() {
}
}
2. Private (private):
Members declared as private are accessible only within the class itself.
Private members are hidden from external code, providing a level of data protection.
class
MyClass
{
private:
int
privateVar;
void
privateMethod
() {
}
public:
void
publicMethod
() {
}
};
3. Protected (protected):
Similar to private, but with a difference in inheritance. Members declared as protected are accessible within the class and its derived classes.
class
MyBaseClass
{
protected:
int
protectedVar;
};
class
MyDerivedClass
:
public
MyBaseClass
{
public:
void
accessProtected
() {
protectedVar
=
42
;
}
};
Implementing Encapsulation:
1. Private Data Members:
Declare data members as private to encapsulate them within the class.
Access them only through public methods (getter and setter methods) to control and validate access.
class
BankAccount
{
private:
double
balance;
public:
double
getBalance
()
const
{
return
balance;
}
void
setBalance
(
double
amount) {
balance
= amount;
}
};
2. Getter and Setter Methods:
Getter methods allow external code to access the values of private members.
Setter methods provide a controlled way to modify the values of private members, often with additional validation.
class
Student
{
private:
string
name;
int
age;
public:
string
getName
()
const
{
return
name;
}
int
getAge
()
const
{
return
age;
}
void
setName
(
const
string
&
newName
) {
name
=
newName;
}
void
setAge
(
int
newAge
) {
age
=
newAge;
}
};
Benefits of Encapsulation:
(a). Data Hiding:
Encapsulation hides the internal details of a class from external code, preventing direct access to the implementation details.
(b). Modularity:
The encapsulated code is modular, allowing changes to one part of the code without affecting other parts. This enhances maintainability.
(c). Security:
By keeping data members private and controlling access through methods, encapsulation enhances security by preventing unintended interference.
Example:
#include <iostream>
Copy Code
class
Person
{
private:
std::string
name;
int
age;
public:
std::string
getName
()
const
{
return
name;
}
int
getAge
()
const
{
return
age;
}
void
setName
(
const
std::string
&
newName
) {
name
=
newName;
}
void
setAge
(
int
newAge
) {
if
(
newAge
>
0
) {
age
=
newAge;
}
}
};
int
main
() {
Person
person
;
person
.
setName
(
"John Doe"
);
person
.
setAge
(
25
);
std::cout
<<
"Name: "
<<
person
.
getName
() <<
std::endl
;
std::cout
<<
"Age: "
<<
person
.
getAge
() <<
std::endl
;
return
0
;
}
In this example, the Person class
encapsulates
the name and age members. The external code interacts with these members through
getter
and
setter
methods, ensuring controlled access and encapsulation.
Encapsulation
, along with other
OOP
principles, contributes to building
robust
,
modular
, and
maintainable
software systems in C++.