c sharp Logo

Inheritance


Inheritance in C++ is a programming technique that allows you to create new classes from existing classes. This is done by using the : operator. The new class is called the derived class, and the existing class is called the base class.

Inheritance allows you to reuse code and create more complex hierarchies of classes. For example, you might create a base class named Animal and then create derived classes for different types of animals, such as Dog, Cat, and Bird.

Example

The following code shows a simple example of inheritance:

class Animal {

public:

  std::string name;

  int age;

 

  void Eat() {

    std::cout << "The animal is eating." << std::endl;

  }

};

 

class Dog : public Animal {

public:

  void Bark() {

    std::cout << "The dog is barking." << std::endl;

  }

};

 

The Dog class inherits from the Animal class. This means that the Dog class has all of the data members and methods of the Animal class. In addition, the Dog class has its own Bark() method.

To create a new Dog object, you can use the following syntax:

Dog dog("Fido", 1);

 

This will create a new Dog object with the name "Fido" and the age 1.

You can then call the methods of the Dog object as follows:

dog.Eat();

dog.Bark();

 

Output:

The animal is eating.

The dog is barking.

 

Benefits of using inheritance

There are several benefits to using inheritance in C++, including:

  • Code reuse: Inheritance allows you to reuse code by creating new classes from existing classes.
  • Code modularity: Inheritance helps to make code more modular by grouping related classes together.
  • Code maintainability: Inheritance makes code more maintainable by making it easier to add new features and fix bugs.

Example

The following code shows how to use inheritance to create a more complex class hierarchy:

C++

class Animal {

public:

  std::string name;

  int age;

 

  void Eat() {

    std::cout << "The animal is eating." << std::endl;

  }

};

 

class Mammal : public Animal {

public:

  void GiveBirth() {

    std::cout << "The mammal is giving birth." << std::endl;

  }

};

 

class Dog : public Mammal {

public:

  void Bark() {

    std::cout << "The dog is barking." << std::endl;

  }

};

 

class Cat : public Mammal {

public:

  void Purr() {

    std::cout << "The cat is purring." << std::endl;

  }

};

 

The Dog and Cat classes inherit from the Mammal class, which inherits from the Animal class. This means that the Dog and Cat classes have all of the data members and methods of the Animal and Mammal classes. In addition, the Dog and Cat classes have their own unique methods, Bark() and Purr(), respectively.

To create a new Dog object, you can use the following syntax:

Dog dog("Fido", 1);

 

To create a new Cat object, you can use the following syntax:

Cat cat("Felix", 2);

 

You can then call the methods of the Dog and Cat objects as follows:

dog.Eat();

dog.Bark();

 

cat.Eat();

cat.Purr();

 

Output:

The animal is eating.

The dog is barking.

The animal is eating.

The cat is purring.

 

Conclusion

Inheritance is a powerful feature of C++ that can be used to write more reusable, modular, and maintainable code. By understanding how to use inheritance, you can write better C++ code.