0% found this document useful (0 votes)
8 views9 pages

Absraction & Encapsulation

Uploaded by

hussoali12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Absraction & Encapsulation

Uploaded by

hussoali12345
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

C++ Encapsulation

Encapsulation is one of the key features of object-oriented programming. It


involves the bundling of data members and functions inside a single class.

Bundling similar data members and functions inside a class together also
helps in data hiding.

C++ Encapsulation
In general, encapsulation is a process of wrapping similar code in one place.

In C++, we can bundle data members and functions that operate together
inside a single class. For example,

class Rectangle {
public:
int length;
int breadth;

int getArea() {
return length * breadth;
}
};

In the above program, the function getArea() calculates the area of a


rectangle. To calculate the area, it needs length and breadth.
Hence, the data members ( length and breadth ) and the function getArea() are
kept together in the Rectangle class.
Encapsulation in C++

Example 1: C++ Encapsulation


// Program to calculate the area of a rectangle
#include <iostream>
using namespace std;

class Rectangle {
public:
// Variables required for area calculation
int length;
int breadth;

// Constructor to initialize variables


Rectangle(int len, int brth) {}
// Function to calculate area
int getArea() {
return length * breadth;
}
};

int main() {
// Create object of Rectangle class
Rectangle rect(8, 6);

// Call getArea() function


cout << "Area = " << rect.getArea();

return 0;
}
Run Code

Output

Area = 48

In the above example, we are calculating the area of a rectangle.

To calculate an area, we need two variables: length and breadth and a


function: getArea() . Hence, we bundled these variables and function inside a
single class named Rectangle .

Here, the variables and functions can be accessed from other classes as well.
Hence, this is not data hiding.
This is only encapsulation. We are just keeping similar codes together.
Note: People often consider encapsulation as data hiding, but that's not
entirely true.
Encapsulation refers to the bundling of related fields and methods together.
This can be used to achieve data hiding. Encapsulation in itself is not data
hiding.
Why Encapsulation?
 In C++, encapsulation helps us keep related data and functions together,
which makes our code cleaner and easy to read.

 It helps to control the modification of our data members.

Consider a situation where we want the length field in a class to be non-


negative. Here we can make the length variable private and apply the logic
inside the method setAge() . For example,

 class Rectangle {
 private:
 int age;

 public:
 void setLength(int len) {
 if (len >= 0)
 length = len;
 }

};

 The getter and setter functions provide read-only or write-only access to our
class members. For example,

 getLength() // provides read-only access


setLength() // provides write-only access

 It helps to decouple components of a system. For example, we can


encapsulate code into multiple bundles.

These decoupled components (bundles) can be developed, tested, and


debugged independently and concurrently. And any changes in a particular
component do not have any effect on other components.
 We can also achieve data hiding using encapsulation. In Example 1, if we
change the length and breadth variables into private or protected , then the
access to these fields is restricted.

And, they are kept hidden from outer classes. This is called data hiding.

Data Hiding
Data hiding is a way of restricting the access of our data members by hiding
the implementation details. Encapsulation also provides a way for data hiding.

We can use access modifiers to achieve data hiding in C++. For example,
Example 2: C++ Data Hiding Using the private Specifier
#include <iostream>
using namespace std;

class Rectangle {
private:

// Variables required for area calculation


int length;
int breadth;

public:

// Setter function for length


void setLength(int len) {
length = len;
}

// Setter function for breadth


void setBreadth(int brth) {
breadth = brth;
}

// Getter function for length


int getLength() {
return length;
}

// Getter function for breadth


int getBreadth() {
return breadth;
}
// Function to calculate area
int getArea() {
return length * breadth;
}
};

int main() {
// Create object of Rectangle class
Rectangle rectangle1;

// Initialize length using Setter function


rectangle1.setLength(8);

// Initialize breadth using Setter function


rectangle1.setBreadth(6);

// Access length using Getter function


cout << "Length = " << rectangle1.getLength() << endl;

// Access breadth using Getter function


cout << "Breadth = " << rectangle1.getBreadth() << endl;

// Call getArea() function


cout << "Area = " << rectangle1.getArea();

return 0;
}
Run Code

Output

Length = 8
Breadth = 6
Area = 48

Here, we have made the length and breadth variables private .

This means that these variables cannot be directly accessed outside of


the Rectangle class.
To access these private variables, we have
used public functions setLength() , getLength() , setBreadth() , and getBreadth() .

These are called getter and setter functions.


Making the variables private allowed us to restrict unauthorized access from
outside the class. This is data hiding.
If we try to access the variables from the main() class, we will get an error.

// error: rectangle1.length is inaccessible


rectangle1.length = 8;

// error: rectangle1.breadth is inaccessible


rectangle1.length = 6;
Abstraction in C++

Data abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and
hiding the details. Data abstraction refers to providing only essential information about
the data to the outside world, hiding the background details or implementation.
Consider a real-life example of a man driving a car. The man only knows that pressing
the accelerator will increase the speed of the car or applying brakes will stop the car but
he does not know how on pressing the accelerator the speed is actually increasing, he
does not know about the inner mechanism of the car or the implementation of the
accelerator, brakes, etc in the car. This is what abstraction is.
Types of Abstraction:
1. Data abstraction – This type only shows the required information about the data and
hides the unnecessary data.
2. Control Abstraction – This type only shows the required information about the
implementation and hides unnecessary information.
Abstraction using Classes
We can implement Abstraction in C++ using classes. The class helps us to group data
members and member functions using available access specifiers. A Class can decide
which data member will be visible to the outside world and which is not.

Abstraction in Header files


One more type of abstraction in C++ can be header files. For example, consider the pow()
method present in math.h header file. Whenever we need to calculate the power of a
number, we simply call the function pow() present in the math.h header file and pass the
numbers as arguments without knowing the underlying algorithm according to which the
function is actually calculating the power of numbers.

Abstraction using Access Specifiers


Access specifiers are the main pillar of implementing abstraction in C++. We can use
access specifiers to enforce restrictions on class members. For example:
 Members declared as public in a class can be accessed from anywhere in the
program.
 Members declared as private in a class, can be accessed only from within the class.
They are not allowed to be accessed from any part of the code outside the class.
We can easily implement abstraction using the above two features provided by access
specifiers. Say, the members that define the internal implementation can be marked as
private in a class. And the important information needed to be given to the outside world
can be marked as public. And these public members can access the private members as
they are inside the class.

You might also like