/*
Abstraction:
Definition: As discussed earlier, abstraction is the concept of hiding unnecessary details and exposing only the
relevant information or functionality to the user.
Goal: To simplify complexity by focusing on essential features and leaving out unnecessary implementation details.
How it works: Achieved using abstract classes and interfaces (pure virtual functions in C++) to expose only the necessary details.
*/
#include <iostream>
// The 'car' class demonstrates both encapsulation and abstraction
class car {
private:
// Private members (encapsulation): These variables cannot be accessed directly from outside the class
int fuel;
int speed;
public:
// Constructor to initialize speed and fuel
car(int sp, int fl);
// Public methods (abstraction): These provide a simplified interface to interact with the car
int getspeed(); // Getter for speed (abstraction)
int getfuel(); // Getter for fuel (abstraction)
void drive(int a); // Method to drive the car and modify speed and fuel (abstraction)
};
// Constructor definition
// Initializes speed and fuel with the values provided
car::car(int sp, int fl) : speed(sp), fuel(fl) {}
// Method to get the current speed (public access to the private speed variable)
// This abstracts the details of how speed is stored internally
int car::getspeed() {
return speed;
}
// Method to get the current fuel level (public access to the private fuel variable)
// This abstracts the details of how fuel is stored internally
int car::getfuel() {
return fuel;
}
// Method to drive the car by increasing both speed and fuel
// This abstracts the complex logic of how driving affects the car's speed and fuel
// The user doesn't need to know how these variables are updated internally
void car::drive(int a) {
speed = speed + a; // Modify the speed
fuel = fuel + a; // Modify the fuel
}
int main() {
// Create a car object with initial speed 5 and fuel level 10
car b1(5, 10);
// Call the 'drive' method to simulate driving the car by 50 units
// The user does not need to know the internal workings of how speed and fuel are affected
b1.drive(50);
// Output the car's current speed and fuel level using the getter methods
// This abstracts away the complexity of retrieving these values
std::cout << b1.getspeed() << std::endl; // Output the current speed
std::cout << b1.getfuel() << std::endl; // Output the current fuel level
}
/*
In this case, Animal is an abstract class that exposes the essential feature (makeSound()), but the implementation details are hidden.
The actual behavior is provided by derived classes like Dog.
Key Differences:
Encapsulation is the process of bundling data and methods that operate on the data into a single unit, typically a class,
and restricting access to the object's internal state to protect it. In encapsulation, access modifiers like private, protected,
and public are used to control access to the object's attributes and methods, ensuring that data is only modified through controlled
methods such as getters and setters.
Abstraction, on the other hand, focuses on hiding the complexity and showing only the essential features of an object.
Abstraction is achieved using abstract classes or interfaces, where the internal workings are hidden, and only the necessary
functionality is exposed to the user. The user interacts with the abstract interface without needing to know the details of how it works.
In short, encapsulation is about protecting data and controlling access to it, while abstraction is about simplifying complex
systems by exposing only relevant details.
*/
To embed this project on your website, copy the following code and paste it into your website's HTML: