Index
Encapsulation
Encapsulation is one of the core principles of object-oriented programming (OOP) that refers to the bundling of data and methods that operate on that data into a single unit or class. It is a mechanism that restricts direct access to some of the object’s components, preventing the accidental modification of data and ensuring that the internal representation of an object remains hidden from the outside world.
Here are some key points about encapsulation:
- Data Hiding: Encapsulation allows the internal representation of an object to be hidden from the outside. Only the necessary information or interface is exposed to the user, preventing unauthorized access and modification.
- Access Control: Access specifiers (public, private, protected) are used to control the visibility of class members. Private members can only be accessed within the class, while public members can be accessed from outside the class. Protected members are similar to private members but with additional access in derived classes.
- Information Hiding: Encapsulation enables the concept of information hiding, where the internal details of an object are hidden from the user. Users interact with the object through a well-defined interface provided by public methods.
- Data Integrity: By encapsulating data and providing controlled access through methods, encapsulation helps in maintaining the integrity of data. It ensures that data is modified in a controlled and consistent manner, reducing the chances of accidental errors and data corruption.
- Modularity and Reusability: Encapsulation promotes modularity by organizing related data and methods into a single unit or class. It allows for code reuse and simplifies maintenance and modification since changes to the internal implementation of a class do not affect its users as long as the interface remains unchanged.
Encapsulation is a fundamental concept in OOP and plays a crucial role in designing robust and maintainable software systems. It encourages good programming practices such as information hiding, separation of concerns, and decoupling of components, leading to cleaner, more manageable code.
Access Private Members
To access a private attribute, use public “get” and “set” methods:
Example:
#include <iostream>
using namespace std;
class Car {
private:
int price;
public:
void setprice(int p) {
price = p;
}
int getprice() {
return price;
}
};
int main() {
Car myObj;
myObj.setprice(1000000);
cout << myObj.getprice();
return 0;
}
Output:
1000000
Explanation:
- We define a class named
Car
with a private member variableprice
. Theprice
variable represents the price of the car and is only accessible within theCar
class. - The class
Car
also contains two public member functions:setPrice(int p)
: This function sets the value of theprice
member variable to the value passed as an argument.getPrice()
: This function returns the value of theprice
member variable.
- In the
main()
function:- We create an object named
myObj
of theCar
class. - We set the price of the car using the
setPrice()
method by passing the value1000000
. - We then use the
getPrice()
method to retrieve the price of the car and print it usingcout
.
- We create an object named
This program demonstrates encapsulation, where the internal data (price
) of the Car
class is hidden from the outside world and can only be accessed and modified through the public member functions setPrice()
and getPrice()
. This helps in maintaining the integrity of the data and prevents direct access to the private member variable price
.