Menu

Abstraction in C++ - Explained in Simple Words
📰

Abstraction in C++ - Explained in Simple Words

DEV Community: cpp·Aliza Kanwal·4 days ago
#pQFXcJ8b
#devtocpp#devto#dev#include#abstraction
Reading 0:00
15s threshold
Cover image for Abstraction in C++ - Explained in Simple Words

Aliza Kanwal

Abstraction means showing only the important things and hiding the complex details.

Think of a car. When you drive a car, you only see steering, brake, and accelerator. You don't see how the engine works inside. You don't need to know it. You just drive. This is Abstraction!

In C++, we use Abstraction to make our code simple to use.

Let's see a simple example:


cpp
#include <iostream>
using namespace std;

class Car {
public:
    void start() {
        cout << "Car Started!" << endl;
        startEngine();
    }

private:
    void startEngine() {
        cout << "Engine is working inside..." << endl;
    }
};

int main() {
    Car myCar;
    myCar.start();
    return 0;
}
Output:
Car Started!
Engine is working inside...

Enter fullscreen mode Exit fullscreen mode