Skip to content

Commit

Permalink
Added Day 15 (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshitGarg24 authored Jun 13, 2024
1 parent 0793c3d commit 627f82b
Show file tree
Hide file tree
Showing 3 changed files with 249 additions and 0 deletions.
138 changes: 138 additions & 0 deletions docs/day-15/Abstraction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# 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.

## Advantages of Data Abstraction

- Helps the user to avoid writing the low-level code.
- Avoids code duplication and increases reusability.
- Can change the internal implementation of the class independently without affecting the user.
- Helps to increase the security of an application or program as only important details are provided to the user.
- It reduces the complexity as well as the redundancy of the code, therefore increasing the readability.

## Types of Abstraction:

- **Data abstraction** - This type only shows the required information about the data and hides the unnecessary data.
- **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.

## Examples

```cpp
// C++ Program to Demonstrate the
// working of Abstraction
#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;
}
```
**Output**
```
a = 10
b = 20
```

You can see in the above program we are not allowed to access the variables a and b directly, however, one can call the function set() to set the values in a and b and the function display() to display the values of a and b.

```cpp
#include<iostream>
using namespace std;

class Vehicle
{
private:
void piston()
{
cout<<"4 piston\n";
}

void manWhoMade()
{
cout<<"Markus Librette\n";
}
public:
void company()
{
cout<<"GFG\n";
}
void model()
{
cout<<"SIMPLE\n";
}
void color()
{
cout<<"Red/GREEN/Silver\n";
}
void cost()
{
cout<<"Rs. 60000 to 900000\n";
}
void oil()
{
cout<<"PETRO\n";
}
};
int main()
{
Vehicle obj;
obj.company();
obj.model();
obj.color();
obj.cost();
obj.oil();
}
```

**Output**
```
GFG
SIMPLE
Red/GREEN/Silver
Rs. 60000 to 900000
PETRO
```
104 changes: 104 additions & 0 deletions docs/day-15/Encapsulation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Encapsulation in C++

Encapsulation in C++ is defined as the wrapping up of data and information in a single unit. In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.

Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section, etc. Now,
- The finance section handles all the financial transactions and keeps records of all the data related to finance.
- Similarly, the sales section handles all the sales-related activities and keeps records of all the sales.

Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month.

In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data.

This is what **Encapsulation** is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”.

### Two Important property of Encapsulation

- **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, ensuring controlled and secure data manipulation.

- **Information Hiding:** Encapsulation hides the internal implementation details of a class from external code. Only the public interface of the class is accessible, providing abstraction and simplifying the usage of the class while allowing the internal implementation to be modified without impacting external code.

## Features of Encapsulation

Below are the features of encapsulation:
- We can not access any function from the class directly. We need an object to access that function that is using the member variables of that class.
- The function which we are making inside the class must use only member variables, only then it is called encapsulation.
- If we don’t make a function inside the class which is using the member variable of the class then we don’t call it encapsulation.
- Encapsulation improves readability, maintainability, and security by grouping data and methods together.
- It helps to control the modification of our data members.

## Examples

For example if we give input , and output should be half of input

```cpp
#include <iostream>
using namespace std;
class temp{
int a;
int b;
public:
int solve(int input){
a=input;
b=a/2;
return b;
}
};

int main() {
int n;
cin>>n;
temp half;
int ans=half.solve(n);
cout<<ans<<endl;

}
```

Let's take one more example.

```cpp
// C++ program to demonstrate
// Encapsulation
#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;
}
```
**Output**
```
5
```

## Role of Access Specifiers in Encapsulation

Access specifiers facilitate Data Hiding in C++ programs by restricting access to the class member functions and data members. There are three types of access specifiers in C++:

- **Private:** Private access specifier means that the member function or data member can only be accessed by other member functions of the same class.

- **Public:** Public access specifier means that the member function or data member can be accessed by any code.
- **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.

By default, all data members and member functions of a class are made **private** by the compiler.
7 changes: 7 additions & 0 deletions docs/day-15/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"label": "Day 15",
"position": 16,
"link": {
"type": "generated-index"
}
}

0 comments on commit 627f82b

Please sign in to comment.