C++ में Single Inheritance
In this page:
class Base {
public:
// shared members
};
class Derived : public Base {
// uses Base members
};
Basic Single Inheritance
Single inheritance C++ में सबसे simple inheritance relationship है: exactly एक derived class exactly एक base class से inherit करती है, एक ज़्यादा complex hierarchy की बजाय एक straightforward parent-child pair बनाते हुए।
उदाहरण: Basic Single Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
void drive() { std::cout << "Driving" << std::endl; }
};
// Define the type Car
class Car : public Vehicle {};
// Program execution starts in main()
int main() {
Car c;
c.drive();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base Class Members Access करना
एक single derived class किसी भी base class member को freely पढ़ और लिख सकती है जो public या protected declared था, क्योंकि inheritance वह accessibility child में carry करता है — private base members यहाँ भी off-limits रहते हैं।
उदाहरण: Accessing Base Class Members
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
int wheels = 4;
};
// Define the type Car
class Car : public Vehicle {};
// Program execution starts in main()
int main() {
Car c;
// Print to the console with cout
std::cout << c.wheels << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Constructor Order of Execution
Base class constructors object के inherited हिस्से को establish करने के लिए हमेशा पहले execute होते हैं, और सिर्फ उसके बाद derived class का अपना constructor इसने खुद introduce किए properties की initialization खत्म करने के लिए चलता है।
उदाहरण: Constructor Order of Execution
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
Vehicle() { std::cout << "Vehicle first" << std::endl; }
};
// Define the type Car
class Car : public Vehicle {
// Members below can be accessed from outside the class
public:
Car() { std::cout << "Car second" << std::endl; }
};
// Program execution starts in main()
int main() {
Car c;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Private Inheritance
Private inheritance base class के हर public और protected member को derived class के अंदर एक private member में convert करती है, मतलब बाहरी code एक derived object के जरिए उन inherited members तक हर access खो देता है — भले ही derived class खुद उन्हें internally अभी भी इस्तेमाल कर सकती है।
उदाहरण: Private Inheritance
#include <iostream>
class Vehicle {
public:
int wheels = 4;
};
class Car : private Vehicle {
public:
void showWheels() { std::cout << wheels << std::endl; }
};
int main() {
Car c;
// c.wheels would fail to compile: private inheritance
c.showWheels();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Protected Inheritance
Protected inheritance एक middle ground है: यह सारे inherited public और protected base members को derived class में protected members में convert करती है, उन्हें main() या बाहरी code से hidden रखते हुए फिर भी derived class की किसी भी आगे की subclass को उन्हें access करने देते हुए।
उदाहरण: Protected Inheritance
// Include std::cout and std::cin
#include <iostream>
// Define the type Vehicle
class Vehicle {
// Members below can be accessed from outside the class
public:
int wheels = 4;
};
// Define the type Car
class Car : protected Vehicle {
// Members below can be accessed from outside the class
public:
void showWheels() { std::cout << wheels << std::endl; }
};
// Program execution starts in main()
int main() {
Car c;
c.showWheels();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- derived class में सीधे base class के private members access करने की कोशिश करना।
- यह उम्मीद करना कि derived constructor पहले चलेगा, जब base constructor पहले चलता है।
- inheritance list में
publicभूल जाना, जो derived class में base members को private बना देता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: