ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    CSI-312
    Progress0 / 16 topics
    Topics
    1. Evolution of Object-Oriented (OO) Programming2. Object-Oriented (OO) Concepts and Principles3. Problem Solving in OO Paradigm4. OO Programme Design Process5. Classes6. Methods7. Objects and Encapsulation8. Constructors and Destructors9. Operator Overloading10. Function Overloading11. Virtual Functions12. Derived Classes13. Inheritance14. Polymorphism15. I/O and File Processing16. Exception Handling
    CSI-312›Inheritance
    Object Oriented ProgrammingTopic 13 of 16

    Inheritance

    8 minread
    1,320words
    Intermediatelevel

    Inheritance in C++

    Inheritance is one of the key features of Object-Oriented Programming (OOP) that allows a class (called the derived class) to inherit properties and behaviors (attributes and methods) from another class (called the base class or parent class). It helps in reusing code, enhancing the modularity of the system, and promoting the idea of generalization and specialization.

    Inheritance represents the "is-a" relationship between classes. For example, a Dog is a type of Animal, so Dog is a derived class of Animal.


    Key Concepts of Inheritance in C++

    1. Base Class (Parent Class): The class whose properties and behaviors are inherited by another class.
    2. Derived Class (Child Class): The class that inherits the properties and behaviors of another class and can also add new features or modify inherited ones.
    3. Access Control in Inheritance: The derived class can inherit attributes and methods from the base class with varying levels of access control (public, protected, private).

    Types of Inheritance in C++

    1. Single Inheritance: A derived class inherits from a single base class.

      Example:

      class Base { ... };
      class Derived : public Base { ... };
      
    2. Multiple Inheritance: A derived class inherits from more than one base class.

      Example:

      class Base1 { ... };
      class Base2 { ... };
      class Derived : public Base1, public Base2 { ... };
      
    3. Multilevel Inheritance: A derived class inherits from a base class, and then another class inherits from the derived class, forming a chain.

      Example:

      class Base { ... };
      class Derived : public Base { ... };
      class SubDerived : public Derived { ... };
      
    4. Hierarchical Inheritance: Multiple derived classes inherit from a single base class.

      Example:

      class Base { ... };
      class Derived1 : public Base { ... };
      class Derived2 : public Base { ... };
      
    5. Hybrid Inheritance: A combination of two or more types of inheritance (e.g., multiple and multilevel inheritance).

      Example:

      class Base1 { ... };
      class Base2 { ... };
      class Derived : public Base1, public Base2 { ... };
      class SubDerived : public Derived { ... };
      

    Syntax of Inheritance

    class Base {
    public:
        // Base class code
    };
    
    class Derived : access_modifier Base {
    public:
        // Derived class code
    };
    
    • access_modifier: Specifies the type of inheritance (public, protected, or private).

    Types of Access Modifiers in Inheritance

    1. Public Inheritance: Public and protected members of the base class become public and protected members of the derived class, respectively. This is the most commonly used type of inheritance.

      class Derived : public Base { ... };
      
    2. Protected Inheritance: Public and protected members of the base class become protected members in the derived class. It restricts access to the inherited members outside the derived class.

      class Derived : protected Base { ... };
      
    3. Private Inheritance: Public and protected members of the base class become private members in the derived class, making them inaccessible from outside the derived class.

      class Derived : private Base { ... };
      

    Constructor and Destructor in Inheritance

    1. Constructor: When a derived class object is created, the constructor of the base class is called first, followed by the constructor of the derived class. The base class constructor is automatically called unless specified otherwise.

      • Base class constructor is called first (implicitly or explicitly).
      • Derived class constructor is called after the base class constructor.
    2. Destructor: The destructor of the derived class is called first, and then the destructor of the base class is called. If the base class has a virtual destructor, it ensures that the derived class's destructor is properly called when the object is deleted through a base class pointer.


    Example 1: Simple Inheritance (Single Inheritance)

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        void eat() {
            cout << "Eating..." << endl;
        }
    };
    
    class Dog : public Animal {
    public:
        void bark() {
            cout << "Barking..." << endl;
        }
    };
    
    int main() {
        Dog dog;
        dog.eat();  // Inherited from Animal class
        dog.bark();  // Defined in Dog class
        
        return 0;
    }
    

    Explanation:

    • The Dog class inherits the eat() function from the Animal class and also defines its own function bark().
    • We create an object dog of type Dog and call both eat() (inherited) and bark() (defined in Dog).

    Output:

    Eating...
    Barking...
    

    Example 2: Multiple Inheritance

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        void eat() {
            cout << "Eating..." << endl;
        }
    };
    
    class Bird {
    public:
        void fly() {
            cout << "Flying..." << endl;
        }
    };
    
    class Bat : public Animal, public Bird {
    public:
        void sleep() {
            cout << "Sleeping..." << endl;
        }
    };
    
    int main() {
        Bat bat;
        bat.eat();  // Inherited from Animal
        bat.fly();  // Inherited from Bird
        bat.sleep();  // Defined in Bat
        
        return 0;
    }
    

    Explanation:

    • The Bat class inherits from both Animal and Bird, so it can access both eat() and fly(). Additionally, sleep() is defined within Bat.

    Output:

    Eating...
    Flying...
    Sleeping...
    

    Example 3: Multilevel Inheritance

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        void eat() {
            cout << "Eating..." << endl;
        }
    };
    
    class Mammal : public Animal {
    public:
        void walk() {
            cout << "Walking..." << endl;
        }
    };
    
    class Dog : public Mammal {
    public:
        void bark() {
            cout << "Barking..." << endl;
        }
    };
    
    int main() {
        Dog dog;
        dog.eat();  // Inherited from Animal
        dog.walk();  // Inherited from Mammal
        dog.bark();  // Defined in Dog
        
        return 0;
    }
    

    Explanation:

    • The Dog class inherits from Mammal, which in turn inherits from Animal.
    • The object dog can access the methods from all three classes: eat(), walk(), and bark().

    Output:

    Eating...
    Walking...
    Barking...
    

    Example 4: Hierarchical Inheritance

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        void eat() {
            cout << "Eating..." << endl;
        }
    };
    
    class Dog : public Animal {
    public:
        void bark() {
            cout << "Barking..." << endl;
        }
    };
    
    class Cat : public Animal {
    public:
        void meow() {
            cout << "Meowing..." << endl;
        }
    };
    
    int main() {
        Dog dog;
        Cat cat;
        
        dog.eat();  // From Animal class
        dog.bark();  // From Dog class
        
        cat.eat();  // From Animal class
        cat.meow();  // From Cat class
        
        return 0;
    }
    

    Explanation:

    • Both Dog and Cat classes inherit from Animal. As a result, both classes have access to the eat() method from Animal.

    Output:

    Eating...
    Barking...
    Eating...
    Meowing...
    

    Pure Virtual Functions and Abstract Classes

    A class that contains at least one pure virtual function is called an abstract class. An abstract class cannot be instantiated directly, and it forces derived classes to provide an implementation for the pure virtual function.

    #include <iostream>
    using namespace std;
    
    class Animal {
    public:
        virtual void sound() = 0;  // Pure virtual function
    };
    
    class Dog : public Animal {
    public:
        void sound() override {
            cout << "Bark" << endl;
        }
    };
    
    int main() {
        // Animal a;  // Error: cannot instantiate abstract class
        Dog dog;
        dog.sound();  // Bark
        
        return 0;
    }
    

    Explanation:

    • The sound() function is pure virtual in the Animal class. This forces any derived class, like Dog, to implement this function.

    Output:

    Bark
    

    Conclusion

    • Inheritance allows you to create a new class based on an existing class, promoting code reuse and extensibility.
    • C++ supports various types of inheritance (single, multiple, multilevel, hierarchical, hybrid) and offers flexibility in how derived classes can access base class members (via public, protected, or private inheritance).
    • Inheritance, combined with polymorphism (via virtual functions), allows
    Previous topic 12
    Derived Classes
    Next topic 14
    Polymorphism

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time8 min
      Word count1,320
      Code examples0
      DifficultyIntermediate