Current Affairs updated daily
KalviGavaniKalviGavani

Inheritance Exercise Solutions | KalviGavani

TN Class 11 Computer Science Inheritance exercise solutions — single, multiple, multilevel, hierarchical, hybrid inheritance with worked examples.

Updated 15 June 202617 min read

Exercise Solutions Available

Step-by-step answers for all textbook questions in this chapter

View Solutions →

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.

Base Class (Parent) | Derived Class (Child) — inherits from Base Class

Syntax

class Derived : access_specifier Base {
    // additional members
};

Types of Inheritance

TypeDescriptionDiagram
SingleOne base class, one derived classA -> B
MultipleMultiple base classes, one derived classA, B -> C
MultilevelChain of inheritanceA -> B -> C
HierarchicalOne base class, multiple derived classesA -> B, A -> C
HybridCombination of two or more typesMix of above

Access Specifiers in Inheritance

Base MemberPublic InheritanceProtected InheritancePrivate Inheritance
publicpublic in derivedprotected in derivedprivate in derived
protectedprotected in derivedprotected in derivedprivate in derived
privateNot accessibleNot accessibleNot 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)

1. In C++, the symbol used to specify inheritance is:
Answer

(a) :: (b) : (c) -> (d) .

Answer(b) : The colon (:) is used in the class header to specify inheritance. Example: class Derived : public Base.
2. In public inheritance, a protected member of the base class becomes:
Answer

(a) Public in the derived class (b) Protected in the derived class (c) Private in the derived class (d) Not accessible

Answer(b) Protected in the derived class In public inheritance, protected members of the base class remain protected in the derived class. Only the access level of public members changes (they stay public).
3. Which type of inheritance involves one base class and multiple derived classes?
Answer

(a) Single (b) Multiple (c) Multilevel (d) Hierarchical

Answer(d) Hierarchical Hierarchical inheritance has one base class with multiple derived classes. Multiple inheritance has multiple base classes with one derived class.
4. Which of the following is NOT inherited by a derived class?
Answer

(a) Public members (b) Protected members (c) Private members (d) Constructor of the base class

Answer(c) Private members Private members of the base class are never accessible in the derived class, regardless of the inheritance mode. Note: Base class constructors are also not inherited (they are called, not inherited).
5. In multilevel inheritance, if class B inherits from class A and class C inherits from class B, then class C can access:
Answer

(a) Only its own members (b) Members of B only (c) Members of A and B (d) Members of A only

Answer(c) Members of A and B In multilevel inheritance, each derived class inherits all accessible members of its base class. Since B inherits A's members and C inherits B's members (which include A's), C can access members of both A and B.
6. When an object of a derived class is created, the order of constructor calls is:
Answer

(a) Derived class constructor first (b) Base class constructor first (c) Both called simultaneously (d) Only derived class constructor is called

Answer(b) Base class constructor first When a derived class object is created, the base class constructor is called first, followed by the derived class constructor. This ensures the base part is initialised before the derived part.

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 AColumn B
1. Single inheritancea. One base, multiple derived
2. Multiple inheritanceb. Chain A -> B -> C
3. Multilevel inheritancec. One base, one derived
4. Hierarchical inheritanced. Multiple bases, one derived
5. Private memberse. Not accessible in derived class
6. protected inheritancef. Public members become protected
7. Base constructorg. Called before derived constructor
8. Is-a relationshiph. Established through inheritance
Answer1-c, 2-d, 3-b, 4-a, 5-e, 6-f, 7-g, 8-h

7. V. Answer Briefly (2-mark questions)

Q1. What is inheritance? What are its advantages?
Answer

Inheritance is an OOP mechanism that allows a new class (derived class) to acquire the properties and methods of an existing class (base class).

Advantages:

  • Code reusability — No need to rewrite common code in the derived class.
  • Extensibility — New features can be added to the derived class without modifying the base class.
  • Maintainability — Changes in the base class automatically apply to derived classes.
Q2. Differentiate between single inheritance and multiple inheritance.
Answer
FeatureSingle InheritanceMultiple Inheritance
Base classesOneTwo or more
Derived classesOneOne
Syntaxclass B : public Aclass C : public A, public B
ComplexitySimpleCan cause ambiguity (diamond problem)
ExampleVehicle -> CarFather, Mother -> Child
Q3. What happens to access specifiers under different inheritance modes?
Answer
Base MemberPublic InheritanceProtected InheritancePrivate Inheritance
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privateNot accessibleNot accessibleNot accessible

Private members are never accessible in the derived class, regardless of the inheritance mode.

Q4. What is the order of constructor and destructor calls in inheritance?
Answer

Constructor order: Base class constructor is called first, then the derived class constructor. (Base -> Derived)

Destructor order: Derived class destructor is called first, then the base class destructor. (Derived -> Base)

This is because the base part must be initialised before the derived part, and the derived part must be cleaned up before the base part.

Q5. Differentiate between is-a and has-a relationships.
Answer
FeatureIs-a (Inheritance)Has-a (Composition)
Meaning"A Car is-a Vehicle""A Car has-a Engine"
ImplementationClass derivationObject as member
Exampleclass Car : public Vehicleclass Car { Engine e; };
ReusabilityInherits all featuresUses features through member

8. VI. Answer in Detail (5-mark questions)

Q1. Explain the types of inheritance with C++ examples.
Answer

1. Single Inheritance: One base class, one derived class.

#include <iostream>
using namespace std;
class Animal {
public:
    void eat() { cout << "Animal is eating" << endl; }
};
class Dog : public Animal {
public:
    void bark() { cout << "Dog is barking" << endl; }
};
int main() {
    Dog d;
    d.eat();   // Inherited from Animal
    d.bark();  // Dog's own method
    return 0;
}

2. Multiple Inheritance: A derived class inherits from more than one base class.

#include <iostream>
using namespace std;
class Father {
public:
    void height() { cout << "Tall height from Father" << endl; }
};
class Mother {
public:
    void skinTone() { cout << "Fair skin from Mother" << endl; }
};
class Child : public Father, public Mother {
public:
    void talent() { cout << "Child's own talent" << endl; }
};
int main() {
    Child c;
    c.height();    // From Father
    c.skinTone();   // From Mother
    c.talent();     // Child's own
    return 0;
}

3. Multilevel Inheritance: A chain where each class inherits from the previous one.

#include <iostream>
using namespace std;
class Grandparent {
public:
    void house() { cout << "Grandparent's house" << endl; }
};
class Parent : public Grandparent {
public:
    void car() { cout << "Parent's car" << endl; }
};
class Child : public Parent {
public:
    void bike() { cout << "Child's bike" << endl; }
};
int main() {
    Child c;
    c.house();  // From Grandparent
    c.car();    // From Parent
    c.bike();   // Child's own
    return 0;
}

4. Hierarchical Inheritance: One base class, multiple derived classes.

#include <iostream>
using namespace std;
class Shape {
public:
    void draw() { cout << "Drawing shape" << endl; }
};
class Circle : public Shape {
public:
    void area() { cout << "Circle area = pi * r * r" << endl; }
};
class Rectangle : public Shape {
public:
    void area() { cout << "Rectangle area = l * b" << endl; }
};
int main() {
    Circle c;
    Rectangle r;
    c.draw();  // From Shape
    c.area();  // Circle's own
    r.draw();  // From Shape
    r.area();  // Rectangle's own
    return 0;
}

5. Hybrid Inheritance: A combination of two or more types of inheritance.

// Combination of hierarchical and single inheritance
class Vehicle {
public:
    void start() { cout << "Vehicle started" << endl; }
};
class FourWheeler : public Vehicle {
public:
    void drive() { cout << "Driving four-wheeler" << endl; }
};
class TwoWheeler : public Vehicle {
public:
    void ride() { cout << "Riding two-wheeler" << endl; }
};
// Hierarchical (Vehicle -> FourWheeler, TwoWheeler)
// and Single (Vehicle -> FourWheeler)
Q2. Explain how access specifiers work in inheritance with a complete example showing public, protected, and private members.
#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;
}
Answer

Summary of access rules in public inheritance:

Base MemberIn Derived ClassOutside Code
privateNot accessibleNot accessible
protectedAccessible as protectedNot accessible
publicAccessible as publicAccessible
Q3. Write a C++ program to demonstrate single inheritance with a 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;
}
Answer

Key points:

  • Student inherits Person's name and age (protected) and getPersonData() and displayPersonData() (public).
  • The derived class adds rollNo, marks, and calculateGrade().
  • getStudentData() calls the base class function getPersonData() to reuse code.
  • Protected members of Person are accessible inside Student but not from main().

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

Frequently Asked Questions

Inheritance is the mechanism by which a new class (derived class) acquires the properties and behaviour of an existing class (base class). It promotes code reuse and establishes an is-a relationship.