0% found this document useful (0 votes)
9 views21 pages

INHERITANCE

The presentation discusses inheritance in object-oriented programming, defining it as the acquisition of properties and behaviors from a parent class to a child class. It outlines five types of inheritance: single, multiple, multilevel, hybrid, and hierarchical, providing examples and code snippets for each type. The document concludes with a thank you note.

Uploaded by

nikitamahadik182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views21 pages

INHERITANCE

The presentation discusses inheritance in object-oriented programming, defining it as the acquisition of properties and behaviors from a parent class to a child class. It outlines five types of inheritance: single, multiple, multilevel, hybrid, and hierarchical, providing examples and code snippets for each type. The document concludes with a thank you note.

Uploaded by

nikitamahadik182
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

“Presentation on

Inheritance and their


types”
 Presentation by :-

Names:
1. Nikita Mahadik
2. Lakshmi Sathe
3. Shabhnam Jaiswal
4. Vaishnavi Rathod
INHERITAN
CE
 Inheritance :
Acquiring properties and behaviors from parents class to child class.
Parent class :- ‘super class ’
T
Child class :- ‘sub class ’

 Types of inheritance :

• Single inheritance
• Multiple inheritance
• Multilevel inheritance
• Hybriding inheritance
• Hierarchical inheritance
 Single Inheritance

 Only one class is derived from another class.

Class A
(parent class)

Class B
(Child class)
 Program

#include<iostream.h>
#include<conio.h>
Class Data
{
Protected:
Int a,b;
Public:
Void read()
{
Cout<<“Enter two numbers:”;
Cin>>a>>b;
}
};
Class Sum: public Data
{
Int sum;
Public:
Void add()
{
Sum=a+b;
}
};
Void display()
{
Cout<<“The sum is”<<sum;
}
};
Void main()
{
clrscr();
Sum s;
s.read();
s.sum();
s.display();
getch();
}

OUTPUT:-

Enter two numbers:


4
5
The sum is 9
 Multiple Inheritance

 One class is derived from multiple classes, called as multilevel.

Class A Class B
(parent class) (parent class)

Class C
(Child class)
 Program

#include<iostream.h>
#include<conio.h>
Class teacher
{
Protected:
Char name[20];
int empid;
};
Class student
{
Protected:
Char sname[30];
int rollno;
};
Class info: public teacher , public student
{
Public:
Void accept()
{
Cout<<“Enter teacher name, employee id, student name and roll_no”:
Cin>>name>>empid>>sname>>rollno;
}
Void display()
{
Cout<<“Teacher name:”<<name<<endl<<“Employee id:”<<empid<<endl<<“Student name:”
<<sname<<endl<<“Roll No:”<<rollno;
}
};
Void main()
{
Info n;
clrscr();
n.accept();
n.display();
}
 Multilevel Inheritance

 One class is derived from another derived class.

Class A
(base class)

Class B
(inherited class)

Class C
(inherited class)
 Program

#include<iostream.h>
#include<conio.h>
Class manufacture
{
Protected:
Char car_name[20];
};
Class carmodel:public manufacture
{
int model_no;
Char model_name[20];
};
Class car:public car_model
{
Char color[20];
int no;
Public:
Void read()
{
Cout<<“Enter the car manufacturer name, model name, model number, color, number”;
Cin>>car_name>>model_n model_name>>model_no>>color>>no;
}
Void display()
{
Cout<<“car manufacturer name:”<<car_name<<endl;
Cout<<“Model name:”<<model_name<<endl;
Cout<<“Model number:”<<model_no<<endl;
Cout<<“Color:”<<color;
Cout<<“Number:”<<no<<endl;
}
};
Void main()
{
car c;
c.read();
c.display();
getch();
}
 Hybrid inheritance

 Combining various types of inheritance like multiple, multilevel, etc.

Class A
(students)

Class B Class C
(test) (sports)

Class D
(result)
 Program

#include<iostream.h>
#include<conio.h>
Class student
{
Protected:
Char name[20];
int roll_no;
};
Class test :public student
{
Protected:
int marks;
Public:
Void read()
{
Cout<<“Enter name, roll number, marks obtained:”;
Cin>>name>>roll_no>>marks;
}
};
Class sports()
{
Protected :
int score;
Public:
Void read()
{
Cout<<“\n 1. student has won in national sports event. \n 2. students has not won in national sports
event. \n Enter your choice:”;
Cin>>score;
}
};
Class result: public test, public sports
{
int total;
Public:
Void calculate()
{
If(score==1)
total= marks+15;
Else
total= marks;
}
Void display()
{
Cout<<“The total is”<<total;
}
};
Void main()
{
clrscr();
result r;
r.test::read();
r.sports::read();
r.calculate();
r.display();
getch();
}
 Hierarchical inheritance

 It is a hierarchy of classes ,i.e. there is a single base class and multiple derived classes. It forms tree
like structure.

Class A
(student)

Class B Class C
(polytechnic) (engineer)
 Program

#include<iostream.h>
#include<conio.h>
Class student
{
Protected:
Char name[20];
int roll_no;
float per;
Public:
Void accept()
{
Cout<<“Enter roll no, name, percentage:”;
Cin>>roll_no>>name>>per;
}
};
Class polytechnic: public student
{
int collegeid;
char collegename[20];
Public:
Void getdata()
{
Cout<<“Enter college id and name”;
Cin>>collegeid>>collegename;
}
void display()
{
Cout<<“Roll no:”<<roll_no;
Cout<<“Name:”<<name;
Cout<<“percentage:”<<per;
Cout<<“college id:”<<collegeid;
Cout<<“college name:”<<collegename;
}
};
Class engineer: public student
{ int collegeid;
char collegename[20];
Public:
Void getdata1()
{
Cout<<“Enter college id and name:”
cin>>collegeid>>collegename;
}
Void show()
{
Cout<<“Roll no:”<<roll_no;
Cout<<“Name:”<<name;
Cout<<“Percentage:”<<per;
Cout<<“College id:”<<collegeid;
Cout<<“College name:”<<collegename;
}
};
Void main()
{
polytechnic p;
engineer e;
p.accept();
p.getdata();
e.accept();
e.getdata1();
p.display();
e.show();
}
THANK YOU…..

You might also like