in , ,

C++ Inheritance

C++ Inheritance
C++ Inheritance

C++ Inheritance

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

Inheritance is one of the key highlights of Object-oriented programming in C++. It allows us to create a new class (determined class) from a current class (base class).

The derived class inherits the features from the base class and can have extra highlights of its own. For instance,

class Animal {
    // eat() function
    // sleep() function
};

class Dog : public Animal {
    // bark() function
};

Here, the Dog class is derived from the Animal class. Since Dog is derived from Animal, members of Animal are accessible to Dog.

Notice the use of the keyword public while inheriting Dog from Animal.

class Dog : public Animal {...};

We can also use the keywords private and protected instead of public. We will learn about the differences between using private, public and protected later in this tutorial.


is-a relationship

Inheritance is an is-a relationship. We use inheritance just if an is-a relationship is available between the two classes.

Here are a few examples:

A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.


Example 1: 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!!

Here, dog1 (the object of derived class Dog) can access members of the base class Animal. It’s because Dog is inherited from Animal.

// Calling members of the Animal class
dog1.eat();
dog1.sleep();

C++ protected Members

The access modifier protected is especially relevant when it comes to C++ inheritance.

Like private members, protected members are inaccessible outside of the class. However, they can be accessed by derived classes and friend classes/functions.

We need protected members if we need to conceal the data of a class, yet at the same time need that data to be acquired by its determined classes.


Example 2 : C++ protected Members

// C++ program to demonstrate protected members

#include <iostream>
#include <string>
using namespace std;

// base class
class Animal {

   private:
    string color;

   protected:
    string type;

   public:
    void eat() {
        cout << "I can eat!" << endl;
    }

    void sleep() {
        cout << "I can sleep!" << endl;
    }

    void setColor(string clr) {
        color = clr;
    }

    string getColor() {
        return color;
    }
};

// derived class
class Dog : public Animal {

   public:
    void setType(string tp) {
        type = tp;
    }

    void displayInfo(string c) {
        cout << "I am a " << type << endl;
        cout << "My color is " << c << endl;
    }

    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();
    dog1.setColor("black");

    // Calling member of the derived class
    dog1.bark();
    dog1.setType("mammal");

    // Using getColor() of dog1 as argument
    // getColor() returns string data
    dog1.displayInfo(dog1.getColor());

    return 0;
}

Output

I can eat!
I can sleep!
I can bark! Woof woof!!
I am a mammal
My color is black

Here, the variable type is protected and is thus accessible from the derived class Dog. We can see this as we have initialized type in the Dog class using the function setType().

On the other hand, the private variable color cannot be initialized in Dog.

class Dog : public Animal {

    public:
      void setColor(string clr) {
          // Error: member "Animal::color" is inaccessible
          color = clr; 
      }
};

Likewise, since the protected watchword shrouds data, we can’t get to type legitimately from an object of Dog or Animal class.

// Error: member "Animal::type" is inaccessible
dog1.type = "mammal";

Access Modes in C++ Inheritance

In our past tutorials, we have found out about C++ access specifiers, for example, public, private, and protected.

Up until this point, we have used the public catchphrase so as to acquire a class from a formerly existing base class. However, we can likewise use private and protected catchphrases to acquire classes. For instance,

class Animal {
    // code
};

class Dog : private Animal {
    // code
};
class Cat : protected Animal {
    // code
};

The different ways we can infer classes are known as access modes. These entrance modes have the accompanying impact:

  1. Public: If an inferred class is pronounced in public mode, at that point the individuals from the base class are acquired by the determined class similarly as they seem to be.

2. Private: For this situation, all the members from the base class become private members in the derived class.

3. Protected: The public members from the base class become protected members in the determined class.

The private members from the base class are consistently private in the derived class.


Member Function Overriding in Inheritance

Assume, base class and derived class have member works with a similar name and arguments.

If we make an object of the determined class and attempt to get to that member’s work, the member work in the derived class is summoned rather than the one in the base class.

The member capacity of determined class overrides the member capacity of the base class.


Thanks for watching! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

How to Make a Calendar in CorelDRAW

How to Make a Calendar in CorelDRAW

How to Convert PDF to Word for Free

Different Ways for How to Convert PDF to Word for Free