Exercise Solutions Available
Step-by-step answers for all textbook questions in this chapter
1. How to Use This Page
1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.
2. Key Concepts (Quick Recap)
What is Inheritance?
Inheritance is an OOP mechanism that allows a new class (derived class) to acquire the properties and methods of an existing class (base class). It promotes code reusability and establishes an is-a relationship.
Syntax
class Derived : access_specifier Base {
// additional members
};
Types of Inheritance
| Type | Description | Diagram |
|---|---|---|
| Single | One base class, one derived class | A -> B |
| Multiple | Multiple base classes, one derived class | A, B -> C |
| Multilevel | Chain of inheritance | A -> B -> C |
| Hierarchical | One base class, multiple derived classes | A -> B, A -> C |
| Hybrid | Combination of two or more types | Mix of above |
Access Specifiers in Inheritance
| Base Member | Public Inheritance | Protected Inheritance | Private Inheritance |
|---|---|---|---|
public | public in derived | protected in derived | private in derived |
protected | protected in derived | protected in derived | private in derived |
private | Not accessible | Not accessible | Not accessible |
Key rule: Private members are never inherited, regardless of the inheritance mode.
Single Inheritance
class Vehicle { // Base class
public:
void start() {
cout << "Vehicle started";
}
};
class Car : public Vehicle { // Derived class
public:
void drive() {
cout << "Car is driving";
}
};
Car c;
c.start(); // Inherited from Vehicle
c.drive(); // Car's own method
Multiple Inheritance
class Father {
public:
void height() { cout << "Tall"; }
};
class Mother {
public:
void skinTone() { cout << "Fair"; }
};
class Child : public Father, public Mother {
// Child inherits from both Father and Mother
};
Child ch;
ch.height(); // From Father
ch.skinTone(); // From Mother
Multilevel Inheritance
class Animal {
public:
void eat() { cout << "Eating"; }
};
class Dog : public Animal {
public:
void bark() { cout << "Barking"; }
};
class Puppy : public Dog {
public:
void play() { cout << "Playing"; }
};
Puppy p;
p.eat(); // Inherited from Animal
p.bark(); // Inherited from Dog
p.play(); // Puppy's own
Hierarchical Inheritance
class Shape {
public:
void draw() { cout << "Drawing shape"; }
};
class Circle : public Shape {
// Circle inherits draw()
};
class Rectangle : public Shape {
// Rectangle inherits draw()
};
Constructors in Inheritance
- Base class constructors are called before derived class constructors.
- When a derived object is created, the base constructor runs first, then the derived constructor.
- To pass arguments to the base constructor, use the initialiser list:
class Base {
int x;
public:
Base(int a) { x = a; }
};
class Derived : public Base {
int y;
public:
Derived(int a, int b) : Base(a) { // Pass 'a' to Base constructor
y = b;
}
};
3. I. Choose the Best Answer (MCQs)
4. II. Fill in the Blanks
1. The process by which a new class acquires properties from an existing class is called inheritance. 2. The class from which properties are inherited is called the base class (or parent class). 3. The class that inherits properties is called the derived class (or child class). 4. In single inheritance, one base class has one derived class. 5. In multiple inheritance, a derived class inherits from more than one base class. 6. In multilevel inheritance, a class is derived from another derived class, forming a chain. 7. In hierarchical inheritance, one base class has multiple derived classes. 8. Private members of a base class are never inherited by a derived class. 9. In public inheritance, public members of the base class remain public in the derived class. 10. In protected inheritance, public members of the base class become protected in the derived class. 11. When a derived class object is created, the base class constructor is called first. 12. The is-a relationship is established through inheritance (e.g., a Car is-a Vehicle). 13. The has-a relationship is established through containment/composition (e.g., a Car has-a Engine).
5. III. True or False (with corrections)
1. A derived class can access private members of the base class directly.
False. Private members of the base class are never directly accessible in the derived class. The derived class can access them only through public or protected member functions of the base class.
2. In multiple inheritance, a derived class can inherit from more than one base class.
True. Multiple inheritance allows a derived class to inherit from two or more base classes simultaneously, combining features from all of them.
3. In public inheritance, protected members become private in the derived class.
False. In public inheritance, protected members remain protected in the derived class. Only in private inheritance do protected members become private.
4. Constructors of the base class are inherited by the derived class.
False. Constructors are not inherited. However, the base class constructor is called (not inherited) when a derived class object is created. The derived class must define its own constructors.
5. Multilevel inheritance involves a chain of classes where each class inherits from the previous one.
True. In multilevel inheritance, class B inherits from A, and class C inherits from B, forming a chain A -> B -> C. Each level adds new features.
6. The : symbol is used to indicate inheritance in C++.
True. The colon (:) in the class header indicates inheritance. Example: class Derived : public Base.
6. IV. Match the Following
| Column A | Column B |
|---|---|
| 1. Single inheritance | a. One base, multiple derived |
| 2. Multiple inheritance | b. Chain A -> B -> C |
| 3. Multilevel inheritance | c. One base, one derived |
| 4. Hierarchical inheritance | d. Multiple bases, one derived |
| 5. Private members | e. Not accessible in derived class |
6. protected inheritance | f. Public members become protected |
| 7. Base constructor | g. Called before derived constructor |
| 8. Is-a relationship | h. Established through inheritance |
7. V. Answer Briefly (2-mark questions)
8. VI. Answer in Detail (5-mark questions)
#include <iostream>
using namespace std;
class Base {
private:
int privateVar; // Not accessible in derived class
protected:
int protectedVar; // Accessible in derived class
public:
int publicVar; // Accessible everywhere
void setValues() {
privateVar = 10;
protectedVar = 20;
publicVar = 30;
}
void showBase() {
cout << "Base - Private: " << privateVar << endl;
cout << "Base - Protected: " << protectedVar << endl;
cout << "Base - Public: " << publicVar << endl;
}
};
class Derived : public Base {
public:
void showDerived() {
// cout << privateVar; // ERROR: private not accessible
cout << "Derived - Protected: " << protectedVar << endl;
cout << "Derived - Public: " << publicVar << endl;
}
};
int main() {
Derived d;
d.setValues();
d.showBase();
d.showDerived();
// d.privateVar = 5; // ERROR: private not accessible
// d.protectedVar = 5; // ERROR: protected not accessible from outside
d.publicVar = 50; // OK: public accessible from outside
return 0;
}
Person base class and a Student derived class.#include <iostream>
#include <cstring>
using namespace std;
class Person {
protected:
char name[30];
int age;
public:
void getPersonData() {
cout << "Enter name: ";
cin >> name;
cout << "Enter age: ";
cin >> age;
}
void displayPersonData() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
}
};
class Student : public Person {
private:
int rollNo;
float marks;
public:
void getStudentData() {
getPersonData(); // Call base class function
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter marks: ";
cin >> marks;
}
void displayStudentData() {
displayPersonData(); // Call base class function
cout << "Roll No: " << rollNo << endl;
cout << "Marks: " << marks << endl;
}
char calculateGrade() {
if (marks >= 90) return 'A';
else if (marks >= 75) return 'B';
else if (marks >= 60) return 'C';
else return 'D';
}
};
int main() {
Student s;
cout << "--- Enter Student Details ---" << endl;
s.getStudentData();
cout << "\n--- Student Report ---" << endl;
s.displayStudentData();
cout << "Grade: " << s.calculateGrade() << endl;
return 0;
}
9. Common Mistakes to Avoid
1. Confusing base class and derived class — The base class is the existing class; the derived class is the new class that inherits. class Dog : public Animal means Dog is the derived class, Animal is the base.
2. Trying to access private members of the base class — Private members are never accessible in the derived class. Use protected or public with getter/setter functions.
3. Forgetting the access specifier in inheritance — class Derived : Base defaults to private inheritance. Always specify the access specifier explicitly: class Derived : public Base.
4. Wrong constructor call order — Base class constructors are called first, then derived. Destructors are called in reverse order. Remember: construction goes base-to-derived; destruction goes derived-to-base.
5. Ambiguity in multiple inheritance — If two base classes have a member with the same name, the derived class must specify which one using the scope resolution operator: Father::height() vs Mother::height().
6. Confusing is-a and has-a — Inheritance models an is-a relationship (Car is-a Vehicle). Composition models a has-a relationship (Car has-a Engine). Don't use inheritance when composition is appropriate.
7. Not using the initialiser list for base constructor arguments — When the base class has a parameterised constructor, the derived class must pass arguments using the initialiser list: Derived(int a) : Base(a) { }.
10. Exam Checklist
- [ ] I can define inheritance and explain its advantages
- [ ] I can list and explain all five types of inheritance with diagrams
- [ ] I can write C++ programs for single, multiple, and multilevel inheritance
- [ ] I understand the access specifier table for public, protected, and private inheritance
- [ ] I know that private members are never inherited
- [ ] I can differentiate between is-a and has-a relationships
- [ ] I can explain the order of constructor and destructor calls in inheritance
- [ ] I can use the initialiser list to pass arguments to base constructors
- [ ] I can resolve ambiguity in multiple inheritance using scope resolution
- [ ] I can write complete programs demonstrating inheritance with classes and objects
