Class 10 | Abstraction and Encapsulation | C++ Notes

Abstraction

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 while hiding the background details or implementation.

Advantages of Abstraction

  1. Helps the user avoid writing low-level code.
  2. Avoids code duplication and increases reusability.
  3. Allows the internal implementation of the class to be changed independently without affecting the user.
  4. Helps increase the security of an application or program because only important details are provided to the user.
  5. Reduces the complexity as well as redundancy of the code, thereby increasing readability.

Ways of Achieving Abstraction

There are different ways to achieve abstraction in C++.

1. Abstraction Using Classes

We can implement abstraction in C++ using classes.

A class helps us group data members and member functions using available access specifiers.

A class can decide which data members will be visible to the outside world and which will remain hidden.

2. Abstraction in Header Files

Another type of abstraction in C++ can be achieved using header files.

For example, consider the pow() method present in the math.h header file.

Whenever we need to calculate the power of a number, we simply call the pow() function and pass the required numbers as arguments.

We do not need to know the underlying algorithm used by the function to calculate the power of the numbers.

3. Abstraction Using Access Specifiers

Access specifiers are one of the main pillars of implementing abstraction in C++.

We can use access specifiers to enforce restrictions on class members.

For example:

Public Members:
Members declared as public in a class can be accessed from anywhere in the program.

Private Members:
Members declared as private in a class can be accessed only from within the class.

They cannot be accessed directly from any part of the code outside the class.

We can implement abstraction using these features provided by access specifiers.

The members that define the internal implementation can be marked as private in a class.

The important information that needs to be provided to the outside world can be marked as public.

These public members can access the private members because they are inside the class.

Example: Achieving Abstraction in C++

#include <iostream>

using namespace std;

class implementAbstraction {

private:

int a, b;

public:

// Method to set values of private members

void set(int x, int y)

{

a = x;

b = y;

}

void display()

{

cout << “a = ” << a << endl;

cout << “b = ” << b << endl;

}

};

int main()

{

implementAbstraction obj;

obj.set(10, 20);

obj.display();

return 0;

}

Encapsulation

Encapsulation in C++ is defined as the wrapping up of data and information into a single unit.

In Object-Oriented Programming, encapsulation means binding together the data and the functions that manipulate that data.

Features of Encapsulation

  1. We cannot access any function from the class directly. We need an object to access a function that uses the member variables of that class.
  2. A function created inside the class must use the member variables of that class for it to be considered encapsulation.
  3. If we do not create a function inside the class that uses the member variable of the class, then it is not considered encapsulation.
  4. Encapsulation improves readability, maintainability, and security by grouping data and methods together.
  5. It helps to control the modification of data members.

Important Properties of Encapsulation

There are two important properties of encapsulation:

1. Data Protection

Encapsulation protects the internal state of an object by keeping its data members private.

Access to and modification of these data members is restricted to the class’s public methods.

This ensures controlled and secure data manipulation.

2. Information Hiding

Encapsulation hides the internal implementation details of a class from external code.

Only the public interface of the class is accessible.

This provides abstraction and simplifies the use of the class while allowing its internal implementation to be modified without affecting external code.

Role of Access Specifiers in Encapsulation

Access specifiers facilitate Data Hiding in C++ programs by restricting access to class member functions and data members.

There are three types of access specifiers in C++:

1. Private

A private access specifier means that the member function or data member can only be accessed by other member functions of the same class.

2. Protected

A protected access specifier means that the member function or data member can be accessed by other member functions of the same class or by derived classes.

3. Public

A public access specifier means that the member function or data member can be accessed by any code.

Note: By default, all data members and member functions of a class are made private by the compiler.

Example 1: Encapsulation in C++

#include <iostream>
#include <string>

using namespace std;

class Person {

private:

string name;

int age;

public:

Person(string name, int age) {

this->name = name;

this->age = age;

}

void setName(string name) {

this->name = name;

}

string getName() {

return name;

}

void setAge(int age) {

this->age = age;

}

int getAge() {

return age;

}

};

int main() {

Person person(“John Doe”, 30);

cout << “Name: ” << person.getName() << endl;

cout << “Age: ” << person.getAge() << endl;

person.setName(“Jane Doe”);

person.setAge(32);

cout << “Name: ” << person.getName() << endl;

cout << “Age: ” << person.getAge() << endl;

return 0;

}

Example 2: Encapsulation in C++

#include <iostream>

using namespace std;

class Encapsulation {

private:

// Data hidden from outside world

int x;

public:

// Function to set value of variable x

void set(int a) {

x = a;

}

// Function to return value of variable x

int get()

{

return x;

}

};

// Driver code

int main()

{

Encapsulation obj;

obj.set(5);

cout << obj.get();

return 0;

}

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play