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›Derived Classes
    Object Oriented ProgrammingTopic 12 of 16

    Derived Classes

    8 minread
    1,275words
    Intermediatelevel

    Derived Classes in C++

    In C++, a derived class is a class that is based on another class (called the base class or parent class). The derived class inherits properties (data members) and behaviors (member functions) of the base class, and it can also have additional features or override base class functionality. This mechanism is central to inheritance, which is one of the four pillars of Object-Oriented Programming (OOP).

    Key Concepts of Derived Classes

    1. Inheritance:

      • A derived class inherits the data members and functions of the base class.
      • It can also have additional members or functions of its own.
      • Inheritance allows you to create a new class by reusing the functionality of an existing class.
    2. Access to Base Class Members:

      • The derived class has access to the public and protected members of the base class, but it cannot directly access the private members (except through getter/setter functions or friend classes).
    3. Overriding:

      • A derived class can override a base class function (if it's virtual) to provide a specific implementation of the function for objects of the derived class.
    4. Constructor and Destructor:

      • The derived class can call the constructor of the base class (implicitly or explicitly).
      • Similarly, if the base class has a virtual destructor, the derived class should call it to ensure proper cleanup when objects are destroyed.
    5. Access Control:

      • Inheritance allows specifying different types of access to base class members using access modifiers (public, protected, private).
        • Public Inheritance: The public and protected members of the base class become public and protected in the derived class.
        • Protected Inheritance: The public and protected members of the base class become protected in the derived class.
        • Private Inheritance: The public and protected members of the base class become private in the derived class.

    Syntax of Derived Classes

    class Base {
        // Base class code
    };
    
    class Derived : access_modifier Base {
        // Derived class code
    };
    
    • access_modifier: Specifies the type of inheritance (public, protected, or private).
    • The derived class inherits the attributes and methods of the base class and can add or modify them as needed.

    Example 1: Basic Inheritance with Derived Class

    In this example, we define a Base class and a Derived class. The derived class inherits properties from the base class.

    #include <iostream>
    using namespace std;
    
    class Base {
    public:
        int baseValue;
    
        // Constructor for Base class
        Base(int val) {
            baseValue = val;
        }
    
        void displayBase() {
            cout << "Base Value: " << baseValue << endl;
        }
    };
    
    class Derived : public Base {
    public:
        int derivedValue;
    
        // Constructor for Derived class
        Derived(int val1, int val2) : Base(val1) {
            derivedValue = val2;
        }
    
        void displayDerived() {
            cout << "Derived Value: " << derivedValue << endl;
        }
    };
    
    int main() {
        Derived derivedObj(5, 10);
        
        derivedObj.displayBase();   // From Base class
        derivedObj.displayDerived();  // From Derived class
        
        return 0;
    }
    

    Explanation:

    • Derived inherits from Base using public inheritance. This means the public members of the Base class are accessible in the Derived class.
    • The constructor of Derived calls the constructor of Base to initialize the baseValue.
    • Both displayBase() and displayDerived() are called to show the values from both classes.

    Output:

    Base Value: 5
    Derived Value: 10
    

    Example 2: Overriding Base Class Functions in Derived Class

    In this example, the Derived class overrides the display() function of the Base class.

    #include <iostream>
    using namespace std;
    
    class Base {
    public:
        // Virtual function
        virtual void display() {
            cout << "Display from Base class" << endl;
        }
    };
    
    class Derived : public Base {
    public:
        // Override the base class function
        void display() override {
            cout << "Display from Derived class" << endl;
        }
    };
    
    int main() {
        Base* basePtr;
        Derived derivedObj;
        
        basePtr = &derivedObj;
        
        // Calls the derived class version of display
        basePtr->display();
        
        return 0;
    }
    

    Explanation:

    • The display() function is declared as virtual in the base class. This allows the Derived class to override it.
    • When basePtr->display() is called (even though basePtr is a pointer to Base), the derived class version of display() is invoked at runtime due to dynamic dispatch.

    Output:

    Display from Derived class
    

    Example 3: Access Control in Derived Classes

    This example demonstrates how different access specifiers (public, protected, and private) affect member inheritance in derived classes.

    #include <iostream>
    using namespace std;
    
    class Base {
    public:
        int publicValue;
        
    protected:
        int protectedValue;
        
    private:
        int privateValue;
    
    public:
        Base(int pub, int prot, int priv) {
            publicValue = pub;
            protectedValue = prot;
            privateValue = priv;
        }
    };
    
    class DerivedPublic : public Base {
    public:
        DerivedPublic(int pub, int prot, int priv) : Base(pub, prot, priv) {}
    
        void display() {
            cout << "Public Value: " << publicValue << endl;   // Accessible
            cout << "Protected Value: " << protectedValue << endl;  // Accessible
            // cout << "Private Value: " << privateValue << endl;  // Not accessible
        }
    };
    
    int main() {
        DerivedPublic obj(10, 20, 30);
        obj.display();
        
        return 0;
    }
    

    Explanation:

    • In DerivedPublic, the public and protected members of the Base class are accessible, but the private members are not.
    • The privateValue is not accessible from the derived class.

    Output:

    Public Value: 10
    Protected Value: 20
    

    Types of Inheritance in C++

    1. Single Inheritance:

      • A derived class inherits from a single base class.
      • Example: class Derived : public Base.
    2. Multiple Inheritance:

      • A derived class inherits from more than one base class.
      • C++ allows multiple inheritance, which can sometimes lead to ambiguity if the base classes have members with the same names.
      class Base1 { /* ... */ };
      class Base2 { /* ... */ };
      
      class Derived : public Base1, public Base2 { /* ... */ };
      
    3. Multilevel Inheritance:

      • A derived class is inherited by another derived class, forming a chain.
      • Example: class Derived : public Base, class SubDerived : public Derived.
    4. Hierarchical Inheritance:

      • Multiple derived classes inherit from a single base class.
      • Example: class Derived1 : public Base, class Derived2 : public Base.
    5. Hybrid Inheritance:

      • A combination of two or more types of inheritance.
      • Example: A class inherits from a derived class and from another class at the same time.

    Example of Multiple Inheritance

    #include <iostream>
    using namespace std;
    
    class Base1 {
    public:
        void base1Function() {
            cout << "Base1 Function" << endl;
        }
    };
    
    class Base2 {
    public:
        void base2Function() {
            cout << "Base2 Function" << endl;
        }
    };
    
    class Derived : public Base1, public Base2 {
    public:
        void derivedFunction() {
            cout << "Derived Function" << endl;
        }
    };
    
    int main() {
        Derived obj;
        obj.base1Function();  // From Base1
        obj.base2Function();  // From Base2
        obj.derivedFunction();  // From Derived
        
        return 0;
    }
    

    Explanation:

    • The Derived class inherits from both Base1 and Base2.
    • It has access to both base1Function() and base2Function().

    Output:

    Base1 Function
    Base2 Function
    Derived Function
    

    Conclusion

    • Derived classes in C++ allow for code reuse and extension of functionality through inheritance.
    • A derived class can override functions, add new members, and control access to inherited members through various access modifiers.
    • Inheritance enables polymorphism, particularly through virtual functions, which allow the derived class to define its own behavior while maintaining a common interface.
    • Understanding the types of inheritance and access control helps in creating flexible and maintainable code structures.
    Previous topic 11
    Virtual Functions
    Next topic 13
    Inheritance

    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,275
      Code examples0
      DifficultyIntermediate