There will be concept
of parent and child.
→Child having the
access of property of parents
Types
1. Single inheritance
2. Multiple inheritance
3. Multilevel inheritance
4. Hybrid inheritance
E.g.:
//single inheritance
#include<iostream.h>
#include<conio.h>
//base class
class student
{
public:
void display()
{
cout<<"base
or parent clas"<<endl;
}
};
//child class
class teacher: public
student
{
public:
void display1()
{
cout<<"child
class"<<endl;
}
};
void main()
{
clrscr();
teacher t; //child class object create
t.display(); //call the base class method
t.display1(); //call the child class method
getch();
}
E.g.: -
//multiple
inheritance
#include<iostream.h>
#include<conio.h>
class student //base class
{
public:
void display()
{
cout<<"base
class"<<endl;
}
};
class teacher //second base class
{
public:
void display1()
{
cout<<”
second base class"<<endl;
}
};
class collegemember :
public teacher, public student
//child class,inherit
the student & teacher base class
{
public:
void display2()
{
cout<<"multiple
inheritance example"<<endl;
}
};
void main()
{
clrscr();
collegemember c; //child class object
creates
c.display(); //call
the first base class method
c.display1(); //call the second base class
method
c.display2(); //call the child class
method
getch();
}
E.g:
//Multilevel Inheritance
//Multilevel Inheritance
#include<iostream.h>
#include<conio.h>
class student //base class
{
public:
void display()
{
cout<<"base
class"<<endl;
}
};
class teacher: public
student //child
class(inherit the student base class)
{
public:
void display1()
{
cout<<"child
class"<<endl;
}
};
class college: public
teacher //child class
(inherit the teacher base class)
{
public:
void display2()
{
cout<<"child
class “<<endl;
}
};
void main()
{
clrscr();
college c; //create
last child class object
c.display(); //call the
student class method
c.display1(); //call the teacher class method
c.display2(); //call the
college class method
getch();
}
4. Hybrid inheritance: -
E.g.:
//Hybrid Inheritance
#include<iostream.h>
#include<conio.h>
class student //first
base class
{
public:
void display()
{
cout<<"first
base class"<<endl;
}
};
class teacher //second
base class
{
public:
void display1()
{
cout<<"second
base class "<<endl;
}
class college: public
teacher ,public student //child class (inherit the student & teacher base class)
{
public:
void display2()
{
cout<<"child
class (inherit two base classes)"<<endl;
}
};
class company: public
college //child class (inherit the college base
class)
{
public:
void display3()
{
cout<<"child
class (inherit the base class)"<<endl;
}
};
void main()
{
clrscr();
company x; //create
last class object
x.display();
//call base class(student)
method
x.display1(); //call base class(teacher) method
x.display2(); //call child class(college)
method
x.display3(); //call child class(company) method
getch();
}
No comments:
Post a Comment