Concept of Abstraction in Object-Oriented Programming
In object-oriented design, we often encounter concepts that are too general to have concrete instances. For example, a geometric shape is a abstract concept - you cannot calculate the area of a "shape" without knowing weather it's a circle, rectangle, or triangle. This is where abstract classes come into play.
Implementing Abstract Classes in C++
C++ doesn't have a dedicated keyword for abstract classes like Java or C#. Instead, it uses pure virtual functions to achieve abstraction. A class becomes abstract when it declares at least one pure virtual function.
Key characteristics:
- Abstract classes can only be used as base classes for inheritance
- They cannot be instantiated directly
- Derived classes must override all pure virtual functions
- If a derived class doesn't implement all pure virtual functions, it remains abstract
Code Example: Abstract Base Class for Geometric Figures
#include <iostream>
#include <cmath>
using namespace std;
// Abstract base class
class Geometry {
public:
virtual double calculateArea() = 0; // Pure virtual function
virtual double calculatePerimeter() = 0; // Another pure virtual function
};
class Rectangle : public Geometry {
private:
double m_width;
double m_height;
public:
Rectangle(double w, double h) : m_width(w), m_height(h) {}
double calculateArea() override {
return m_width * m_height;
}
double calculatePerimeter() override {
return 2 * (m_width + m_height);
}
};
class Circular : public Geometry {
private:
double m_radius;
public:
Circular(double r) : m_radius(r) {}
double calculateArea() override {
return M_PI * m_radius * m_radius;
}
double calculatePerimeter() override {
return 2 * M_PI * m_radius;
}
};
void displayProperties(Geometry* shape) {
cout << "Area: " << shape->calculateArea() << endl;
cout << "Perimeter: " << shape->calculatePerimeter() << endl;
}
int main() {
Rectangle rect(6.0, 8.0);
Circular circle(5.0);
// Geometry g; // Error: Cannot instantiate abstract class
displayProperties(&rect);
displayProperties(&circle);
return 0;
}
Interfaces in C++
In C++, an interface is a special form of abstract class with these characteristics:
- Contains no member variables (data members)
- All member functions are public and pure virtual
- Serves as a contract for derived classes
- Enables multiple inheritance of behavior
Code Example: Defining an Interface
#include <iostream>
#include <string>
using namespace std;
// Interface definition
class DataStream {
public:
virtual bool initialize() = 0;
virtual void shutdown() = 0;
virtual bool transmit(const char* data, int length) = 0;
virtual int retrieve(char* buffer, int maxLength) = 0;
virtual ~DataStream() = default; // Virtual destructor
};
// Implementation of the interface
class NetworkStream : public DataStream {
private:
bool m_isConnected;
public:
NetworkStream() : m_isConnected(false) {}
bool initialize() override {
m_isConnected = true;
cout << "Network connection established" << endl;
return true;
}
void shutdown() override {
m_isConnected = false;
cout << "Network connection closed" << endl;
}
bool transmit(const char* data, int length) override {
if (!m_isConnected) return false;
cout << "Transmitting " << length << " bytes" << endl;
return true;
}
int retrieve(char* buffer, int maxLength) override {
if (!m_isConnected) return 0;
cout << "Receiving data (max " << maxLength << " bytes)" << endl;
return 0; // Simplified example
}
};
int main() {
DataStream* stream = new NetworkStream();
stream->initialize();
stream->transmit("Hello", 5);
stream->shutdown();
delete stream;
return 0;
}
Key Takeaways
- Abstract classes model conceptual entities that cannot exist independently
- Pure virtual functions make a class abstract in C++
- Abstract classes must be inherited and they pure virtual functions overridden
- Interfaces are specialized abstract classes with only public pure virtual functions
- C++ achieves polymorphism through abstract base classes and virtual functions