Topic :Inheritance in C++ Programming Language:



WHAT IS INHERITANCE?

Inheritance is where Object-Oriented Programming really comes into its own. Understanding this most important concept is the key to effective C++ Programming and the goal of this Tutorial. There's no going back now. after you've completed this part, you can call yourself an Object-Oriented-Programmer. so to understand the concept of Inheritance, let's take an real life example in our tutorial.



image: real example of inheritance

Inheritance is why we look similar to our Mom or Dad. People's bodies have characteristics, like the shape of their nose or the color of their eyes, and parents pass on these characteristics to their children, this is inheritance.

We are made of cells like a building is made of bricks. Our cells are told what to do by DNA. DNA is the boss and tells the cells how to make a human body. All of us are made up of DNA from both our Mom and dads so we inherit orders on how to build a body from both of them that is why we have characteristics from both our parents.

People look like People. Every Person is Unique but they have far more in common than they are different. This is Because we all are related and we have all inherited characteristics from our ancestors. This is all Inheritance. and Now We can hope that you Understand the concept of Inheritance, So Let's move to our Programming World.



Inheritance In C++:

The process of Obtaining the data Members and Methods from one class to another class is known as Inheritance. It is one of the fundamental features of object-oriented programming. Inheritance is based on the principle that knowledge of a general category can be applied to more specific objects. Specifically, inheritance in C++ means we can create classes that derive their attributes from existing classes. in other words, a newly created class can absorb all the data fields and functions of a class already being used. Inheritance save our time because we don't have to start form scratch every time we want to create a class. instead, when we use inheritance to create a class, we expand on an existing class.

The Class whose Properties are inherited by other class is called the Parent or Base or Super class. And the class which inherits properties of other class is called Child or Derived or Sub class.


We will use Base and Derived name in our tutorials.


The basic syntax to use Inheritance features in C++ is:


class Derived_Class: Access-Specifier Base_class
{
    //body of the Derived class.
}

  • In Above Syntax:

    » Class is the keyword to declare class.
    » Derived_Class is the class which inherits properties of other class.
    » Access-Specifier: Defines the Scope of a Class members. A Class member can be variable or method. In C++ There are three type of Access Specifiers.

    Access SpecifierDefinition:
    Public: The Class Member that is define as Public can be accessed by other Class Member that is initialized Outside the Class. A Public member can be accessed from Anywhere even outside the Class.
    Private: The Class Member that is define as Private, can't be accessed from outside thee Class. The private Access specifiers restrict the Member Variable or Method to be called outside from the Parent class.
    Protected:The Protected access specifier hides its member method and variables from other classes. this type of Variable or methods can only be accessed in Child Class and friend class. If We want to Inherit some Properties or Method of once class to another, then we declare our method or variable as protected.

    » Base_class is the class whose Properties are inherited by other class.



Let's have an simple example to understand the concept of Inheritance. In this example, we will create a base class called TwoD_Shape that stores the width and height of two-dimensional object. and a derived class called Triangle which will inherit the properties of TwoD_Shape class. pay Close Attention to the way of Declaring the classes.


/*Example - Inheritance - InfoBrother*/
 
#include<iostream>
#include<cstring>
using namespace std;
 
class TwoD_Shape //Base Class:
{ 
   public: //Public Members:
	double width;
	double height;
 
   //this function will Show dimensions:
	void showDim() 
	{
    	   cout<<" Width and height are " <<width<< " and " 
				          <<height<<endl;
	}
};
 
/*Base Class(i.e. TwoD_Shape) is Inherited by 
  derived class(i.e. Triangle)*/
class Triangle: public TwoD_Shape 
{
   public:
       char style[20];
       double area()
       {
       	 //using Public Members of Base Class.
           return width * height / 2;
       }
 
        void showStyle()
        {
            cout<<" Triangle is " <<style<<endl;
        }
};
 
main()
{
	//Two objects Created having type Triangle.
    Triangle t1, t2; 
 
    t1.width=4.0;
    t1.height=4.0;
    //string copy function. (use to assign string.)
    strcpy(t1.style, "isosceles"); 
 
    t2.width=8.0;
    t2.height=12.0;
    //string copy function. (use to assign string.)
    strcpy(t2.style, "right");
 
    cout<<" Info for t1: "<<endl; //1st object Information.
    t1.showStyle();
    t1.showDim();
    cout<< " Area is "<<t1.area()<<endl;
 
    cout<<" \n Info for t2: "<<endl; //2nd object Information.
    t2.showStyle();
    t2.showDim();
    cout<<" Area is "<<t2.area()<<endl;
 
    return 0;
}


In Above Example, We create Base Class TwoD-Shape. This shape contain some properties and methods. We create another Class Traingle. This class inherit the methods of Base class. The following line of code shows, how Triangle Class inherits TwoD-Shape Class.


class Triangle: Public TwoD-Shape


Here, TwoD_Shape is a base class that is inherited by Triangle, which is derived class. As this example shows, the Syntax for inheriting a class is remarkably simply and easy to use. Because Triangle includes all of the members of its base class, TwoD_shape, it can access width and height inside area(). also inside main(), objects t1 and t2 can refer to width and height directly, as if they were part of Triangle.



Even Through TwoD_shape is a base for Triangle, it is also a completely independent, stand-alone class. Being a base class for a derived class does not mean that the base class cannot be used by itself.





image: Inheritance, Base and Derive Class.

Member Access and Inheritance:

A Derived Class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member function of derived classes should be declared private in the base class. We can summarize the different access types according to who can access them, in the following way:



AccessPublicProtectedPrivate
Same Class YesYesYes
Derived ClassYesYesNo
Outside ClassYesNoNo


  • A Derived Class can inherits all base class Public Methods except the following:
    » Constructor, Destructor and copy Constructor of the base class.
    » Overloaded Operators of the base class.
    » The Friend function of the base class.



  • Advantages of Inheritance:

    If we Develop Any Application using the concept of Inheritance, The Application will have the Following Advantages:
    » Development of Application will take less time.
    » Application will assume less Memory.
    » Execution time of Application will be less.
    » Enhance Application Performance.
    » Repetition of the code will reduce so that we get consistence results and less storage cost.













I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement