Inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP), and it allows you to create a new class that is based on an existing class. This existing class is referred as the base class or parent class and the new class is called the derived class or child class. Inheritance facilitates code reusability and the creation of a hierarchical structure for classes.

In C++, there are different types of inheritance:

  1. Single Inheritance: In single inheritance, a derived class inherits from a single class.
  2. Multiple Inheritance: In multiple inheritance, a derived class can inherit from multiple base classes.
  3. Multilevel Inheritance: In multilevel inheritance, a class inherits from a base class, and then another class inherits from the derived classs, creating a chain.
  4. Hierarchical Inheritance: In hierarchical inheritance, multiple classes inherit from a single base class.
  5. Hybrid Inheritance: Hybrid Inheritance is a combination of two or more types of inheritance.

Here’s an example in C++ that demonstrates single inheritance:

C++




#include <iostream>
 
// Base class (Parent class)
class Animal {
public:
    void eat()
    {
        std::cout << "Animal is eating." << std::endl;
    }
 
    void sleep()
    {
        std::cout << "Animal is sleeping." << std::endl;
    }
};
 
// Derived class (Child class)
class Dog : public Animal {
public:
    void bark()
    {
        std::cout << "Dog is barking." << std::endl;
    }
};
 
int main()
{
    Dog myDog;
 
    myDog.eat(); // Inherited from Animal
    myDog.sleep(); // Inherited from Animal
    myDog.bark(); // Part of Dog class
 
    return 0;
}


Output

Animal is eating.
Animal is sleeping.
Dog is barking.

This is a simple example of single inheritance in C++. The Dog class inherits the properties and behaviors of Animal class and can also have its own additional members. This code structure promotes code reuse and the modeling of relationships between different classes in a hierarchical manner.

Object Model | Object Oriented Analysis & Design

Object-Oriented Programming (OOP) is a fundamental paradigm in modern software development that has transformed the way we design, build, and maintain software systems. OOP is centered around the concept of objects, which are self-contained, reusable units that encapsulate both data and the operations that can be performed on that data. This approach mirrors real-world modeling, making it easier to understand, manage, and expand software projects.

Important Topics for the Object Model

  • Objects and Classes
  • Encapsulation and Data Hiding
  • Message Passing
  • Inheritance
  • Polymorphism
  • Generalization and Specialization
  • Association and Aggregation
  • Benefits of Object Model
  • Conclusion

Similar Reads

Objects and Classes

What is a Class?...

Encapsulation and Data Hiding

...

Message Passing

...

Inheritance

Encapsulation and Data Hiding are two fundamental principles of object-oriented programming....

Polymorphism

...

Generalization and Specialization

...

Association and Aggregation

Message Passing is a fundamental conecpt in computer science and programming especially in the context of concurrent and distributed systems. It refers to a method of communication between different processes or objects, where they exchange information by sending and receiving messages. This enables the synchronization and cooordination of activities in a multi-threaded or distributed environment....

Benefits of Object Model

Inheritance is a fundamental concept in object-oriented programming (OOP), and it allows you to create a new class that is based on an existing class. This existing class is referred as the base class or parent class and the new class is called the derived class or child class. Inheritance facilitates code reusability and the creation of a hierarchical structure for classes....

Conclusion

...

Contact Us