Introduction
Inheritance (means attributes passing) is one of the five component's of OOP's concept,which is the basis of C++. In other words, it is the OOP's concept responsible for the
development of C++ out of C. This concept is used in making classes which is again the
basis of the development of OOP's concept.
Definition
Inheritance is a technique in C++ which is used to make child or friend classes out of previously existing classes in C++. As the new classes are called child
classes, these classes already inherit features of parent classes i.e. they will show each
& every behavior shown by its parent class in addition with some new features needed for
the new task. But inheritance is also not so easy to implement because misplacing of just
one line will rise problems in program which would cause loss of time & money.
The idea was adopted when there was felt a need of function whose coding will be a bit
different from that of a previously defined function. Although the best alternative is that
one should modify the pre-existing function for the new task but than the concept of data
reusability which is one of the most important feature of C++ will be voilated. In other
words, one should not be able to use the previous function again.
e.g- Rectangle & triangle belong to family of polygons, i.e. they have all the attributes
of a polygon like they have sides and etc..
Here rectangle and triangle can be thought as chlid classes of parent class polygon.
Although they have the features of their parent but they will also have their own attributes
like their own set of angles.
Below is a general syntax of inherited and parent classes assumming that the syntax of the
class is known -
class Animal
{
public:
Animal();
~Animal();
void eat();
void sleep();
void drink();
private:
int legs;
int arms;
int age;
};
//This class Animal contains information and functions related to all animals.
class Cat : public Animal
{
public:
int fur_color;
void purr();
void fish();
void markTerritory();
};
//But the above class wil only contain the information and functions realated to cat only.
In above example the first class is a parent class but the second class is the inherited or
the child class.
Now as you can see a 'public' word in the inherited class. What's this?
This is again an important component of OOP's concept ABSTARCTION which means data hiding
which is used to ignore a problem called data insecurity which existed in C but not C++.
A child class can be inherited in 3 ways by the parent class, i.e publically, in protected
mode and privately.
If a class is publically derived from an pre-defined class, all the objects and functions of
the parent class will be accessible by child class and by the outside world through the
functions & objects of the child class.
If it is derived protectedlly, the child class only can access the objects and functions of
the parent class directly but they can't be accessed by the outside world.
If it is derived privately, the child class can access the parent
No comments:
Post a Comment