2.
Reusability is another important feature of oop
. It is nice if we could reuse something that
already exists rather than trying to create the
same all over again.
The mechanism of deriving a new class from an
old one is called inheritance.
The old class is referred to as the base class and
the new one is called the derived class or sub
class.
INTRODUCTION
3.
In inheritance, some of the base class data element and
member function are “inherited” into the derived class.
Base class
Derived class
5.
In this inheritance , a derived class is created from
a single base class. The class which inherits the
properties of another class is called derived or
child or subclass and the class whose properties
are inherited is called base or parent or super
class .
When a single class is derived from a single
parent class, it is called single inhertiance.
Single Inheritance:
12.
A class can inherit the attributes of two or more classes .
This is known as multiple inheritance.
Multiple inheritance allows us to combine the features
of several existing classes as a starting point for
defining new classes.
It like a child inheriting the physical features of one
parent and the intelligence of another.
A class can be derived from more than one parent.
Multiple inheritance:
15.
The class A serves as a base lass for thee
derived class B , which in turns serves as a base
class for the derived class C. The class B is
known as intermediate base class.
In c ++ programming, not only you can derive
a class from the base class but you can also
derive a class from the derived class. This form
of inheritance is known as multilevel
inheritance.
Multilevel Inheritance:
18.
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base class content.";
}
};
class B :
public A
{
};
Program For Multilevel Inheritance:
19.
class B :
public A
{
}
;
class C :
public B
{
};
int main()
{
C obj;
obj.display();
return 0;
}
20.
Hybrid inheritance is the combination of two or
more inheritance.
When you have a hybrid inheritance then a
Diamond problem may arise. In this problem a
Derived class will have multiple paths to a Base
class. This will result in duplicate inherited
members of the Base class
HYBRID INHERITANCE:
30.
Hierarchical Inheritance is used as a support to a
hierarchical design of a class program.
Many programming problems can be cast into a hierarchy
where certain features of one level are shared by many
others below that level.
The base class includes all the features that are common to
the sub class. A sub class can be constructed by inheriting
the features of base class and so on.
Hierarchical inheritance:
35.
#include <iostream>
#include <conio.h>
using namespace std;
class person /*Parent class*/
{
private:
char fname[100],lname[100],gender[10];
protected:
int age;
Program for hirarchical inheritance:
36.
public: void input_person();
void display_person();
};
class student: public person /*Child class*/
{
private:
char college_name[100];
char level[20];
public: void input_student();
void display_student();
};