Abstraction

Abstraction is a fundamental concept in programming that involves simplifying complex systems by hiding unnecessary details while exposing essential features. In C++, abstraction is achieved through classes and objects.

Why Abstraction is Important?

Abstraction helps manage complexity, improves code readability, promotes code reuse, and enhances maintainability. It allows programmers to focus on high-level design rather than low-level implementation details.

Basics of Abstraction

Classes and Objects

In C++, classes are user-defined data types that encapsulate data and functions into a single entity. Objects are instances of classes that represent real-world entities.

				
					#include <iostream>

class Car {
private:
    std::string brand;
    std::string model;
public:
    void setBrand(std::string b) {
        brand = b;
    }
    void setModel(std::string m) {
        model = m;
    }
    void displayInfo() {
        std::cout << "Brand: " << brand << ", Model: " << model << std::endl;
    }
};

int main() {
    Car myCar;
    myCar.setBrand("Toyota");
    myCar.setModel("Camry");
    myCar.displayInfo();
    return 0;
}

				
			
				
					// output //
Brand: Toyota, Model: Camry

				
			

Data Abstraction

Access Specifiers

Access specifiers (public, private, protected) control the visibility of class members. private members are accessible only within the class, public members are accessible from outside the class, and protected members are accessible within the class and its derived classes.

				
					#include <iostream>

class Employee {
private:
    int salary;
public:
    void setSalary(int s) {
        salary = s;
    }
    int getSalary() {
        return salary;
    }
};

int main() {
    Employee emp;
    emp.setSalary(50000);
    std::cout << "Salary: " << emp.getSalary() << std::endl;
    // emp.salary = 60000; // Error: 'salary' is private within this context
    return 0;
}

				
			
				
					// output //
Salary: 50000

				
			

Abstraction with Constructors and Destructors

Constructors

Constructors are special member functions that are automatically called when an object is created. They initialize the object’s state.

				
					#include <iostream>

class Rectangle {
private:
    int length;
    int width;
public:
    Rectangle(int l, int w) : length(l), width(w) {}
    int area() {
        return length * width;
    }
};

int main() {
    Rectangle rect(5, 3);
    std::cout << "Area: " << rect.area() << std::endl;
    return 0;
}

				
			
				
					// output //
Area: 15

				
			

Destructors

Destructors are special member functions that are automatically called when an object is destroyed. They release resources allocated by the object.

				
					#include <iostream>

class Resource {
public:
    Resource() {
        std::cout << "Resource acquired" << std::endl;
    }
    ~Resource() {
        std::cout << "Resource released" << std::endl;
    }
};

int main() {
    Resource res;
    std::cout << "Inside main function" << std::endl;
    return 0;
}

				
			
				
					// output //
Resource acquired
Inside main function
Resource released

				
			

Advanced Abstraction Techniques

Abstract Classes and Interfaces

Abstract classes are classes that cannot be instantiated and may contain one or more pure virtual functions. They serve as base classes for other classes.

				
					#include <iostream>

class Shape {
public:
    virtual void draw() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
    void draw() override {
        std::cout << "Drawing a circle" << std::endl;
    }
};

int main() {
    // Shape shape; // Error: Cannot instantiate abstract class
    Circle circle;
    circle.draw();
    return 0;
}

				
			
				
					// output //
Drawing a circle

				
			

Abstraction is a cornerstone of object-oriented programming in C++. By encapsulating implementation details and exposing only essential features, abstraction allows for easier maintenance, code reuse, and scalability. From basic concepts like classes and objects to advanced techniques like abstract classes and interfaces, mastering abstraction is crucial for writing clean, modular, and efficient code in C++.Happy coding !❤️

Table of Contents