25 C
Jaipur
Friday, April 16, 2021

C++ Inheritance – Linux Hint

Must read

Inheritance is a very important feature of object-oriented programming. It allows the programmer to derive a class from an existing class. This is very useful especially in a large complex project because it allows the programmer to re-use the code.

In this article, we are going to discuss the inheritance concept in C++ programming. We will explain the concept of the friend function in C++ with working examples.

Why Inheritance?

Inheritance allows creating a new class or derived class from another class or base class. The derived class or the child class will have all the features of the parent class or base class. We can re-use the code with the help of inheritance.

Type of Inheritance

There are different types of inheritance:

  1. Simple/Single Inheritance
  2. Hierarchical Inheritance
  3. Multilevel Inheritance
  4. Multiple Inheritance

In this article, we are going to consider only simple/single inheritance.

Example 1:

Now, let us look into an example program to understand the concept of inheritance in C++. We have defined a base class and then derived another class from it. Therefore, the derived class will have the features (members and functions) from the base class.

#include <iostream>

using namespace std;

class Base_Class
{
public:
    int i;
    void display()
    {
        cout << “Display of Base Class “ << i << endl;
    }
   
};

class Derived_Class:public Base_Class
{
public:
    void show()
    {
        cout << “Show of Derived Class” << endl;
    }
};

int main()
{
    Derived_Class dc;
    dc.i = 100;
    dc.display();
    dc.show();

    return 0;
}

Example 2:

This is another example of inheritance in C++. In this example, we are going to see how the constructors are called when a derived class object is created.

As you can see below, we have defined two base class constructors and three derived class constructors. You can clearly notice from the below output that the base class constructor is called first before the derived class constructor is called.

#include <iostream>

#include
using namespace std;

class Base_Class
{
public:
    Base_Class()
    {
        cout << “Base_Class – No Parameters” << endl;
    }
    Base_Class(int x)
    {
        cout << “Base_Class – Parameters : “ << x << endl;
    }
};

class Derived_Class:public Base_Class
{
public:
    Derived_Class()
    {
        cout << “Derived_Class – No Parameters” << endl;
    }
    Derived_Class(int y)
    {
        cout << “Derived_Class – Parameters : “ << y << endl;
    }
    Derived_Class(int x,int y):Base_Class(x)
    {
        cout << “Param of Derived_Class : “ << y << endl;
    }
};

int main()
{
    Derived_Class d(7,19);
}

Example 3:

In this example, we are going to see how derived class objects can be used.

As you can see, there are two classes defined: Rectangle_Class and Cube_Class. The Rectangle_Class is the base class from which the derived class, i.e., Cube_Class is derived. Therefore, we are inheriting the features from the Rectangle_Class to Cube_Class.

Also, you can notice that we are inheriting the Cube_Class with the public access control. This means that the derived class can access all the non-private members of the base class.

We have declared an object of the derived class, and then call the methods from base class, i.e., setLength() and setBreadth().

#include <iostream>

using namespace std;

class Rectangle_Class
{
private:
    int length;
    int breadth;
public:
    Rectangle_Class();
    Rectangle_Class(int l,int b);
    Rectangle_Class(Rectangle_Class &r);
    int getLength()
    {
        return length;
    }
    int getBreadth()
    {
        return breadth;
    }
    void setLength(int l);
    void setBreadth(int b);
    int area();
};

class Cube_Class:public Rectangle_Class
{
private:
    int height;
public:
    Cube_Class(int h)
    {
        height=h;
    }
    int getHeight()
    {
        return height;
    }
    void setHeight(int h)
    {
        height=h;
    }
    int volume()
    {
        return getLength()*getBreadth()*height;
    }
};

Rectangle_Class::Rectangle_Class()
{
    length=1;
    breadth=1;
}
Rectangle_Class::Rectangle_Class(int l,int b)
{
    length=l;
    breadth=b;
}
Rectangle_Class::Rectangle_Class(Rectangle_Class &r)
{
    length=r.length;
    breadth=r.breadth;
}
void Rectangle_Class::setLength(int l)
{
    length=l;
}
void Rectangle_Class::setBreadth(int b)
{
    breadth=b;
}
int Rectangle_Class::area()
{
    return length*breadth;
}

int main()
{
    Cube_Class c(8);
    c.setLength(12);
    c.setBreadth(9);
    cout<<“Volume is “<<c.volume()<<endl;
}

Conclusion:

In this article, I have explained the concept of Inheritance in C++. The C++ supports different types of inheritance including “multiple inheritance” (i.e., inheriting features from multiple base class or parent class). However, to make it simpler, I have only considered single inheritance here. I have shown three working examples to explain how we can use inheritance in C++ programming and re-use the code. Moreover, this is a very useful feature of C++.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article