C++ 33: Inheritance

 Inheritance Introduction:

1. Reusability of existing classes without disturbing the existing classes 
    is called inheritance.
2. By using inheritance we can drive new class form an existing class.
3. In this case the existing class is called as base class/parent class/
    super class and newly created class as derived class/child class/sub
    class.
4. By using inheritance we can created new class versions as base class of 
    the old c lasses by adding some new features. Also we can create new 
    classes which will represent new entity which is partially same as previous class.
5. The benefit of inheritance is that the derived class inherits some or all 
    the features(i.e of code ) of the base class from which is derived. This
6. That means we don't need to write or the previous functionalities in the
    derived classes
7. In short Inheritance is the process of passing on the Childs
8. Inheritance represents IS-A relationship.


Existing Class--> Super class/Parent Class / Base Class
New Class --> Sub Class / Child Class / Derived Class



Types of Inheritance:
  • Single Inheritance.
  • Multiple Inheritance.
  • Hierarchical Inheritance.
  • Multilevel Inheritance.
  • Hybrid Inheritance (also known as Virtual Inheritance)

Inheritance is also called as deviation.

is-a relationship

Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present between the two classes.

Here are some examples:

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

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 want to hide the data of a class, but still want that data to be inherited by its derived classes.

To learn more about protected, refer to our C++ Access Modifiers tutorial.

Access Modes in C++ Inheritance

In our previous tutorials, we have learned about C++ access specifiers such as public, private, and protected.

So far, we have used the public keyword in order to inherit a class from a previously-existing base class. However, we can also use the private and protected keywords to inherit classes. For example,

class Animal {
    // code
};

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

The various ways we can derive classes are known as access modes. These access modes have the following effect:

  1. public: If a derived class is declared in public mode, then the members of the base class are inherited by the derived class just as they are.
  2. private: In this case, all the members of the base class become private members in the derived class.
  3. protected: The public members of the base class become protected members in the derived class.

The private members of the base class are always private in the derived class.



Examples on inheritance:

Design a class distance with data members Feet and Inches. Define member
functions setdata() & get Data() Derive a  new class MyDistance from the class
Distance with additional member function convert() which will convert the data
of MyDistance object into inches (1 Feet = 12 Inches).


#include <iostream>
using namespace std;

class Distance
{
protected:
    int FeetInches;

public:
    void setdata(int f, int i)
    {
        Feet = f;
        Inches = i;
    }
    void display()
    {
        cout << "Distance is " << Feet << " and " << Inches << " inches" << endl;
    }
};
class MyDistance : public Distance
{
public:
    void convert()
    {
        Inches += Feet * 12;
        cout << "Distance inches is " << Inches << endl;
    }
};
int main()
{

    MyDistance a;
    a.setdata(27);
    a.display();
    a.convert();
    return 0;
}

Output:

Distance is 2 and 7 inches
Distance inches is 31

Design a class Cylinder with data -member radius and height .Definemember 
functions setDtaa() and volume(). Derive a class MyCylinder freom the class 
Cylinder with an additional memeber function surfacearea().
#include <iostream>
using namespace std;

class Cylinder
{
protected:
    int radiusheight;

public:
    void setdata(int r, int h)
    {
        radius = r;
        height = h;
    }
    void volume()
    {
        cout << "Volume is " << 3.14 * radius * radius * height << endl;
    }
};

class MyCylinder : public Cylinder
{
public:
    void surfacearea()
    {
        cout << "Surface area is " << 2 * (3.14 * radius * radius+ (2 * 3.14 * radius* height << endl;
    }
};

int main()
{
    MyCylinder a;
    a.setdata(210);
    a.volume();
    a.surfacearea();

    return 0;
}

Output:

Enter your age: 56
You can vote

Method Resolution Order (MRO):
Whenever a memeber_function is called with derived class object then frist that member function will search in the derived class, if not found then it will be search in its immediate base class and so on.


Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected