C++ में Abstraction
In this page:
class AbstractClass {
public:
virtual return_type method_name() = 0;
};
class Concrete : public AbstractClass {
public:
return_type method_name() override { }
};
Abstraction क्या है?
Abstraction एक object की complex internal implementation hide करने का process है जबकि सिर्फ essential features expose करना जो एक user को actually इसके साथ interact करने के लिए चाहिए।
यह programmers को उन low-level mechanics से distract हुए बिना high-level concepts के साथ काम करने देता है जो उन्हें जानने की ज़रूरत नहीं।
उदाहरण: What is Abstraction?
// Include std::cout and std::cin
#include <iostream>
// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
void start() {
// Print to the console with cout
std::cout << "Car started" << std::endl;
}
};
// Program execution starts in main()
int main() {
Car car;
car.start();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstract Classes
एक abstract class specifically दूसरी classes के लिए एक blueprint की तरह serve करने के लिए designed class है: इसमें कम से कम एक pure virtual function होता है और, एक direct consequence की तरह, इसे कभी अपने आप instantiate नहीं किया जा सकता।
उदाहरण: 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
Pure Virtual Functions
एक pure virtual function base class में अपने कोई implementation के साथ declared होता है — इसकी body को declaration को 0 के बराबर set करके पूरी तरह replace किया जाता है — उस specific behavior को पूरी तरह उस subclass पर छोड़ते हुए जो आखिरकार इसे implement करे।
उदाहरण: 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
Abstraction का Implementation
Abstraction actually practice में लाने के लिए, एक abstract parent से derive करने वाली हर concrete class को इसके inherit किए हर pure virtual function के लिए override करना और एक real implementation देना ज़रूरी है, वरना यह खुद abstract रहती है और इसे भी instantiate नहीं किया जा सकता।
उदाहरण: Implementation of Abstraction
// 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 Triangle
class Triangle : public Shape {
// Members below can be accessed from outside the class
public:
void draw() override { std::cout << "Triangle" << std::endl; }
};
// Program execution starts in main()
int main() {
Triangle t;
t.draw();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Abstraction के Advantages
Abstraction code duplication कम करके और एक system के design को इसके implementation details से cleanly अलग करके payoff देता है — एक benefit जो बड़े, evolving codebases को copy-pasted logic का एक tangle बनने की बजाय scalable रखता है।
उदाहरण: Advantages of Abstraction
// Include std::cout and std::cin
#include <iostream>
// Define the type PaymentMethod
class PaymentMethod {
// Members below can be accessed from outside the class
public:
virtual void pay() = 0;
};
// Define the type Cash
class Cash : public PaymentMethod {
// Members below can be accessed from outside the class
public:
void pay() override { std::cout << "Paid with cash" << std::endl; }
};
// Program execution starts in main()
int main() {
Cash cash;
cash.pay();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Abstraction (एक चीज़ क्या करती है यह hide करना) को encapsulation (data और methods bundle करना) से confuse करना।
- public interface में implementation details expose करना।
- एक concrete derived class में हर pure virtual function implement न करना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: