Single Inheritance Cpp Presentation
Single Inheritance Cpp Presentation
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).
• Syntax:
• class Base {
• // members
• };
• 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: