Inheritance ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग सिस्टम की एक विशेषता है, C++ में inheritance को कोड की reusability के लिए उपयोग किया जाता है inheritance आपको मौजूदा क्लास से नए classes बनाने की सुविधा देता है किसी मौजूदा क्लास से बनाई गए new class को derived class कहा जाता है और मौजूदा क्लास को बेस क्लास कहा जाता है।
ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग में सबसे महत्वपूर्ण अवधारणाओं में से inheritance एक है inheritance हमें किसी अन्य वर्ग के मामले में एक वर्ग को परिभाषित करने की अनुमति देता है, जो एक आवेदन को बनाने और बनाए रखने में आसान बनाता है यह कोड की कार्यक्षमता और तेजी से Implementation और समय का पुन: उपयोग करने का एक अवसर भी प्रदान करता है।
inheritance ऑब्जेक्ट ओरिएंटेड प्रोग्रामिंग सिस्टम की एक विशेषता है, जिसके द्वारा एक कक्षा अन्य कक्षा में इस्तेमाल किये जाने वाली सुविधाओं और गुणों को प्राप्त कर सकती है इस lesson में आप inheritance का उपयोग करके C++ कार्यक्रमों को हल कैसे करते है वो सीखोगे।
Single Inheritance − इस inheritance अनुक्रम में एक derived कक्षा एक आधार कक्षा से प्राप्त होती है।
Multiple Inheritance − इस inheritance अनुक्रम में एक derived कक्षा बहु आधार कक्षा से प्राप्त होती है
Hierarchical Inheritance − इस inheritance अनुक्रम में कई sub-class एक आधार class से प्राप्त होती हैं।
#include <iostream> using namespace std; // Base class class Shape { public: void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Derived class class Rectangle: public Shape { public: int getArea() { return (width * height); } }; int main(void) { Rectangle Rect; Rect.setWidth(15); Rect.setHeight(10); // Print the area of the object. cout << "Total area: " << Rect.getArea() << endl; return 0; }