Inheritance in C++
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
What is inheritance?
Inheritance lets one class build on another. The existing class is the base; the new one is the derived class, which gains the base's members and can add or change behaviour. It captures an "is-a" relationship — a Dog is an Animal — so shared code lives in one place.
Base and derived classes
class Animal { // base
public:
void eat() { cout << "Eating\n"; }
};
class Dog : public Animal { // derived: Dog is-a Animal
public:
void bark() { cout << "Woof\n"; }
};The : public Animal part says Dog inherits publicly from Animal.
A working example
int main() {
Dog d;
d.eat(); // inherited from Animal
d.bark(); // Dog's own
return 0;
}Eating
Woof
A Dog object can use both its own bark() and the inherited eat().
Access and inheritance modes
A derived class sees the base's public and protected members, but not its private ones. The inheritance mode also matters:
| Mode | Public base members become |
|---|---|
public | public (models is-a) — most common |
protected | protected (rare) |
private | private (rare) |
Use protected in the base for data that subclasses should reach. See access specifiers.
Constructor order
When a derived object is created, the base constructor runs first, then the derived one. Destructors reverse this. You can pass arguments to the base constructor from the derived one:
class Animal {
public:
Animal(string n) { cout << n << " created\n"; }
};
class Dog : public Animal {
public:
Dog() : Animal("Dog") { } // call base constructor
};Overriding a function
A derived class can redefine an inherited function to change its behaviour:
class Animal { public: void sound() { cout << "Some sound\n"; } };
class Cat : public Animal {
public:
void sound() { cout << "Meow\n"; } // overrides
};For overriding that works through base-class pointers, you need virtual functions and polymorphism.
Common mistakes
- Trying to access the base's
privatemembers from the derived class. - Forgetting that the base constructor runs before the derived one.
- Using inheritance for a "has-a" relationship where composition fits better.
- Expecting an overridden function to be chosen through a base pointer without
virtual.
Create a Vehicle base with a start() function, and derive Car and Bike that each add their own function. Make objects and call both inherited and own functions.
Summary
- Inheritance lets a derived class reuse and extend a base class.
- It models an "is-a" relationship and avoids code duplication.
- Derived classes see public/protected base members, not private.
- Base constructor runs first; destructors run in reverse.
- Derived classes can override inherited functions.
Frequently Asked Questions
What is inheritance in C++?
What is the difference between a base class and a derived class?
What are the access modes of inheritance in C++?
public, protected or private, which controls how the base's members appear in the derived class. Public inheritance keeps public members public and models "is-a"; it is by far the most common, while protected and private inheritance are rarely used.In what order are constructors called with inheritance in C++?
Can a derived class access private members of the base class in C++?
private members; those stay hidden. It can access public and protected members of the base, so data meant for subclasses is usually declared protected.Inheritance क्या है?
Inheritance एक class को दूसरी पर बनाने देता है। मौजूदा class base है; नई derived class है, जो base के members पाती है और व्यवहार जोड़ या बदल सकती है। यह एक "is-a" संबंध पकड़ता है — Dog एक Animal है — तो साझा code एक जगह रहता है।
Base और derived classes
class Animal { // base
public:
void eat() { cout << "Eating\n"; }
};
class Dog : public Animal { // derived: Dog is-a Animal
public:
void bark() { cout << "Woof\n"; }
};: public Animal भाग कहता है Dog, Animal से publicly inherit करता है।
एक चलता उदाहरण
int main() {
Dog d;
d.eat(); // Animal se inherited
d.bark(); // Dog ka apna
return 0;
}Eating
Woof
Dog object अपने bark() और inherited eat() दोनों इस्तेमाल कर सकता है।
Access और inheritance modes
Derived class base के public और protected members देखती है, पर उसके private नहीं। Inheritance mode भी मायने रखता है:
| Mode | Public base members बनते हैं |
|---|---|
public | public (is-a मॉडल) — सबसे आम |
protected | protected (दुर्लभ) |
private | private (दुर्लभ) |
Base में उस data के लिए protected इस्तेमाल करें जिस तक subclasses पहुँचें। access specifiers देखें।
Constructor क्रम
जब derived object बनता है, base constructor पहले चलता है, फिर derived। Destructors इसे उल्टा करते हैं। आप derived से base constructor को arguments भेज सकते हैं:
class Animal {
public:
Animal(string n) { cout << n << " created\n"; }
};
class Dog : public Animal {
public:
Dog() : Animal("Dog") { } // base constructor call
};Function override करना
Derived class inherited function को फिर से परिभाषित करके उसका व्यवहार बदल सकती है:
class Animal { public: void sound() { cout << "Some sound\n"; } };
class Cat : public Animal {
public:
void sound() { cout << "Meow\n"; } // override karta hai
};Base-class pointers के ज़रिए काम करने वाले overriding के लिए, आपको virtual functions और polymorphism चाहिए।
आम गलतियाँ
- Derived class से base के
privatemembers access करने की कोशिश। - यह भूलना कि base constructor derived से पहले चलता है।
- "has-a" संबंध के लिए inheritance इस्तेमाल करना जहाँ composition बेहतर बैठता है।
virtualबिना base pointer के ज़रिए overridden function के चुने जाने की उम्मीद।
एक Vehicle base बनाएँ जिसमें start() function हो, और Car तथा Bike derive करें जो हर एक अपना function जोड़ें। Objects बनाएँ और inherited तथा अपने दोनों functions call करें।
सारांश
- Inheritance derived class को base class दोबारा इस्तेमाल और विस्तारित करने देता है।
- यह "is-a" संबंध मॉडल करता है और code दोहराव से बचाता है।
- Derived classes public/protected base members देखती हैं, private नहीं।
- Base constructor पहले चलता है; destructors उल्टे चलते हैं।
- Derived classes inherited functions override कर सकती हैं।