View and vote on the article here: Practical C++ Part 11: Inheritance
Practical C++ Part 11: Inheritance| Category | | | Summary | | | Body | Practical C++ Part 11: Inheritance
This part of the "Practical C++" series deals with the concept of Inheritance, which is a key feature of Object Oriented Programming. Inheritance simply means one class deriving or inheriting the properties of another class. Here the parent class is known as 'base class' whereas the child class which gets the properties is known as 'derived class'. The properties that the derived class gets is the public data members and member functions.
Let's take an example to better understand the concept of inheritance :-
class automobile
{
public :
float price;
int tyres;
void purchase();
};
class car : public automobile
{
int seats;
public :
void paint;
}
class truck : public automobile
{
int capacity;
public :
void build();
}
The base class 'automobile' has two data members - price and tyres, since every automobile has these two (unless its a hovercraft ;-) ). The two derived classes will inherit both the data members and the member function 'purchase()'. Notice that all these are in public mode. Anything in private mode doesn't get inherited.
If you want to inherit certain members but also want them to be hidden (as in private), there is a separate mode called 'protected' mode. Members in the protected mode cannot be directly accessed by the object but can be inherited. Let us take an example to clarify this :-
class abc
{
private :
int a;
protected :
int b;
public :
int c;
}
class xyz : public xyz
{
public :
int x;
}
Now derived class 'xyz' has three members - x, b anc c. 'b' in this case will remain protected in the derived class also.
This leads to the concept of inheritance mode. So far while inheriting classes we used "class derived : public base", which means a public inheritance mode. In this mode all members which are inherited remain of the same visibility in the derived class. For example, 'b' in the above code remained protected. In private inheritance mode, all inherited members become private members of the derived class. Similarly, in protected inheritance mode, they become protected members of the derived class.
References :-
Written by Rae (30 January 2005)
Rae is a member of CAU Knowledge-Bank Tutorial Writers |
|
This article was imported from the CyberArmy University site. (original author: Rae)
There are no replies to this post yet.
|