What is abstraction

What is abstraction? | c++ Advanced | Part – 7

Imagine you are watching a movie in a movie theatre.

While watching the movie you enjoy the story and the drama happening, Right?

Do you ever think about the technical aspects like from what angle a particular shot was taken, how the set would have been designed, which camera would have been used while shooting?

No

We just focus on the movie and none of these even pops up in our minds.

We focus on the important stuff and forget about the background details.

This is what exactly abstraction is about.

Abstraction is a process of hiding the implementation details from the user and providing only the functionality.

In other words, abstraction is a process of hiding background details and focusing on the essential details.

This way, the user will know the answer to what but not the how of it.

This can also be viewed as data hiding or only showing the essential details.

This becomes necessary as not every user has to see or know everything.

Also, security would be at risk if there is no abstraction.

Abstraction is a process of hiding background details and focusing on the essential details.

Implementing abstraction

We have been using abstraction since the time we started using classes.

Can you guess where?

Do you remember access specifiers?

Access Specifiers are the keywords that specify the access level of members.

Abstraction in C++ is not possible without access specifiers.

Members defined as public can be accessed anywhere within the whole program.

And, members declared as private can only be accessed within the class.

These two form the basis of abstraction.

Abstraction in c++ is not possible without access specifiers.

class OhAbstraction{

private:

string name; //private member of the class

public:

void getName(string newName){

name = newName;

}

void showName(){

cout<<name;

}

};

Here, “name” is a private member of the class, thus it is not accessible outside the class.

But we have member function “getName()” which is used to set the value of the data member “name”.

Thus, we provide an interface for the world to access and set the value of the “name”, instead of making it accessible directly.

class OhAbstraction{

private:

string name;

public:

void getName(string newName){

name = newName;

}

void showName(){

cout<<name;

}

};

int main() {

OhAbstraction oh;

oh.name = “Rahul“; //assigning value to data member “name“

}

Since the member “name” is declared as private and private members cannot be accessed outside the class.

We will encounter an error:

OhAbstraction::name’ is private within this context

Something like the one above.

In order to assign a value to the member function “name” we need to pass the value to the member function getName().

 int main() {

OhAbstraction oh;

oh.getName(“Rahul“); //Calling member function getName()

oh.showName(); //Calling member function showName()

}

Can you guess the output now?

Rahul

To summarize

  • In C++ abstraction is implemented using access specifiers.
  • Abstraction helps improve the security of the application as only important details are provided to the user.
  • Abstraction is a process of hiding background details and focusing on the essential details.
  • In C++, abstraction is achieved using access specifiers.
  • Members defined as public can be accessed anywhere within the whole program.
  • And, members declared as private can only be accessed within the class.

Leave a Comment

Your email address will not be published. Required fields are marked *

PYQ of History UGC NET UGC NET Mathematical Reasoning and Aptitude ICT (Digital Initiatives) | UGC NET | paper – 1 The Scope of Information and Communication Technology(ICT) PYQ of People, Development, and Environment Top 30 PYQ of HINDI | UGC NET – 2023 Top 30 PYQ of Teaching Aptitude PYQ of Research Aptitude | NTA UGC NET | Paper 1 | Part 1 UGC NET Paper 1 Syllabus | Updated | 2023 Types of Research | Research Aptitude | nta ugc net | UGC NET 2023