Hierarchical Inheritance
- The form of inheritance in
which more than one classes are derived from single base class is known as
hierarchical inheritance. We can implement the hierarchical inheritance by
defining at least three classes in which one base class and the remaining
two classes are derived classes. These two derived classes should be
derived the same base class.
- It is the inheritance
hierarchy wherein multiple subclasses are inherited from one base
class.
Diagram:
Example :
Syntax:
class base_class_name
{
private:
…………….
public:
…………….
};
class derived_class_name1 : public base_class_name
{
private:
……………
public:
……………
};
class derived_class_name2 : public base_class_name
{
private:
…………..
public:
…………..
};
Sample Program:
#include <iostream.h>
class Side
{
protected:
int l;
public:
void set_values (int x)
{
l=x;
}
};
class Square: public Side
{
public:
int sq()
{
return (l *l);
}
};
class Side
{
protected:
int l;
public:
void set_values (int x)
{
l=x;
}
};
class Square: public Side
{
public:
int sq()
{
return (l *l);
}
};
class Cube:public Side
{
public:
int cub()
{
return (l *l*l);
}
};
int main ()
{
Square s;
s.set_values (10);
cout << "The square value is::" << s.sq() << endl;
Cube c;
c.set_values (20);
cout << "The cube value is::" << c.cub() << endl;
return 0;
}
{
public:
int cub()
{
return (l *l*l);
}
};
int main ()
{
Square s;
s.set_values (10);
cout << "The square value is::" << s.sq() << endl;
Cube c;
c.set_values (20);
cout << "The cube value is::" << c.cub() << endl;
return 0;
}
Advantages:
- code re-usability.
- Saves time in program
development.
Disadvantages:
- Though object-oriented
programming is frequently program as an answer for complicated
project,inappropriate use of inheritance makes a program more complicated.
- Invoking member function
using object create more compiler overheads.
- In class hierarchy various
data elements remains unused,the memory allocated to them is not utilized.
Hierarchical Inheritance
Reviewed by NEERAJ SRIVASTAVA
on
4:14:00 PM
Rating:
No comments: