Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Abstraction
2.1.
Abstraction using Classes
2.2.
Abstraction using Header files
2.3.
Abstraction using Access Specifiers
3.
Abstraction Example
3.1.
Output
4.
Advantages of Abstraction
5.
FAQs
6.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

Abstraction

Author Anant Dhakad
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

In programming, object-oriented programming seeks to implement real-world concepts such as inheritance, hiding, polymorphism, etc. The main goal of OOP is to connect data and the functions that operate on it so that no other part of the code may access the data except that function. In this blog, we will learn about Abstraction, one of the fours pillar of OOPs.

Abstraction

In C++, data abstraction is one of the most significant and crucial features of object-oriented programming. Abstraction refers to revealing only the most essential information while hiding the details. Data abstraction refers to exposing only the most essential aspects of the data to the outside world while hiding the implementation details.
Consider an example of a man driving a car. The man only knows that pressing the accelerators will increase the car's speed and that applying the brakes will stop it. Still, he has no idea how the speed is increased by pressing the accelerators, nor does he understand the car's inner mechanism or how the accelerator, brakes, and other controls are implemented. This is precisely what Abstraction is.
We can implement abstraction in several ways. Following are a few methods to implement abstraction in C++.

Abstraction using Classes

Abstraction can be implemented using classes in C++. We can group data members and member functions in a class using the available access specifiers. A Class can choose which data members are exposed to the outside world and which are hidden.

Abstraction using Header files

Abstraction is also implemented using Header files. For example, we use many mathematical functions present in math.h header file. We use the function pow() in the math.h header file whenever we need to determine the power of an integer and supply the numbers as arguments without knowing the underlying algorithm.

Abstraction using Access Specifiers

In C++, access specifiers provide the foundation for implementing Abstraction. We can use access specifiers to impose restrictions on class members. Access specifiers in C++:

  • Public: members and methods declared as public can be accessed from anywhere in the program.
  • Private: members and methods declared as private can only be accessed within the class.

Using the two properties provided by access specifiers, we can easily implement Abstraction. For example, in a class, the members that specify the internal implementation can be defined as private. Important information that needs to be shared with the outer world can be public. And because they are inside the class, these public members have access to the private members.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Abstraction Example

#include <bits/stdc++.h>
using namespace std;
 
class ExampleAbstraction{
    private:
        // Here we have hidden (made 'a' & 'b' unaccessible) from
        // the outside world.
        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(){
    ExampleAbstraction obj;
    obj.set(30, 40);
    obj.display();
    return 0;
}

Output

a = 30
b = 40


You can also practice with the help of Online C++ Compiler

Advantages of Abstraction

Following are the main advantages of Abstraction:

  • Increase reusability of code and avoids its duplication.
  • It helps in making the application secure as only essential details are provided to the user.
  • We can change the internal implementation of a class without affecting the low-level code.

FAQs

1. What is Object Oriented Programming?

Object Oriented Programming (OOP) is a programming paradigm in which the entire software is composed of a collection of objects that communicate with one another. A collection of data and methods that operate on that data is referred to as an object.

2. What are the main features of OOPs?

Following are the main features of OOPs:
a) Polymorphism
b) Inheritance
c) Encapsulation
d) Abstraction

3. What is Inheritance?

The concept of inheritance is simple: a class is built on another class and uses the other class's data and implementation. The goal of inheritance is to reuse code.

4. What is a Namespace?

All C++ programmes must include Namespaces. In C++, namespaces define a scope for the identifiers used in the programme.

5. What are cin and cout in C++?

The object of the ostream class is cout. By default, the cout stream is connected to the console output device. Its main purpose is to display characters on the console screen. It's analogous to the printf function in C. The object of the istream class is cin. By default, the cin stream is connected to the console input device. Its main purpose is to extract characters from the user. In C, it's similar to scanf.

Key Takeaways

Cheers if you reached here!! 

In this article, we learned about Abstraction in C++. We also saw an example and advantages of abstraction.

Recommended Readings:

We hope that this blog has helped you enhance your knowledge regarding Abstraction in C++ and if you would like to learn more, check out our articles on the platform Coding Ninjas Studio. Also, do check out our course on C++. Do upvote our blog to help other ninjas grow. Happy Coding!

Next article
Encapsulation in C++
Live masterclass