Inheritance in C++
# CHAPTER 17
Inheritance in C++
1. Introduction
If you are coding a game, aPlayer and an Enemy both need health, speed, and an x,y position. Instead of writing that same code twice, you can create a base class called Entity, and have both Player and Enemy inherit from it. Inheritance is the mechanism that allows one class to acquire the properties and methods of another.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define Base (Parent) and Derived (Child) classes.
-
Use the
protectedaccess modifier.
- Understand Single, Multilevel, and Multiple Inheritance.
- See how Constructors behave during inheritance.
3. Syntax of Inheritance
Syntax:class ChildClass : access_modifier ParentClass { };
4. The protected Access Modifier
We know private hides data completely, and public exposes it completely. What if you want to hide data from main(), but still allow Child classes to access it? You use protected.
| Modifier | Access from own Class | Access from Derived Class | Access from main() |
|---|---|---|---|
public | Yes | Yes | Yes |
protected | Yes | Yes | No |
private | Yes | No | No |
5. Types of Inheritance
1. Single Inheritance:
One child inherits from one parent. (e.g., Dog inherits from Animal).
2. Multilevel Inheritance: A child inherits from a parent, and then another child inherits from that child.
3. Multiple Inheritance: A child inherits from *more than one* parent simultaneously. (C++ supports this, but Java/C# do not because it can get messy!).
6. Constructors and Inheritance
When you create a child object, the Parent's constructor is called first, followed by the Child's constructor. When destroyed, the order is reversed: Child destructor first, then Parent destructor.Output:
7. Calling a Parameterized Base Constructor
If the Base class does not have a default constructor, the Derived class MUST explicitly call the Base's parameterized constructor using an initializer list.8. Memory-Level Explanation
When aDog object is instantiated, the OS allocates a single block of memory large enough to hold all the data members of Animal PLUS all the data members of Dog. The base class data sits at the top of this memory layout, followed by the derived class data.
9. Common Mistakes
-
Private Inheritance by Accident: If you forget
publicinclass Dog : Animal, it defaults toprivateinheritance! This meanseat()becomes private inDogand cannot be called frommain(). Always writeclass Dog : public Animal.
-
The Diamond Problem: In multiple inheritance, if
BandCinherit fromA, andDinherits from bothBandC,Dgets two copies ofA's data! (Solved usingvirtualinheritance).
10. Exercises
-
1.
Create a base class
Shapewith protectedwidthandheight. Create a derived classRectanglewith agetArea()method.
-
2.
Demonstrate Multilevel inheritance by creating
Grandparent,Parent, andChildclasses.
11. MCQ Quiz with Answers
What is the main advantage of Inheritance?
Which symbol is used to define inheritance?
Which access modifier hides data from main() but allows Child classes to access it?
What is it called when a class inherits from two or more Base classes?
When a child object is created, whose constructor runs first?
When a child object is destroyed, whose destructor runs first?
If you omit the access modifier in class Child : Parent, what is the default inheritance type?
private members of its base class?
a) Yes b) No
Answer: b) No (Only public and protected)
What is Multilevel Inheritance?
How do you call a specific Parameterized constructor of the Base class from the Derived class?
12. Interview Questions
- Q: Explain the "Diamond Problem" in Multiple Inheritance and how to solve it.
-
Q: What is the difference between
public,protected, andprivateinheritance?
- Q: Why do Java and C# not support Multiple Inheritance with classes, while C++ does?
13. Summary
Inheritance allows you to build a hierarchy of classes, drastically reducing code duplication. By using theprotected modifier, base classes can safely share data with derived classes. C++ is one of the few languages that supports both multilevel and multiple inheritance.