Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Unit 6 Inheritance

This document covers the concept of inheritance in Object Oriented Programming, detailing its types including single, multiple, multilevel, hierarchical, and hybrid inheritance. It explains the role of protected members, function overriding, and the use of virtual base classes to avoid duplication in inheritance. Additionally, it discusses abstract classes and constructors in derived classes, emphasizing their significance in class relationships.
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)
9 views8 pages

Unit 6 Inheritance

This document covers the concept of inheritance in Object Oriented Programming, detailing its types including single, multiple, multilevel, hierarchical, and hybrid inheritance. It explains the role of protected members, function overriding, and the use of virtual base classes to avoid duplication in inheritance. Additionally, it discusses abstract classes and constructors in derived classes, emphasizing their significance in class relationships.
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
You are on page 1/ 8

Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

UNIT 6

Inheritance
Topics Covered

1. Concept of Inheritance
2. types of inheritance
 single
 multiple
 multilevel
 hierarchical
 hybrid
3. protected members
4. overriding
5. virtual base class

Concept of Inheritance
 The mechanism of deriving a new class from an old one is called inheritance.
 The old class is referred as base class and new is define as derived class.

Types of inheritance

 There are 5 types of inheritance.


1)Single inheritance
2)Multiple inheritance
3)Hierarchical inheritance
4)Multilevel inheritance
5)Hybrid inheritance.

1)Single inheritance
 A derived class have only one base class is known as single inheritance.

Page 1 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

2)multiple inheritance
 A derived class have more than one base class is known as multiple inheritance.

A B

3) Hierarchical Inheritance
 A base class have more than one derived class is known as hierarchical inheritance.
A

B C D

4)Multilevel inheritance
 the mechanism of deriving a class from another derived class is known as multilevel
inheritance.

5)Hybrid inheritance
 if we want to combine more than one inheritance than it is hybrid inheritance.

B C

Page 2 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

Protected members
The protected Members

 A protected member variable or function is very similar to a private member but


it provided one additional benefit that they can be accessed in child classes which
are called derived classes.
 You will learn derived classes and inheritance in next chapter. For now you can
check following example where I have derived one child class SmallBox from a
parent class Box.
 Following example is similar to above example and here width member will be
accessible by any member function of its derived class SmallBox.

#include <iostream>
using namespace std;

class Box {
protected:
double width;
};

class SmallBox:Box { // SmallBox is the derived class.


public:
void setSmallWidth( double wid );
double getSmallWidth( void );
};

// Member functions of child class


double SmallBox::getSmallWidth(void) {
return width ;
}

void SmallBox::setSmallWidth( double wid ) {


width = wid;
}

// Main function for the program


int main() {
SmallBox box;

// set box width using member function


box.setSmallWidth(5.0);
cout << "Width of box : "<< box.getSmallWidth() << endl;

return 0;
}

Page 3 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

Defining derived classes


 A derived class can be defined by specifying its relationship with base class in addition to
its own details.
Syntax:
Class derived-class-name:visibility-mode base-class-name
{
…………..
…………..
};
 The colon indicates the derived class name is derived from the base-class.the visibility
mode is optional and if it is present it may be public or private.
 The default visibility mode is private.
examples
Class ABC : private XYZ
{
members of ABC
}
Class ABC : public XYZ
{
members of ABC
}

Class ABC : XYZ


{
members of ABC
}

 When a base class is privately inherited by a derived class, ‘public members’ of the base
class become private members of the derived class therefore the public members of the
base class can only be accessed by the member function of the derived class.
 When a base class is publicly inheritd, ‘public members’ of the base class become ‘public
members’ of the derived class therefore they are accessible to the objects of the derived
class.

Making private member inheritable


 We see that a private member of a base class cannot be inherited and therefore it is not
available for the derived class directly.
 C++ provides a third visibility modifier, protected which serve a limited purpose in
inheritance.
 A member declared as protected is accessible by the member functions within its class.
 It cannot be accessed by the functions outside these two classes.
 When a protected member is inherited in public mode,it becomes protected in the derived
class.
 A protected member, inherited in the private mode derivation, it becomes private in the
derived class.

Page 4 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

Base class Public Private Protected


visibility derivation derivation derivation
Private Not inherited Not inherited Not inherited
Protected Protected Private Protected
Public public private Protected

Overriding
 If base class and derived class have member functions with same name and arguments. If
you create an object of derived class and write code to access that member function then,
the member function in derived class is only invoked, i.e., the member function of derived
class overrides the member function of base class. This feature in C++ programming is
known as function overriding.

Accessing the Overridden Function in Base Class From Derived Class

To access the overridden function of base class from derived class, scope resolution operator
::. For example: If you want to access get_data() function of base class from derived class in
above example then, the following statement is used in derived class.

A::get_data; // Calling get_data() of class A.

Page 5 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

It is because, if the name of class is not specified, the compiler thinks get_data() function is
calling itself

virtual base class


 If you want to combine more than one type of inheritance than virtual base class is used.
 EXAMPLE
GrandParent

Parent1 Parent2

Child
 Here the class child has two direct base classes parent1 and parent2 which have a
common base class grandparent.
 The class child inherits the properties of grandparent via parent1 and parent2 so there is
possibility of duplication of values.

Page 6 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

 The duplication of inherited members due to these paths can be avoided by making the
common base class as virtual base class.
Class GrandParent
{
………..
};
Class Parent1 : virtual public GrandParent
{
………..
};
Class Parent2 : public virtual GrandParent
{
………….
};
Class Child : public Parent1,public Parent2
{
………….
};

Abstract class
 The class which is not used to create objects is known as abstract class.
 Base class is known as abstract class.
 Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract
classes are used to provide an Interface for its sub classes. Classes inheriting an Abstract
Class must provide definition to the pure virtual function, otherwise they will also
become abstract class

Characteristics of Abstract Class


1. Abstract class cannot be instantiated, but pointers and refrences of Abstract class
type can be created.
2. Abstract class can have normal functions and variables along with a pure virtual
function.
3. Abstract classes are mainly used for Upcasting, so that its derived classes can use
its interface.
4. Classes inheriting an Abstract Class must implement all pure virtual functions, or
else they will become Abstract too

Constructors in derived class


 A base class constructor takes any argument, the derived class need not have a
constructor function.
 If any base class contain a constructor with one or more arguments, then it is necessary
for the derived class to have a constructor and pass the argument to the base class
constructor.
 When both the derived and base classes, the base constructor is executed first and then the
derived constructor is executed.
Derived-constructor(arg1,arg2,…argN,arg(D)) :
Base1(arg1),
Base2(arg2),

Page 7 of 8
Subject : Object Oriented Programming UNIT – 6 Subject Code : 202000212

…………….
…………….
baseN(argN),
{
body of derived constructor
}
 The header line of derived constructor function contain two parts separated by a colon.
the first provides the declaration of the arguments that are passed to the derived
constructor and the second part list the function callls to the base constructors.
D(int a1,int a2,float b1, float b2,int d1) :
A(a1,a2),
B(b1,b2)
{
d=d1;
}
 Here A(a1,a2) invokes the base constructor A() and B(b1,b2) invokes the another base
constructor b().
 The constructor D() supplies the values for these four arguments.
 If you want call the above cnstructor as follow:
• D objD(5, 12, 2.5, 7.5, 30);
 Then the value is assigned as follow:
5 ->a1
12->a2
2.5->b1
7.5->b2
30->d1
 Execution of base class constructor is as follow:
class B : public A
{
};
 It first call base constructor A() then call derived constructor b().
class A : public B, public C
{
};
 It first call base constructor B(),then second base constructor C() and then derived
constructor A().
class A : public B,virtual public C
{
};
 It first call the virtual base class constructor C(), then base class constructor B() and then
derived class A().

Page 8 of 8

You might also like