Single Inheritance and Derived
Classes in C++
Understanding Basic OOP Concepts
in C++
What is Inheritance?
• Inheritance is a mechanism in C++ that allows
one class (derived) to inherit properties and
behaviors (data and functions) from another
class (base).
• Why Use It?
• - Code reusability
• - Reduces redundancy
• - Easier maintenance
Types of Inheritance in C++
• - Single Inheritance
• - Multiple Inheritance
• - Multilevel Inheritance
• - Hierarchical Inheritance
• - Hybrid Inheritance
• 👉 Focus Today: Single Inheritance
What is Single Inheritance?
• A derived class inherits from only one base
class.
• Syntax:
• class Base {
• // members
• };
• class Derived : access-specifier Base {
Access Specifiers in Inheritance
• - Public Inheritance: Public & protected
members stay the same
• - Protected Inheritance: Public becomes
protected
• - Private Inheritance: Public & protected
become private
• Example:
• class Derived : public Base {
Example of Single Inheritance
• #include <iostream>
• using namespace std;
• class Animal {
• public:
• void eat() {
• cout << "This animal eats food.\n";
• }
• };
Output of the Example
• This animal eats food.
• The dog barks.
• Explanation:
• - Dog inherits the eat() method from Animal
• - Can use both base and derived class
methods
Base vs. Derived Class
• Aspect | Base Class | Derived Class
• ----------------|------------------|---------------------
• Definition | Original class | Inherits from
base
• Access | Direct | Uses inheritance
• Use | General purpose | More specific
Key Benefits of Inheritance
• ✅ Code Reusability
• ✅ Better Code Organization
• ✅ Easy to Extend Functionality
• ✅ Supports OOP Principles (Encapsulation,
Abstraction, Polymorphism)
Conclusion
• Recap:
• - Single Inheritance involves one base and one
derived class
• - Use inheritance to promote clean,
maintainable code
• - Understand access specifiers and their
impact
• Next Steps: