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.
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.
The basic syntax to use Inheritance features in C++ is:
class Derived_Class: Access-Specifier Base_class
{
//body of the Derived 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;
}
Inheritance:
Info for t1: Triangle is isosceles Width and height are 4 and 4 Area is 8 Info for t2: Triangle is right Width and height are 8 and 12 Area is 48
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.
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:
Access | Public | Protected | Private |
---|---|---|---|
Same Class | Yes | Yes | Yes |
Derived Class | Yes | Yes | No |
Outside Class | Yes | No | No |