Introduction
Object Oriented Programming में कई बार ऐसी classes बनाई जाती हैं जिनका उद्देश्य केवल एक base structure प्रदान करना होता है, न कि सीधे objects बनाना। ऐसी classes का उपयोग common properties और behaviors को define करने के लिए किया जाता है, जिन्हें derived classes आगे implement करती हैं।
C++ में इस प्रकार की classes को Abstract Classes कहा जाता है। ये classes पूरी तरह से implement नहीं होतीं, बल्कि इनमें कुछ functions अधूरे (incomplete) होते हैं, जिन्हें derived class में पूरा करना आवश्यक होता है।
Definition
Abstract Class वह class होती है जिसमें कम से कम एक pure virtual function होता है और जिसका object सीधे create नहीं किया जा सकता।
Pure Virtual Function
Pure virtual function वह function होता है जिसकी declaration तो class में होती है, लेकिन उसकी definition नहीं दी जाती।
Syntax
virtual returnType functionName() = 0;
Abstract Class का Syntax
class ClassName {
public:
virtual void show() = 0; // pure virtual function
};
Example (Abstract Class)
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() = 0; // pure virtual function
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing Circle";
}
};
int main() {
// Shape s; // not allowed
Circle c1;
c1.draw();
return 0;
}
Output:
Drawing Circle
Working को समझना
Shapeएक abstract class है- इसमें
draw()pure virtual function है Circleclass नेdraw()को implement किया- Abstract class का object नहीं बनाया जा सकता
- Derived class का object बनाया गया
Abstract Class की विशेषताएँ
- इसमें कम से कम एक pure virtual function होता है
- इसका object create नहीं किया जा सकता
- Derived class को सभी pure virtual functions implement करने होते हैं
- यह base class के रूप में उपयोग होती है
Real Life Example
मान लीजिए “Shape” एक abstract concept है।
आप सीधे “Shape” नहीं बना सकते, लेकिन “Circle”, “Rectangle” जैसे specific shapes बना सकते हैं।
Important Points
= 0pure virtual function को दर्शाता है- Abstract class incomplete होती है
- Derived class में implementation देना जरूरी है
- Polymorphism में इसका उपयोग होता है
निष्कर्ष
Abstract classes C++ में एक powerful concept हैं जो base structure प्रदान करती हैं और derived classes को specific implementation देने के लिए बाध्य करती हैं। यह code reuse और abstraction को बेहतर बनाती हैं।