0% found this document useful (0 votes)
10 views

Lecture 1 (Module 3)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lecture 1 (Module 3)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Module: 3

Inheritance
Inheritance

• The capability of a class to derive properties and characteristics from


another class is called Inheritance.

• Inheritance is a feature or a process in which, new classes are created


from the existing classes.

• The new class created is called “derived class” or “child class” and the
existing class is known as the “base class” or “parent class”.
Sub Class vs Super Class:
• Sub Class: The class that inherits properties from another class is
called Subclass or Derived Class.

• Super Class: The class whose properties are inherited by a subclass is


called Base Class or Superclass.
Why and when to use
inheritance?
• Consider a group of vehicles. You need to create classes for Bus, Car,
and Truck.
• The methods fuelAmount(), capacity(), applyBrakes() will be the same
for all three classes.
• If we create these classes avoiding inheritance then we have to write
all of these functions in each of the three classes as shown below
figure:
Why and when to use
inheritance?
Why and when to use
inheritance?
• The above process results in duplication of the same code 3 times.
• This increases the chances of error and data redundancy.
• To avoid this type of situation, inheritance is used.
• If we create a class Vehicle and write these three functions in it and
inherit the rest of the classes from the vehicle class, then we can
simply avoid the duplication of data and increase re-usability.
Why and when to use
inheritance?
Synta
x:

class <derived_class_name> : <access-specifier> <base_class_name>


{

//body

}
Modes of Inheritance:
• Public Mode: If we derive a subclass from a public base class. Then
the public member of the base class will become public in the derived
class and protected members of the base class will become protected
in the derived class.
• Protected Mode: If we derive a subclass from a Protected base class.
Then both public members and protected members of the base class
will become protected in the derived class.
• Private Mode: If we derive a subclass from a Private base class. Then
both public members and protected members of the base class will
become Private in the derived class.
Modes of Inheritance:
Types of Inheritance:
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
Single inheritance

• In single inheritance, a class is allowed to inherit from only one class.


i.e. one subclass is inherited by one base class only.

Syntax:
class subclass_name : access_mode base_class
{
// body of subclass
};
Single inheritance
Single Inheritance example 1:
#include<iostream>
using namespace std;

// base class
class Vehicle {
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};

// sub class derived from a single base classes


class Car : public Vehicle {

};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output:

This is a Vehicle
Single Inheritance example 2:
#include<iostream>
using namespace std;

class A
{
protected:
int a;

public:
void set_A()
{
cout<<"Enter the Value of A=";
cin>>a;

}
void disp_A()
{
cout<<endl<<"Value of A="<<a;
}
};
class B: public A
{
int b,p;

public:
void set_B()
{
set_A();
cout<<"Enter the Value of B=";
cin>>b;
}

void disp_B()
{
disp_A();
cout<<endl<<"Value of B="<<b;
}

void cal_product()
{
p=a*b;
cout<<endl<<"Product of "<<a<<" * "<<b<<" = "<<p;
}

};
int main()
{

B b;
b.set_B();
b.cal_product();

return 0;

}
OUTPUT:

Enter the Value of A=3


Enter the Value of B=4

Product of 3 * 4 = 12

You might also like