0% found this document useful (0 votes)
11 views

Abstraction Using C++

Uploaded by

zarazarwa149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Abstraction Using C++

Uploaded by

zarazarwa149
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Abstraction using C++

Abstraction in C++ is a concept that involves hiding the complex implementation details of a
system and exposing only the necessary parts. It allows you to manage complexity by separating
the interface (what something does) from the implementation (how it does it).
Let's look at a very simple example to understand abstraction in C++.

Example: Abstraction with a `Shape` Class

1. Define an Abstract Base Class:


We define a base class `Shape` which has a pure virtual function `area()`. This makes `Shape`
an abstract class, meaning you cannot create objects of this class directly.

2. Derived Classes Implementing the Abstract Base Class:


We then define derived classes like `Rectangle` and `Circle` that inherit from `Shape` and
provide implementations for the `area()` function.

Here's the code to illustrate this:

#include <iostream>
#include <cmath> // For M_PI

// Abstract base class


class Shape {
public:
// Pure virtual function
virtual double area() const = 0; // = 0 means it is pure virtual
virtual ~Shape() = default; // Virtual destructor for proper cleanup
};
// Derived class: Rectangle
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h) {}

// Implementing the pure virtual function


double area() const override {
return width * height;
}
};

// Derived class: Circle


class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}

// Implementing the pure virtual function


double area() const override {
return M_PI * radius * radius;
}
};

// Function to print the area of any shape


void printArea(const Shape& shape) {
std::cout << "Area: " << shape.area() << std::endl;
}

int main() {
// Creating objects of Rectangle and Circle
Rectangle rect(5.0, 3.0);
Circle circ(4.0);

// Using the printArea function to demonstrate abstraction


printArea(rect);
printArea(circ);

return 0;
}

Explanation:

1. Abstract Base Class `Shape`:


```cpp
class Shape {
public:
virtual double area() const = 0;
virtual ~Shape() = default;
};
```
- `Shape` has a pure virtual function `area()`, making it an abstract class.
- A virtual destructor is provided to ensure proper cleanup of derived class objects.
2. Derived Class `Rectangle`:
```cpp
class Rectangle : public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) : width(w), height(h) {}

double area() const override {


return width * height;
}
};
```
- `Rectangle` inherits from `Shape` and implements the `area()` function.
- The `area()` function for `Rectangle` calculates the area as `width * height`.

3. Derived Class `Circle`:


```cpp
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}

double area() const override {


return M_PI * radius * radius;
}
};
```
- `Circle` also inherits from `Shape` and provides its own implementation of the `area()` function.
- The `area()` function for `Circle` calculates the area using the formula `πr²`.

4. Function to Print Area:


```cpp
void printArea(const Shape& shape) {
std::cout << "Area: " << shape.area() << std::endl;
}
```
- `printArea` takes a reference to a `Shape` object and prints its area.
- This function demonstrates how abstraction allows us to write code that works with any `Shape`
without needing to know the specific type.

5. Main Function:
```cpp
int main() {
Rectangle rect(5.0, 3.0);
Circle circ(4.0);

printArea(rect);
printArea(circ);

return 0;
}
```
- We create objects of `Rectangle` and `Circle` and pass them to the `printArea` function.
- The `printArea` function calls the appropriate `area()` method based on the actual object type,
demonstrating polymorphism.

Key Points:

- Abstraction: The user of the `Shape` class doesn't need to know the details of how the area is
calculated for different shapes.
- Polymorphism: The same function call to `area()` behaves differently based on the object type
(rectangle or circle).
- Encapsulation: The implementation details (like `width`, `height`, `radius`) are hidden inside
the derived classes.

This example shows how abstraction helps in managing complexity by focusing on what
operations an object can perform rather than how these operations are implemented.

P.S:
If you declare a function as virtual in a class but do not make it a pure virtual function (i.e., you
do not set it equal to zero), the class does not become abstract. The class can still be instantiated
because a virtual function only means that the function can be overridden in derived classes, not
that it must be.

You might also like