C++ के Pure Virtual Functions
In this page:
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?
// 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
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Partial Implementation
अगर एक hierarchy में एक intermediate class एक abstract class से inherit करे लेकिन इसका pure virtual function override न करे, वह intermediate class भी abstract रहती है — सिर्फ chain में आगे की एक class जो आखिरकार implementation provide करती है concrete और instantiable बनती है।
उदाहरण: Partial Implementation
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Pure Virtual Destructors
आप एक destructor को pure virtual declare कर सकते हैं, जो class को abstract रहने पर force करता है, लेकिन आपको अभी भी इसके लिए एक body देनी होगी, क्योंकि inheritance chain में हर destructor — इस समेत — आखिरकार एक derived object destroy होने पर call होगा।
उदाहरण: Pure Virtual Destructors
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक pure virtual function वाली class का एक object बनाने की कोशिश करना, जो एक compile error है।
- derived class में हर pure virtual function implement न करना, जो इसे भी abstract छोड़ देता है।
virtual void f() {} = 0;लिखना, जब pure specifiervirtual void f() = 0;है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: