C++ की Abstract Classes
In this page:
class AbstractClass {
public:
virtual return_type method_name() = 0; // makes the class abstract
};
Abstract Class क्या है?
एक abstract class कोई भी class है जिसमें कम से कम एक pure virtual function हो।
आप कभी एक abstract class का एक direct object नहीं बना सकते, लेकिन आप अभी भी इसके pointers और references declare कर सकते हैं, जो exactly है कि polymorphic code abstract types के साथ कैसे interact करता है।
उदाहरण: What is an Abstract Class?
#include <iostream>
class Shape {
public:
virtual void draw() = 0;
};
int main() {
// Shape s; would fail to compile: Shape is abstract
std::cout << "Cannot instantiate Shape directly" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Interface की तरह Abstract Class
Abstract classes interfaces की तरह function करती हैं: वे specify करती हैं कि एक derived class को कौन से operations support करने ज़रूरी हैं यह dictate किए बिना कि वे operations कैसे implement होते हैं, हर concrete subclass को एक shared, predictable API के पीछे अपना behavior provide करने देते हुए।
उदाहरण: Abstract Class as an Interface
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
c.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstract Classes में Constructors
भले ही आप एक abstract class को सीधे instantiate नहीं कर सकते, इसमें अभी भी constructors होने की permission है — ये automatically किसी भी concrete derived class construct होने के हिस्से के तौर पर चलते हैं, abstract class जो भी base-level state define करती है उसे setup करते हुए।
उदाहरण: Constructors in Abstract Classes
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
Shape() { std::cout << "Shape constructor runs" << std::endl; }
virtual void draw() = 0;
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
c.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstract Class Arrays/Vectors
आप abstract base class pointers के arrays या containers declare कर सकते हैं, जो अलग concrete subtypes के एक mixed collection को store करने और उन सबको उनके shared abstract interface के जरिए uniformly process करने के लिए एक powerful pattern है।
उदाहरण: Abstract Class Arrays/Vectors
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
class Square : public Shape {
public:
void draw() override { std::cout << "Square" << std::endl; }
};
// Program execution starts in main()
int main() {
// Create a vector named shapes (a resizable array)
std::vector<Shape *> shapes = {new Circle(), new Square()};
for (Shape *s : shapes) s->draw();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Concrete Classes बनाम Abstract Classes
Concrete classes हर inherited pure virtual method implement करती हैं और freely instantiated हो सकती हैं, जबकि abstract classes जानबूझकर उस implementation के कुछ या सारे हिस्से को defer करती हैं और purely अपने descendants के भरने के लिए एक shared outline define करने के लिए exist करती हैं।
उदाहरण: Concrete Classes vs Abstract Classes
// Include std::cout and std::cin
#include <iostream>
// Define the type Shape
class Shape {
// Members below can be accessed from outside the class
public:
virtual void draw() = 0;
};
// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Circle" << std::endl; }
};
// Program execution starts in main()
int main() {
Circle c;
c.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- एक abstract class को सीधे instantiate करने की कोशिश करना, जो एक compile error है।
- सारे pure virtual functions implement न करना, ताकि derived class abstract रहे।
- pointers या smart pointers इस्तेमाल करने की बजाय एक vector में abstract objects को value से store करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: