Escolar Documentos
Profissional Documentos
Cultura Documentos
CST-152
UNIT-2
Inheritance
Chapter- 5
Course Objectives
•To gain knowledge of Inheritance and its various types.
sub Class
Class C
University Institute of Engineering
Example of Single
#include <iostream.h>
Inheritance
class Rectangle: public Shape
// Base class {
class Shape public:
{ int getArea()
public: {
return (width * height);
void setWidth(int w)
}
{ };
width = w;
} int main(void)
void setHeight(int h) {
{ Rectangle Rect;
height = h;
} Rect.setWidth(5);
Rect.setHeight(7);
protected:
int width; // Print the area of the object.
int height; cout << "Total area: " << Rect.getArea() <<
}; endl;
return 0;
University Institute of Engineering Output:
} Total area: 35
Multilevel and
Hierarchical Inheritance
Multilevel Inheritance Hierarchical Inheritance
Class A Base
Class
Class B
sub sub sub
Class 3 Class 1 Class 2
Class C
University Institute of Engineering
Example of Multilevel
#include<iostream.h>
#include<conio.h> Inheritance
class publications void getdata()
{ {
private : publications::getdata1();
int book_no; cout<<"Enter author name";
public : cin>>author_name;
void getdata1() }
{ void show()
cout<<"\n"<<"Enter Book Number:"; {
cin>>book_no; cout<<book_no<<anthor_name;
} }
void putdata() };
{ class deri:public author
cout<<book_no<<endl; {
} };
};
class author: public publications void main()
{ {
private : deri d;
char author_name[20]; d.getdata();
public : d.putdata();
getch();
University Institute of Engineering }
Example of Hierarchical
#include<iostream.h>
class employee
Inheritance
void getdata()
{ {
private : employee::getdata();
int empno; cout<<"Enter Basic Salary \n";
char empname[20]; cin>>basic;
public : }
void getdata(){
cout<<"\n Enter Employee No"; void putdata()
cin>>empno; {
cout<<"\n Enter Employee name "; da=0.30*basic;
cin>>empname;} hra=0.15*basic;
void putdata(){ cca=0.05*basic;
cout<<"\n Employee No"<<empno; pf=0.12*basic;
cout<<"\n Employee name "<<empname;} special_allowances=0.4*basic;
}; salary=basic+da+hra+cca+special_allowances-pf;
class manager :public employee employee::putdata();
{ cout<<endl<<salary;
private : }};
float basic; class supervisor : public employee
public : {
float salary; private :
float da; float basic;
float hra; public :
float cca; float salary;
float pf; float da;
float special_allowances; float hra;
float cca;
float pf;
University Institute of Engineering float special_allowances;
Cont..
void getdata()
void getdata()
{
{
employee::getdata();
employee::getdata();
cout<<"Enter Basic Salary \n";
cout<<"\n Enter wages & incentives";
cin>>basic;
cin>>wages>>incentive;
}
}
void putdata()
void putdata()
{
{
da=0.30*basic;
salary=wages+incentive;
hra=0.15*basic;
cca=0.05*basic;
employee::putdata();
pf=0.12*basic;
special_allowances=0.4*basic;
cout<<endl<<salary;
salary=basic+da+hra+cca+special_allowances-
}
pf;
};
employee::putdata();
void main()
cout<<endl<<salary;
{
}
manager m;
};
supervisor s;
class worker :public employee
worker w;
{
clrscr();
public :
m.getdata();
float salary;
s.getdata();
float wages;
w.getdata();
float incentive;
w.putdata();
University Institute of Engineering s.putdata(); }
Hybrid Inheritance
•Hybrid is nothing but the combination of Multilevel and
multiple Inheritance
Lecturer
Multi level
Inheritance
Marks Department
Student
Multiple
Inheritance
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 :
//members;};
• https://www.cse.unr.edu/~bebis/CS308/PowerPoint/Inheritance.
ppt
• https://cs.wmich.edu/~rhardin/Spring04/cs112/lecturespecial.ppt
• www.cs.cityu.edu.hk/~lwang/ccs5253/lab3.ppt
• www.utdallas.edu/~john.cole/CS1Slides/CS1Lesson15-
Inheritance.pptx