Inheritance

3. 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 is base class and the remaining two classes is derived from the same one base class.Hierarchical Inheritance. Hierarchical Inheritance is that in which a Base class has many sub classes or when a Base class is used or inherited by many sub classes.
#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 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;
}
Result:
The square value is:: 100
The cube value is::8000
4. Multilevel Inheritance
Multilevel Inheritance is a method where a derived class is derived from another derived class.When a class is derived from another derived class is called multilevel inheritance. It is implemented by defining at least three classes. In multilevel inheritance, there is one base class and the remaining two is derived class.
In Multilevel Inheritance a derived class can also inherited by another class. For instance in the program below we have a base class and two derived classes. We are inheriting Base class in Derived1 class and inheriting Derived1 class in Derived2 class. So when a derived is inherited by another class then it creates Multiple Levels and this is know an Multi-level Inheritance. #include < iostream.h >
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout << "Roll Number Is:\n"<< rollno << "\n"; }
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class res : public marks
{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2;
put_num();
put_marks();
cout << "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(5);
std1.get_marks(10,20);
std1.disp();
return 0;
}
Result:
Roll Number Is:
5
Subject 1: 10
Subject 2: 20
Total: 30
5. Hybrid Inheritance
"Hybrid Inheritance" is a method where one or more types of inheritance are combined together and used.When a derived class inherits from multiple base classes and all of its base classes inherits from a single base class.
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes. In this example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:
#include < iostream.h >
class mm
{
protected:
int rollno;
public:
void get_num(int a)
{ rollno = a; }
void put_num()
{ cout << "Roll Number Is:"<< rollno << "\n"; }
};
class marks : public mm
{
protected:
int sub1;
int sub2;
public:
void get_marks(int x,int y)
{
sub1 = x;
sub2 = y;
}
void put_marks(void)
{
cout << "Subject 1:" << sub1 << "\n";
cout << "Subject 2:" << sub2 << "\n";
}
};
class extra
{
protected:
float e;
public:
void get_extra(float s)
{e=s;}
void put_extra(void)
{ cout << "Extra Score::" << e << "\n";}
};
class res : public marks, public extra{
protected:
float tot;
public:
void disp(void)
{
tot = sub1+sub2+e;
put_num();
put_marks();
put_extra();
cout << "Total:"<< tot;
}
};
int main()
{
res std1;
std1.get_num(10);
std1.get_marks(10,20);
std1.get_extra(33.12);
std1.disp();
return 0;
}
Result:
Roll Number Is: 10
Subject 1: 10
Subject 2: 20
Extra score:33.12
Total: 63.12


Free Web Hosting