1. Inheritance
09/04/131 VIT - SCSE
By
G.SasiKumar., M.E., (Ph.D).,
Assistant Professor
School of Computing Science and Engineering
VIT University
2. 09/04/132 VIT - SCSE
It is the Process of creating a New class from an existing class.
The Existing class is called Base or Parent class.
The New class is called as Child or Derived Class
Inheritance
base
subclass1 subclass2
Notation:
3. 09/04/133 VIT - SCSE
Advantages
It permits code reusability. So, save time and increase
the program reliability.
Improve Program Reliability
It Permits code sharing
4. 09/04/134 VIT - SCSE
class <derived classname> : public
<base classname>
{
---
----
};
Syntax for Inheritance
7. Single Inheritance
A class can be derived from a single base
class is called single inheritance
Base Class
sub Class
Multiple Inheritance
Class A Class B
Class C
8. Class A
Class B
Class C
Multilevel Inheritance
Hierarchical Inheritance
Base Class
sub Class 1sub Class 3 sub Class 2
9. Hybrid Inheritance
Hybrid is nothing but the combination of
Multilevel and multiple Inheritance
Lecturer
Marks Department
Student
Multi level
Inheritance
Multiple
Inheritance
10. Function Type
Access directly to
Private Protected Public
Class member Yes Yes Yes
Derived class
member
No Yes Yes
Friend Yes Yes Yes
Friend class
member
Yes Yes Yes
11. //Example for Single inheritance
#include<iostream.h>
class test
{
public:
int a,b;
void getdata();
void display();
};
class sample:public test
{
public:
int x,y;
void getinfo();
void dis();
void sum();
};
void test::getdata()
{
cout<<"Enter a & b:"<<endl;
cin>>a>>b;
}
void test::display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
}
void sample::getinfo()
{
cout<<"Enter x & Y:";
cin>>x>>y;
}
void sample::dis()
{
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
void sample::sum()
{
int s;
s=a+b+x+y;
cout<<"Sum="<<s;
}
void main()
{
sample s;
s.getdata();
s.display();
s.getinfo();
s.dis();
s.sum();
getch();
}
12. It is the process of creating new class from more than one base
classes.
Syntax :
class <derived class >:<access specifier>
base_class1,<access specifier> base_class2...
{
private :
// members;
protected :
// members;
public :
//memebers;
};
13. #include<iostream.h>
class base1
{
protected :
int v1;
public :
void disp_base1() {
cout<<“v1 "<<v1<<endl;
}};
class base2
{
protected :
int v2;
public :
void disp_base2()
{
cout<<“v2 is"<<v2<<endl;
}};
class deri:public base1,public
base2
{
private:
int v3;
public :
deri(int a,int b,int c)
{
v1=a;
v2=b;
v3=c;
}
void disp_me()
{
cout<<“v3 is"<<v3;
} };
17. //Example for Hierarchical inheritance
#include<iostream.h>
#include<conio.h>
class test
{
public:
int a,b;
void getdata()
{
cout<<"Enter a & b:"<<endl;
cin>>a>>b;
}
};
class simple:public test
{
public:
void display()
{
cout<<"a="<<a<<endl;
cout<<"b="<<b;
}
};
class sample:public test
{
public:
void sum()
{
int sum;
sum=a+b;
cout<<"sum="<<sum;
}
};
void main()
{
simple s1;
sample s2;
s1.getdata();
s1.display();
s2.getdata();
s2.sum();
getch();
}
18. HYBRID
INHERITANCE
#include<iostream.h>
class lecturer
{
private :
char lecturer_name[20];
public :
void getdata()
{
cout<<"Enter Lecturer name";
cin>>lecturer_name;
}
void putdata()
{
cout<<"lecturer name is
"<<lecturer_name;
}
};
class department : public lecturer
{
private :
char department_name[20];
public:
void getdata()
{
cout<<"Enter Department name";
cin>>department_name;
}
void putdata()
{
cout<<"Department name is "<<department_name;
}
};
class marks
{
public :
int mark1;
int mark2;
int mark3;
void getdata()
{
cout<<"Enter the marks for 3 subjects";
cin>>mark1>>mark2>>mark3;
}
void putdata()
{
cout<<"The marks for 3 subjects are
"<<mark1<<"t"<<mark2<<"t"<<mark3;
}
};
class student:public department,public marks
{
private:
int roll_no;
20. 09/04/1320 VIT - SCSE
1. The derived class need not have a constructor as long as
the base class has a no-argument constructor.
2. However, if the base class has constructors with
arguments (one or more), then it is mandatory for the
derived class to have a constructor and pass the
arguments to the base class constructor.
3. When an object of a derived class is created, the
constructor of the base class is executed first and later the
constructor of the derived class.
Constructors in Derived Classes