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

inherit 2

Oop

Uploaded by

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

inherit 2

Oop

Uploaded by

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

Object Oriented Programming

Week 7

Lecture 1 & 2

Inheritance:
One of the most important concepts in object-oriented programming is that
of inheritance. Inheritance allows us to define a class in terms of another class, which makes it
easier to create and maintain an application. This also provides an opportunity to reuse the code
functionality and fast implementation time.

Inheritance is a mechanism in which one class acquires the property of another class. For
example, a child inherits the traits of his/her parents. With inheritance, we can reuse the fields
and methods of the existing class. Hence, inheritance facilitates Reusability and is an important
concept of OOPs.

The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object Oriented
Programming.
Sub Class/Derive Class: The class that inherits properties from another class is called Sub
class or Derived Class.
Super Class/Base Class: The class whose properties are inherited by sub class is called
Base Class or Super class.

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 same for all of the 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 in below figure:

Page 1 of 15
Object Oriented Programming

You can clearly see that above process results in duplication of 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. Look at the below diagram in which the three classes are inherited from
vehicle class:

Implementing inheritance in C++: For creating a sub-class which is inherited from the
base class we have to follow the below syntax.
Syntax:
class subclass_name : access_mode base_class_name

Page 2 of 15
Object Oriented Programming

//body of subclass

};

Here, subclass_name is the name of the sub class, access_mode is the mode in which you want
to inherit this sub class for example: public, private etc. and base_class_name is the name of
the base class from which you want to inherit the sub class.
Note: A derived class doesn’t inherit access to private data members. However, it does inherit
a full parent object, which contains any private members which that class declares.

// C++ program to demonstrate implementation

// of Inheritance

#include <bits/stdc++.h>

using namespace std;

//Base class

class Parent

public:

int id_p;

};

// Sub class inheriting from Base Class(Parent)

class Child : public Parent

Page 3 of 15
Object Oriented Programming

public:

int id_c;

};

//main function

int main()

Child obj1;

// An object of class child has all data members

// and member functions of class parent

obj1.id_c = 7;

obj1.id_p = 91;

cout << "Child id is " << obj1.id_c << endl;

cout << "Parent id is " << obj1.id_p << endl;

return 0;

Output:
Child id is 7
Parent id is 91

Page 4 of 15
Object Oriented Programming

In the above program the ‘Child’ class is publicly inherited from the ‘Parent’ class so the
public data members of the class ‘Parent’ will also be inherited by the class ‘Child’.

Modes of Inheritance:

1. Public mode: If we derive a sub class 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 derived class.
2. Protected mode: If we derive a sub class from a Protected base class. Then both public
member and protected members of the base class will become protected in derived class.
3. Private mode: If we derive a sub class from a Private base class. Then both public member
and protected members of the base class will become Private in derived class.
Note : The private members in the base class cannot be directly accessed in the derived class,
while protected members can be directly accessed. For example, Classes B, C and D all contain
the variables x, y and z in below example. It is just question of access.

// C++ Implementation to show that a derived class

// doesn’t inherit access to private data members.

// However, it does inherit a full parent object

class A

public:

int x;

Page 5 of 15
Object Oriented Programming

protected:

int y;

private:

int z;

};

class B : public A

// x is public

// y is protected

// z is not accessible from B

};

class C : protected A

Page 6 of 15
Object Oriented Programming

// x is protected

// y is protected

// z is not accessible from C

};

class D : private A // 'private' is default for classes

// x is private

// y is private

// z is not accessible from D

};

The below table summarizes the above three modes and shows the access specifier of the
members of base class in the sub class when derived in public, protected and private modes:

Page 7 of 15
Object Oriented Programming

Types of Inheritance in C++

Single Inheritance: In single inheritance, a class is allowed to inherit from only one class.
i.e. one sub class is inherited by one base class only.
Syntax:
class subclass_name : access_mode base_class
{
//body of subclass
};

// C++ program to explain

// Single inheritance

#include <iostream>

Page 8 of 15
Object Oriented Programming

using namespace std;

// base class

class Vehicle {

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

// sub class derived from two base classes

class Car: public Vehicle{

};

// main function

int main()

Page 9 of 15
Object Oriented Programming

// creating object of sub class will

// invoke the constructor of base classes

Car obj;

return 0;

Output:
This is a vehicle
Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can inherit
from more than one classes. i.e one sub class is inherited from more than one base classes.

Syntax:
class subclass_name : access_mode base_class1, access_mode base_class2, ....

//body of subclass

};

Here, the number of base classes will be separated by a comma (‘, ‘) and access mode for every
base class must be specified.

Page 10 of 15
Object Oriented Programming

// C++ program to explain

// multiple inheritance

#include <iostream>

using namespace std;

// first base class

class Vehicle {

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

// second base class

class FourWheeler {

public:

FourWheeler()

Page 11 of 15
Object Oriented Programming

cout << "This is a 4 wheeler Vehicle" << endl;

};

// sub class derived from two base classes

class Car: public Vehicle, public FourWheeler {

};

// 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

Page 12 of 15
Object Oriented Programming

This is a 4 wheeler Vehicle


Multilevel Inheritance: In this type of inheritance, a derived class is created from
another derived class.

1.

// C++ program to implement

// Multilevel Inheritance

#include <iostream>

using namespace std;

// base class

class Vehicle

Page 13 of 15
Object Oriented Programming

public:

Vehicle()

cout << "This is a Vehicle" << endl;

};

class fourWheeler: public Vehicle

{ public:

fourWheeler()

cout<<"Objects with 4 wheels are vehicles"<<endl;

};

// sub class derived from two base classes

class Car: public fourWheeler{

public:

car()

Page 14 of 15
Object Oriented Programming

cout<<"Car has 4 Wheels"<<endl;

};

// 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
Objects with 4 wheels are vehicles
Car has 4 Wheels

Page 15 of 15

You might also like