Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
🟣 OOP  ·  Lesson 45

Inheritance in C++

Written and reviewed by · 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

C++
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

C++
int main() {
    Dog d;
    d.eat();    // inherited from Animal
    d.bark();   // Dog's own
    return 0;
}
Output:
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:

ModePublic base members become
publicpublic (models is-a) — most common
protectedprotected (rare)
privateprivate (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:

C++
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:

C++
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 private members 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.
🏋️ Practice

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++?
Inheritance lets a new class, called the derived class, reuse the members of an existing class, called the base class, and add or change behaviour. It models an "is-a" relationship — a Dog is an Animal — and avoids repeating shared code across related classes.
What is the difference between a base class and a derived class?
A base class is the existing class whose members are inherited, and a derived class is the new class that inherits from it. The derived class gets the base's public and protected members and can add its own or override inherited functions.
What are the access modes of inheritance in C++?
Inheritance can be 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++?
The base class constructor runs first, then the derived class constructor. Destructors run in the reverse order — derived first, then base — so each class sets up before its additions and cleans up after them.
Can a derived class access private members of the base class in C++?
No. A derived class cannot directly access the base class's 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

C++
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 करता है।

एक चलता उदाहरण

C++
int main() {
    Dog d;
    d.eat();    // Animal se inherited
    d.bark();   // Dog ka apna
    return 0;
}
Output:
Eating
Woof

Dog object अपने bark() और inherited eat() दोनों इस्तेमाल कर सकता है।

Access और inheritance modes

Derived class base के public और protected members देखती है, पर उसके private नहीं। Inheritance mode भी मायने रखता है:

ModePublic base members बनते हैं
publicpublic (is-a मॉडल) — सबसे आम
protectedprotected (दुर्लभ)
privateprivate (दुर्लभ)

Base में उस data के लिए protected इस्तेमाल करें जिस तक subclasses पहुँचें। access specifiers देखें।

Constructor क्रम

जब derived object बनता है, base constructor पहले चलता है, फिर derived। Destructors इसे उल्टा करते हैं। आप derived से base constructor को arguments भेज सकते हैं:

C++
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 को फिर से परिभाषित करके उसका व्यवहार बदल सकती है:

C++
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 के private members 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 कर सकती हैं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.