← Back to C++ Course | Chapter 10: Polymorphism & Abstraction | Lesson 5 of 11

C++ की Abstract Classes

एक abstract class एक unfinished blueprint है जिसे extend करने का इरादा है, दूसरों को पूरा करना है ऐसे एक sketch की तरह। आप इसे सीधे नहीं बना सकते।
Syntax
cpp
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?

cpp
#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;
}

एक 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

cpp
// 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;
}

Abstract Classes में Constructors

भले ही आप एक abstract class को सीधे instantiate नहीं कर सकते, इसमें अभी भी constructors होने की permission है — ये automatically किसी भी concrete derived class construct होने के हिस्से के तौर पर चलते हैं, abstract class जो भी base-level state define करती है उसे setup करते हुए।

उदाहरण: Constructors in Abstract Classes

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक abstract class को सीधे instantiate करने की कोशिश करना, जो एक compile error है।
  2. सारे pure virtual functions implement न करना, ताकि derived class abstract रहे।
  3. pointers या smart pointers इस्तेमाल करने की बजाय एक vector में abstract objects को value से store करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.