Introduction to C++ | Data Abstraction

Data Abstraction in C++

Data Abstraction is one of the fundamental principles of Object-Oriented Programming (OOP). It allows the programmer to hide the complex implementation details and show only the essential features of the object. In C++, data abstraction is achieved using abstract classes and interfaces. The idea is to expose only the necessary information to the user, hiding the internal workings.

What is Data Abstraction?

Data abstraction means providing only essential information to the outside world and hiding the implementation details. In C++, this is typically done by creating classes with private or protected data members and providing public member functions to access or modify those data members. This provides a controlled way to interact with the object, ensuring that its internal data is not directly modified.

How Does Data Abstraction Work?

In C++, data abstraction is implemented using:

Syntax of Data Abstraction

Syntax Example


        class AbstractClass {
        public:
            virtual void display() = 0;  // Pure virtual function
        };
                    

Example of Data Abstraction in C++

Here’s an example of using data abstraction in C++ to hide the internal details of a class and provide a controlled way to interact with it:

Code Example


        #include <iostream>
        using namespace std;
        
        // Abstract class for Vehicle
        class Vehicle {
        public:
            virtual void startEngine() = 0;  // Pure virtual function
        
            void showDetails() {
                cout << "This is a vehicle" << endl;
            }
        };
        
        // Derived class Car
        class Car : public Vehicle {
        private:
            string carModel;
        
        public:
            Car(string model) : carModel(model) {}
        
            // Implementing the abstract method
            void startEngine() override {
                cout << "Starting engine of car model: " << carModel << endl;
            }
        };
        
        // Derived class Bike
        class Bike : public Vehicle {
        private:
            string bikeModel;
        
        public:
            Bike(string model) : bikeModel(model) {}
        
            // Implementing the abstract method
            void startEngine() override {
                cout << "Starting engine of bike model: " << bikeModel << endl;
            }
        };
        
        int main() {
            Vehicle* vehicle1 = new Car("Toyota Corolla");
            Vehicle* vehicle2 = new Bike("Yamaha R15");
        
            vehicle1->startEngine();
            vehicle2->startEngine();
        
            delete vehicle1;
            delete vehicle2;
        
            return 0;
        }
                    

Output

Starting engine of car model: Toyota Corolla
Starting engine of bike model: Yamaha R15

Key Points About Data Abstraction

Advantages of Data Abstraction

Pro Tip:

💡 Pro Tip

When designing classes, always ask yourself: What information should be accessible to users of this class, and what should be hidden? This will help ensure that your class follows the principles of data abstraction and provides a clean interface while protecting its internals.