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

C++ के Pure Virtual Functions

एक pure virtual function बिना body का एक promise है जिसे child classes को भरना ज़रूरी है, एक job listing जिसे अभी तक किसी ने नहीं किया। एक ऐसा member रखने वाली class खुद नहीं बनाई जा सकती।
Syntax
cpp
class Base {
public:
  virtual return_type method_name() = 0;  // pure virtual
};

class Derived : public Base {
public:
  return_type method_name() override { }
};

Pure Virtual Function क्या है?

एक pure virtual function base class में बिल्कुल कोई default implementation के साथ declared होता है, 'virtual void functionName() = 0;' syntax इस्तेमाल करते हुए।

यह एक contract की तरह act करता है, हर concrete subclass को instantiate होने से पहले अपना implementation देना force करते हुए।

उदाहरण: What is a Pure Virtual Function?

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

Pure Virtual Functions Implement करना

किसी भी concrete (non-abstract) child class को इसके inherit किए हर pure virtual function को implement करना ज़रूरी है; अगर यह उनमें से एक भी override करने में fail हो, वह child class खुद भी abstract बन जाती है और directly instantiated होने के against same restriction inherit करती है।

उदाहरण: Implementing Pure Virtual Functions

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 Square
class Square : public Shape {
// Members below can be accessed from outside the class
public:
	void draw() override { std::cout << "Square" << std::endl; }
};

// Program execution starts in main()
int main() {
	Square s;
	s.draw();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Partial Implementation

अगर एक hierarchy में एक intermediate class एक abstract class से inherit करे लेकिन इसका pure virtual function override न करे, वह intermediate class भी abstract रहती है — सिर्फ chain में आगे की एक class जो आखिरकार implementation provide करती है concrete और instantiable बनती है।

उदाहरण: Partial Implementation

cpp
#include <iostream>

class Shape {
public:
	virtual void draw() = 0;
	virtual void resize() = 0;
};

class PartialShape : public Shape {
public:
	void draw() override { std::cout << "Drawing" << std::endl; }
	// resize() not overridden: PartialShape is still abstract
};

class FullShape : public PartialShape {
public:
	void resize() override { std::cout << "Resizing" << std::endl; }
};

int main() {
	FullShape s;
	s.draw();
	s.resize();
	return 0;
}

Body के साथ Pure Virtual

कुछ surprisingly, C++ में एक pure virtual function का एक function body होने की permission है, भले ही class को concrete बनाने के लिए इसे अभी भी override करना ज़रूरी हो।

वह body class definition के बाहर define होनी चाहिए, और derived classes optionally इसे shared default logic की तरह explicitly call कर सकती हैं।

उदाहरण: Pure Virtual with Body

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

void Shape::draw() {
	// Print to the console with cout
	std::cout << "Default shape drawing logic" << std::endl;
}

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	void draw() override {
		Shape::draw();
		// Print to the console with cout
		std::cout << "Circle" << std::endl;
	}
};

// Program execution starts in main()
int main() {
	Circle c;
	c.draw();
	return 0;
}

Pure Virtual Destructors

आप एक destructor को pure virtual declare कर सकते हैं, जो class को abstract रहने पर force करता है, लेकिन आपको अभी भी इसके लिए एक body देनी होगी, क्योंकि inheritance chain में हर destructor — इस समेत — आखिरकार एक derived object destroy होने पर call होगा।

उदाहरण: Pure Virtual Destructors

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 ~Shape() = 0;
};

Shape::~Shape() {
	// Print to the console with cout
	std::cout << "Shape destructor body" << std::endl;
}

// Define the type Circle
class Circle : public Shape {
// Members below can be accessed from outside the class
public:
	~Circle() { std::cout << "Circle destructor" << std::endl; }
};

// Program execution starts in main()
int main() {
	// Allocate an object on the heap with new
	Shape *s = new Circle();
	delete s;
	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. एक pure virtual function वाली class का एक object बनाने की कोशिश करना, जो एक compile error है।
  2. derived class में हर pure virtual function implement न करना, जो इसे भी abstract छोड़ देता है।
  3. virtual void f() {} = 0; लिखना, जब pure specifier virtual void f() = 0; है।

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.