Abstraction in C++

Abstraction is a design concept of hiding data or implementation wherever possible and expose the essential parts only. Data hiding helps to prevent potential misuse and accidental change of data. Implementation hiding helps refactoring code by doing localized changes.

In C++, the data and implementations are kept hidden as much as possible within the classes.

Data Abstraction

class Student {
public:
    string name;

    Student(string s) {
        name = s;
    }
};

In this Student class, we have a data member, name. The name is set from the constructor in type of object creation. The calling code can access the name directly from a student object. But the problem is that the calling code can change the name also intentionally or unintentionally. We don’t want that because the student name is a type of information that should not be changed.

So, we need a mechanism to give read but not write permission of this data to the calling code.

class Student {
public:
    Student(string s) {
        name = s;
    }

    string getName() {
        return name;
    }

private:
    string name;
};

So, we made the member variable private to revoke access from outside the class. Now the calling code can not change the student name. But it can not access the name also.

That’s why we added a semantically related member function getName() to return the student name. Now the calling code can access the student name via this getName() function. But there is no way for the calling code to change it.

Implementation Abstraction

class Student {
public:
    Student(string s, int marks = 0) {
        name = s;
        total_marks = marks;
    }

    string getName() {
        return name;
    }

    string grade() {
        if (total_marks >= 80) return string("A");

        return string("B");
    }
private:
    string name;
    int total_marks;
};

In the Student class, we added one more function grade() to return a student’s grade based on total_marks. The function returns grade “A” if total marks is 80 or above, otherwise grade “B”.

The calling code can use this function to get students’ grade from multiple places. How the grade is being calculated is totally hidden in the class and from the calling code.

Say, after few years, the school authority decided to change the grading system. Minimum 90 marks is required from grade “A” in new system. We can refactor the class without any impact in the calling code.

We can change the implementation of the grade() function.

.
.
    string grade() {
        if (total_marks >= 90) return string("A");

        return string("B");
    }
.
.

After this change in the class, the new grading system will be in effect in the whole program.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

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

0
0
0
0
0
0