Abstract Class in OOP

Understanding abstract classes and their role in C++ programming

What is an Abstract Class?

An abstract class is a class that contains at least one pure virtual function. It cannot be instantiated directly and serves as a base class for other classes. It provides a common interface and implementation that derived classes can inherit and extend.

Abstract Class क्या है? (हिंदी में)

Abstract class एक ऐसा class है जिसमें कम से कम एक pure virtual function होता है। इसे directly instantiate नहीं किया जा सकता और यह अन्य classes के लिए base class के रूप में serve करता है। यह एक common interface और implementation provide करता है जिसे derived classes inherit और extend कर सकती हैं।

Key Points:

  • Pure Virtual: Contains at least one pure virtual function
  • No Instantiation: Cannot create objects directly
  • Base Class: Serves as a base for other classes
  • Interface: Defines common interface for derived classes

Types of Abstract Classes

Pure Abstract Class

All functions are pure virtual

Partial Abstract Class

Mix of virtual and non-virtual functions

Interface Class

Only pure virtual functions

Abstract Class Rules

Rule Description
Pure Virtual Must have at least one pure virtual function
Instantiation Cannot create objects directly
Implementation Can have implemented functions
Derived Classes Must implement all pure virtual functions

Simple Example

#include <iostream>
#include <string>
#include <vector>
#include <memory>

// Abstract base class
class Animal {
protected:
    std::string name;
    int age;

public:
    Animal(const std::string& n, int a) : name(n), age(a) {}
    
    // Pure virtual function
    virtual void makeSound() = 0;
    
    // Virtual function with implementation
    virtual void display() const {
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;
    }
    
    // Virtual destructor
    virtual ~Animal() {
        std::cout << "Destroying " << name << std::endl;
    }
};

// Derived class
class Dog : public Animal {
private:
    std::string breed;

public:
    Dog(const std::string& n, int a, const std::string& b)
        : Animal(n, a), breed(b) {}
    
    // Implementation of pure virtual function
    void makeSound() override {
        std::cout << name << " says: Woof!" << std::endl;
    }
    
    // Override display function
    void display() const override {
        Animal::display();
        std::cout << "Breed: " << breed << std::endl;
    }
};

// Another derived class
class Cat : public Animal {
private:
    std::string color;

public:
    Cat(const std::string& n, int a, const std::string& c)
        : Animal(n, a), color(c) {}
    
    // Implementation of pure virtual function
    void makeSound() override {
        std::cout << name << " says: Meow!" << std::endl;
    }
    
    // Override display function
    void display() const override {
        Animal::display();
        std::cout << "Color: " << color << std::endl;
    }
};

int main() {
    // Using smart pointers for better memory management
    std::vector> animals;
    
    // Creating animals
    animals.push_back(std::make_unique("Buddy", 3, "Golden Retriever"));
    animals.push_back(std::make_unique("Whiskers", 2, "Orange"));
    
    // Using virtual functions
    for (const auto& animal : animals) {
        animal->display();
        animal->makeSound();
        std::cout << std::endl;
    }
    
    return 0;
}

उदाहरण की व्याख्या (Example Explanation)

इस उदाहरण में abstract class का उपयोग किया गया है:

  • Abstract Base Class: Animal class एक abstract class है जिसमें pure virtual function makeSound() है
  • Derived Classes: Dog और Cat classes Animal class को inherit करती हैं
  • Pure Virtual Function: makeSound() function derived classes में implement किया गया है
  • Virtual Function: display() function base class में implement किया गया है

Benefits of Abstract Classes

Abstract Classes के फायदे

  1. Interface Definition
    • Common interface define करने में help करता है
    • Code consistency maintain करता है
  2. Code Reusability
    • Common functionality को share करने में help करता है
    • Code duplication को reduce करता है
  3. Polymorphism
    • Runtime polymorphism को enable करता है
    • Flexible और extensible code बनाता है

Best Practices

  • Use abstract classes to define common interfaces
  • Keep abstract classes focused and cohesive
  • Provide default implementations when appropriate
  • Use pure virtual functions for required behavior
  • Consider using interfaces for pure abstract classes