0% found this document useful (0 votes)
5 views5 pages

Inheritance

Inheritance is a key concept in Object-Oriented Programming (OOP) that allows a child class to inherit properties and methods from a parent class, enhancing code reusability and maintainability. There are five main types of inheritance: single, multiple, multilevel, hierarchical, and hybrid, each serving different structural needs in software design. Examples include user management systems where classes like Admin and Customer inherit from a common User class, and university systems where a Professor inherits capabilities from both Teacher and Researcher classes.

Uploaded by

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

Inheritance

Inheritance is a key concept in Object-Oriented Programming (OOP) that allows a child class to inherit properties and methods from a parent class, enhancing code reusability and maintainability. There are five main types of inheritance: single, multiple, multilevel, hierarchical, and hybrid, each serving different structural needs in software design. Examples include user management systems where classes like Admin and Customer inherit from a common User class, and university systems where a Professor inherits capabilities from both Teacher and Researcher classes.

Uploaded by

habibamushtaq142
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance

Inheritance is a fundamental concept in Object-Oriented Programming (OOP). It allows one


class to reuse the properties and methods of another class. This improves code reusability,
modularity, and maintainability, which is why inheritance is widely used in real software
systems like banking apps, management systems, and game engines.
Inheritance means creating a new class (child/derived class) from an existing class (parent/base
class).
• Parent / Base Class → The class whose properties and methods are inherited.
• Child / Derived Class → The class that inherits from the parent class.
Basic Syntax
class Parent
{
};
class Child : public Parent
{
};
Here the Child class can access the functions and data members of the Parent class.
Types of Inheritance in OOP
There are five main types of inheritance:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance
1. Single Inheritance

Single inheritance occurs when one child class inherits from only one parent class.

Parent

Child
Basic Structure

class Parent

};

class Child : public Parent

};
#include<iostream>
using namespace std;
class parent
{
public:
int n;
void in()
{
cout<<"You entered number";
cin>>n;
}
};
class child:public parent //derive or inherit
{
public:
void out()
{
cout<<"Your entered number is"<<n;
}
};
main()
{
child c;
[Link]();
[Link]();
}
Real Software Example

User Management System

Parent Class:

User

Child Classes may include:

• Admin
• Customer
• Employee

All users share common properties like:

• name
• email
• login()

But each child class can have additional features.

2. Multiple Inheritance

Multiple inheritance occurs when a child class inherits from more than one parent class.

Parent A Parent B

Child

Basic Structure

class A

{ };

class B

{ };

class C : public A, public B, public D

{ };
#include<iostream>
using namespace std;
class Teacher
{
public:
void teach()
{
cout<<"Teaching students"<<endl;
}
};
class Researcher
{
public:
void research()
{
cout<<"Doing research"<<endl;
}
};

class Professor : public Teacher, public Researcher


{
};

int main()
{
Professor p;
[Link]();
[Link]();
}

Real Software Example

University Management System

A Professor may have two roles:

• Teacher
• Researcher

Structure:

Teacher Researcher

Professor
The Professor class inherits both teaching and research capabilities.

You might also like