Inheritance

October 5, 2022
C++ Programming

C++ Inheritance

In this tutorial, we will learn about inheritance in C++ with the help of examples.

Inheritance is one of the key features of Object-oriented programming in C++. It allows us to create a new class (derived class) from an existing class (base class).

The derived class inherits the features from the base class and can have additional features of its own. For example,

Simple Example of C++ Inheritance

// C++ program to demonstrate inheritance
#include <iostream>
using namespace std;
// base class
class Animal {
  public:
   void eat() {
       cout << "I can eat!" << endl;
   }
   void sleep() {
       cout << "I can sleep!" << endl;
   }
};
// derived class
class Dog : public Animal {
  public:
   void bark() {
       cout << "I can bark! Woof woof!!" << endl;
   }
};
int main() {
   // Create object of the Dog class
   Dog dog1;
   // Calling members of the base class
   dog1.eat();
   dog1.sleep();
   // Calling member of the derived class
   dog1.bark();
   return 0;
}

Output

I can eat!I can sleep!I can bark! Woof woof!!

VIkas Donta

My name is VIkas Donta and I first discovered Web Designingin 2018. Since then, It has impact on my web design projects development career, and  improve my understanding of HTML/CSS tremendously!

Related Posts

Stay in Touch

Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form