C++ Inheritance का परिचय
In this page:
class Base {
// members
};
class Derived : public Base {
// extra members
};
Inheritance क्या है?
Inheritance एक नई class को, derived class कहलाती है, एक existing class की properties और methods automatically acquire करने देता है, base class कहलाती है।
यह related classes को हर एक अलग से reimplement करने की बजाय common behavior share करने देकर code reuse promote करता है।
उदाहरण: What is Inheritance?
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
void eat() { std::cout << "Eating" << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
void bark() { std::cout << "Barking" << std::endl; }
};
// Program execution starts in main()
int main() {
Dog d;
d.eat();
d.bark();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base और Derived Class Constructors
जब आप एक derived class का एक object बनाते हैं, base class का constructor object के inherited हिस्से को setup करने के लिए हमेशा पहले चलता है, और वह खत्म होने के बाद ही derived class का अपना constructor इसने add किए parts को initialize करने के लिए चलता है।
उदाहरण: Base and Derived Class Constructors
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
Animal() { std::cout << "Animal constructed" << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
Dog() { std::cout << "Dog constructed" << std::endl; }
};
// Program execution starts in main()
int main() {
Dog d;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Public Inheritance Access
Public inheritance के साथ, जो भी member base class में public था वह derived class में public रहता है, मतलब external code इसे एक derived object के जरिए exactly उतनी ही freely access कर सकता है जितना एक base object के जरिए कर सकता था।
उदाहरण: Public Inheritance Access
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
int legs = 4;
};
// Define the type Dog
class Dog : public Animal {};
// Program execution starts in main()
int main() {
Dog d;
// Print to the console with cout
std::cout << d.legs << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Protected Access Specifier
Protected members को class hierarchy के बाहर से कभी access नहीं किया जा सकता, लेकिन private members के उलट, वे उस class से derive करने वाली किसी भी class के अंदर freely accessible रहते हैं — एक subclass को legitimately जिस internal state पर build करना चाहिए उसके लिए useful।
उदाहरण: Protected Access Specifier
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
protected:
int age = 2;
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
void showAge() { std::cout << age << std::endl; }
};
// Program execution starts in main()
int main() {
Dog d;
d.showAge();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base Class Methods Override करना
Method overriding एक derived class को उस function का अपना specific implementation देने देता है जो base class पहले ही define करता है, inherited behavior को उस particular subclass के लिए tailored logic से replace करते हुए।
उदाहरण: Overriding Base Class Methods
// Include std::cout and std::cin
#include <iostream>
// Define the type Animal
class Animal {
// Members below can be accessed from outside the class
public:
void speak() { std::cout << "..." << std::endl; }
};
// Define the type Dog
class Dog : public Animal {
// Members below can be accessed from outside the class
public:
void speak() { std::cout << "Woof" << std::endl; }
};
// Program execution starts in main()
int main() {
Dog d;
d.speak();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- initializer list में arguments के साथ base class constructor call करना भूल जाना, ताकि base का default constructor required हो।
publicके बिनाclass B : Aलिखना और private inheritance पाना।- base pointer के जरिए derived objects delete करते समय base destructor को
virtualन बनाना।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: