0% found this document useful (0 votes)
20 views55 pages

Unit 3 (Topic 2)

Class B and class C both inherit from class A, allowing them to reuse attributes and behaviors of class A. This is an example of hierarchical inheritance, where the traits of class A are inherited by multiple derived classes (B and C). Hierarchical inheritance allows code reuse by inheriting the same parent class in different parts of the class hierarchy.

Uploaded by

isayashpbende26
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)
20 views55 pages

Unit 3 (Topic 2)

Class B and class C both inherit from class A, allowing them to reuse attributes and behaviors of class A. This is an example of hierarchical inheritance, where the traits of class A are inherited by multiple derived classes (B and C). Hierarchical inheritance allows code reuse by inheriting the same parent class in different parts of the class hierarchy.

Uploaded by

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

C++ and Object Oriented Programming

Unit – 3 (Topic – 2)
Inheritance

Prepared By
Prof. Shah Brijesh
Inheritance
Inheritance:
-> Reusability is another important feature of OOP.
-> The reuse of a class that has already been tested, debugged and
used many times can save us the effort of developing and testing
the same again.
-> C++ also supports the concept of reusability.
-> The C++ classes can be reused in several ways.
-> Once a class has been written and tested, it can be adapted by
other programmers to suit their requirements.
-> This is basically done by creating new classes, reusing the
properties of the existing ones.
-> The mechanism of deriving a new class from an old one is called
inheritance (Derivation).
-> The old class is referred to as the base class(super class or
parent class) and the new one is called the derived class (sub class
or child class).
Inheritance
Inheritance:
Defined Derived Classes:
-> A derived class can be defined by specifying its relationship with the base class in addition to its
own details.
-> The general form of defining a derived class is:

class derivedclassname : visibilitymode baseclassname


{
……………
…………….
};
-> The colon indicate that the derivedclassname is derived from the baseclassname.
-> The visibilitymode is optional and if present, may be either private or public.
-> The default visibilitymode is private.
-> Visibility mode specifies whether the features of the base class are privately derived or publicly
derived.
For Example:
class B : private A // private derivation
{ …..
}
class B : private A // public derivation
{ …..
}
Inheritance
Inheritance:
Defined Derived Classes:
class B : A // private derivation by default
{ …..
}
-> When a base class is privately inherited by a derived class, ‘public members’ of
the base class become ‘private member’ of the derived class and therefore the
public members of the base class can only be accessed by the member functions
of the derived class.
-> They are inaccessible to the objects of the derived class.
-> A public member of a class can be accessed by its own objects using the dot
operator.
-> The result is that no member of the base class is accessible to the derived class.
-> When the base class is publicly inherited, ‘public members’ of the base class
become ‘public members’ of the derived class and therefore they are accessible to
the objects of the derived class.
-> In both the cases, the private members are not inherited and therefore, the
private members of a base class will never become the members of its derived
class.
-> In inheritance, some of the base class data elements and member functions are
inherited into the derived class.
Inheritance
Inheritance:
Defined Derived Classes:
-> We can add our own data and member functions and thus extend the
functionality of the base class.
Types Of Inheritance:
-> Single Inheritance -> Multiple Inheritance
-> Hierarchical Inheritance -> Multilevel Inheritance
-> Hybrid Inheritance
1. Single Inheritance:
-> The derived class with only one base class is called single inheritance.

B
D
-> Example - 1: (Public Derivation)
class A
{
int a;
public:
void getA(); void putA(); };
Inheritance
Inheritance:
1. Single Inheritance:
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
class B : public A
{
int b;
public:
void getB();
void putB();
};
Inheritance
Inheritance:
1. Single Inheritance:
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
void main()
{
A x;
B y;
clrscr();
x.getA();
x.getB();
y.getA();
y.getB();
Inheritance
Inheritance:
1. Single Inheritance:
x.putA();
x.putB();
y.putA();
y.putB();
}
class A B : public A
private a b
public getA() getB()
putA() putB()
getA()
putA()
Note : -> Here we derived class A(Base class) into class B publicly .
-> So, public members of class A(base class) are also public
members of class B (Derived class)
-> Therefore with the help of object of class B we can access
public members of class A also.
Inheritance
Inheritance:
1. Single Inheritance:
-> Example - 2: (Private Derivation)
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : private A
{
int b;
public:
void getB();
void putB(); };
void B :: getB()
{
cout<<“Enter b:”;
cin>>b; }
void B :: putB()
{
cout<<“\n b = “<<b; }
void main()
{
A x; B y;
x.getA(); x.getB();
y.getA(); y.getB();
Inheritance
Inheritance:
1. Single Inheritance:
x.putA();
x.putB();
y.putA();
y.putB();
}
class A B : public A
private a b
getA()
putA()
public getA() getB()
putA() putB()
Note : -> Here we derived class A(Base class) into class B privately.
-> So, public members of class A(base class) are private
members of class B (Derived class)
-> Therefore we can not access public members of class A
with the help of class B.
Inheritance
Inheritance:
2. Multiple Inheritance:
-> A class can inherit the attribute of two or more classes as show in below fig.
B-1 B-2 B-3

-> This is known as multiple inheritance.


-> Multiple inheritance allows us to combine the features of several existing
classes as a starting point for defining new classes.
-> It is like a child inheriting the physical features of one parent and the
intelligence of another.
-> The syntax of a derived class with multiple base classes is as follows.
class D : visibility B-1, visibility B-2,……….
{
……………………
………………….. (Body of class D)
};
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C : public A, public B
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
void main()
{
C x;
x.getA();
x.getB();
x.getC();
x.putA();
x.putB();
x.putC();
getch();
}
class private public
A a getA(), putA()
B b getB(), putB()
C : public A< public B c getC(), putC(), getA(), getB(), putA(), putB()

Note : -> Here we derived class A and B(Base class) into class C publicly.
-> So, public members of class A and B (base class) are public members of class C (Derived
class)
-> Therefore we can access public members of class A and B with the help of class C.
Inheritance
Inheritance:
3. Hierarchical Inheritance:
-> The traits (members) of one class may be inherited by
more than one class.
-> This process is known as hierarchical inheritance.
-> Below figure shows the hierarchical inheritance

D-1 D-2 D-3


Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C : public A
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
void main()
{
B x; C y;
x.getA(); y.getA();
x.getB(); y.getB();
x.getC(); y.getC();
x.putA(); y.putA();
x.putB(); y.putB();
x.putC(); y.putC();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C : public A c getC(), putC(), getA(), putA()

Note : -> Here we derived class A (Base class) into class B and C publicly.
-> So, public members of class A are public members of class B and class C
-> Therefore we can access public members of class A with the help of class B.
-> as well as public members of class A with the help of class C
-> but we can not access members of class B from object of class C and members of class C
from objects of class B.
Inheritance
Inheritance:
4. Multi Level Inheritance:
-> The mechanism of deriving a class from another derived class is known as
multilevel inheritance.
-> Below figure shows the multi level inheritance.

-> The class A serves as a base class for the derived class B, which in turn serves as
a base class for the derived class C.
-> The class B is known as intermediate base class since it provides a link for the
inheritance between A and C.
-> The chain ABC is known as inheritance path.
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C : public B
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
void main()
{
C x;
x.getA();
x.getB();
x.getC();
x.putA();
x.putB();
x.putC();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C : public B c getC(), putC(), getA(), putA(), getB(), putB()

Note : -> Here we derived class A into class B publicly & derived class C from class B Publicly.
-> So, We can access public members of class A from class B and public members of class A
& B From class C.
Inheritance
Inheritance:
5. Hybrid Inheritance:
-> Combination of Two or more types of inheritance (Multiple Inheritance,
Hierarchical Inheritance & Multi Level Inheritance) is known as hybrid inheritance.
-> Below figure shows the Hybrid inheritance.

B C

-> The class A serves as a base class for the derived class B, which in turn serves as
a base class for the derived class D & The class C is also serve as a base class for
the derived class D.
-> Here we combine multilevel inheritance and multiple inheritance.
-> Hybrid inheritance is also known as multi path ineritance.
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
class D : public B, public C
{
int d;
public:
void getD();
void putD();
};
void D :: getD()
{
cout<<“Enter d:”;
cin>>d;
}
void D :: putD()
{
cout<<“\n d = “<<d;
}
Inheritance
Inheritance:
void main()
{
D x;
x.getA();
x.getB();
x.getC();
x.getD();
x.putA();
x.putB();
x.putC();
x.putD();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C c getC(), putC()
D : public B, public C d getD(), putD(), , getA(), putA(), getB(), putB()
getC(), putC()

Note : -> Here we derived class A into class B publicly & derived class C from class B and D Publicly.
-> So, We can access public members of class A, class B and class C from class D.
Inheritance
Inheritance:
Virtual Base Class:
-> Consider a situation where all the three kinds of inheritance (multiple, multi
level and hierarchical) are involved.
-> This is illustrated in below figure.

Grand Parent

Parent 1 Parent 2

Child

-> The ‘child’ has two direct base classes ‘parent1’ and ‘parent2’ which themselves
have a common base class ‘grandparent’.
-> The ‘child’ inherits the traits of ‘grandparent’ via two separate paths.
-> It can also inherit directly as shown by the broken line.
-> The ‘grandparent’ is sometimes referred to as indirect base class.
-> Inheritance by the ‘child’ as shown in above fig. Might pose some problems.
-> All the public and protected members of ‘grandparent’ are inherited into ‘child’
twice, first via ‘parent1’ and again via ‘parent2’.
Inheritance
Inheritance:
Virtual Base Class:
-> This means, ‘child’ would have duplicate sets of the members inherited from
‘grandparent’.
-> This introduces ambiguity.
-> The duplication of inherited members due to these multiple paths can be
avoided by making the common base class (ancestor class) as virtual base class
while declaring the direct or intermediate base classes which is shown as follows:
class A class B1 : virtual public A
{ {
……………. ……………….
}; };
class B2 : public virtual A class D : public B1, public B2
{ {
……………. ……………….
}; };
-> When a class is made a virtual base class, C++ takes necessary care to see that
only one copy of that class is inherited, regardless of how many inheritance paths
exist between the virtual base class and derived class.
Note : The keywords virtual and public may be used in either order.
Inheritance
Inheritance:
class A
{
int a;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
class B : virtual public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter b:”;
cin>>b;
}
void B :: putB()
{
cout<<“\n b = “<<b;
}
Inheritance
Inheritance:
class C : public virtual A
{
int c;
public:
void getC();
void putC();
};
void C :: getC()
{
cout<<“Enter c:”;
cin>>c;
}
void C :: putC()
{
cout<<“\n c = “<<c;
}
Inheritance
Inheritance:
class D : public B, public C
{
int d;
public:
void getD();
void putD();
};
void D :: getD()
{
cout<<“Enter d:”;
cin>>d;
}
void D :: putD()
{
cout<<“\n d = “<<d;
}
Inheritance
Inheritance:
void main()
{
D x;
x.getA();
x.getB();
x.getC();
x.getD();
x.putA();
x.putB();
x.putC();
x.putD();
getch();
}
class private public
A a getA(), putA()
B : public A b getB(), putB(), getA(), getB()
C c getC(), putC()
D : public B, public C d getD(), putD(), , getA(), putA(), getB(), putB()
getC(), putC()

Note : -> Here we derived class A into class B and class C publicly & derived class B and class C into
class D Publicly.
-> So, We can access public members of class A, class B and class C from class D.
Inheritance
Inheritance:
Making a private member inheritable:
-> We know that a private member of a base class cannot be inherited and therefore it is not
available for the derived class directly.
-> To inherit private members of a base class 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 and any
class immediately derived from it.
-> It cannot be accessed by the functions outside these two classes.
-> A class can now use all the three visibility modes as illustrated below:
class A
{
private:
…………….
…………….
protected:
…………….
…………….
public:
…………….
…………….
};
-> When a protected member is inherited in public mode, it becomes protected in the derived class
too and therefore is accessible by the member functions of the derived class.
Inheritance
Inheritance:
Making a private member inheritable:
-> It is also ready for further inheritance.
-> A protected member, inherited in the private mode derivation,
becomes private in the derived class.
-> Although it is not available for further inheritance
-> It is also possible to inherit a base class in protected mode.
-> In protected derivation, both the public and protected members of the
base class become protected members of the derived class.
-> Below table summarizes how the visibility of base class members
undergoes modifications in all the three types of derivation.

Base Class Derived Class Visibility


Visibility Private Protected Public
Derivation Derivation Derivation
Private Not Inherited Not Inherited Not Inherited
Protected Private Protected Protected
Public Private Protected Public
Inheritance
Inheritance:
Making a private member inheritable:
class A
{
int a;
protected:
int a1;
public:
void getA();
void putA();
};
void A :: getA()
{
cout<<“Enter a:”;
cin>>a;
}
void A :: putA()
{
cout<<“\n a = “<<a;
}
Inheritance
Inheritance:
Making a private member inheritable:
class B : public A
{
int b;
public:
void getB();
void putB();
};
void B :: getB()
{
cout<<“Enter a1 and b:”;
cin>>a1>>b;
}
void B :: putB()
{
cout<<“\n a1 = “<<a1<<“\t b = “<<b;
}
Inheritance
Inheritance:
Making a private member inheritable:
void main()
{
B x;
x.getA();
x.getB();
x.putA();
x.putB();
cout<<“\n a1 =“<<a1;
getch();
}
class private protected public
A a a1 getA(), putA()
B : public A b a1 getB(), putB(),
getA(), putA()

Note : -> Here we derived class A into class B publicly.


-> So, public members of class A are public members of class B, and protected members of
class A are protected of class B.
-> Therefore we can access public members of class A with the help of class B.
Inheritance
Constructors In Derived Classes:
-> We know that constructors can not be inherited.
-> As long as base class have constructor with no arguments, the derived class
need not have a constructor function.
-> However, if any base class contains a constructor with one or more arguments,
then it is mandatory for the derived class to have a constructor and pass the
arguments to the base class constructors.
-> Remember, while applying inheritance we usually create objects using the
derived class.
-> Thus, it makes sense for the derived class to pass arguments to the base class
constructor.
-> When both the derived and base classes contain constructors, the base
constructor is executed first and then the constructor in the derived class is
executed.
-> In case of multiple inheritance, the base classes are constructed in the order in
which they appear in the declaration of the derived class.
-> Similarly, in a multilevel inheritance, the constructors will be executed in the
order of inheritance.
-> The constructor of the derived class receives the entire list of values as its
arguments and passes them on to the base constructors in the order I which they
are declared I the derived class.
Inheritance
Constructors In Derived Classes:
Syntax:
Derivedconstructor(arglist1, arglist2, …. Arglistn) : base1(arglist1),
base2(arglist2,….. Basen(arglistn)
{
Body of derived constructor;
}
-> The header line of derived constructor function contains two parts
separated by a colon.
-> The first part provides the declaration of the arguments that are passed
to the derived constructor and the second part lists the function calls to
the base constructors.
-> base1(arglist1), base2(arglist2),…… are function calls to base
constructors base1(), base2(), …… and therefo0re arglist1, arglist2, … etc
represent the actual parameters that are passed to the base constructors.
->arglist1 through arglistn are the argument declaratio ns for base
constructors base1 through basen.
-> arglistD provides the parameters that are necessary to initialize the
members of the derived class.
Inheritance
Constructors In Derived Classes:
For Example:
class A
{
int a;
public:
A(int x)
{
cout<<“\n Base Constructor”;
a=x;
}
void dispA()
{
cout<<“\n a =“<<a;
}
};
class B : public A
{
int b;
public:
Inheritance
Constructors In Derived Classes:
For Example:
public:
B(int x, int y) : A(x)
{
cout<<“\n Derive Constructor”;
b = y;
}
void dispB()
{
cout<<“\n b =“<<b;
}
};
void main()
{
B x(10,20);
x.dispA();
x.dispB();
}
Inheritance
MIL:
-> Full form of MIL is Multiple Initialization List.
-> C++ also supports method of initializing class objects.
-> This method is known as Initialization List in the constructor function.
-> This takes the following form:
constructor(arglist) : initialization-section
{
assignment-section;
}
-> The assignment-section is nothing but the body of the constructor function and
is used to assign initial values to its data members.
-> The part immediately following the colon is known as the initialization-section.
-> We can use this section to provide initial values to the base constructors and
also to initialize its own class members.
-> This means that we can use either of the sections to initialize the data members
of the constructors class.
-> The initialization-section basically contains a list of initializtions separated by
commas.
-> This is known as initialization-list (MIL).
Inheritance
MIL:
For Example:
class A
{
int a1, a2;
public:
A(int x, int y) : a1(x), a2(2*y)
{
cout<<“\n Base Constructor”;
}
void dispA()
{
cout<<“\n a1 =“<<a1<<“\t a2 = “<<a2;
}
};
class B : public A
{
int b1, b2;
public:
Inheritance
Constructors In Derived Classes:
For Example:
public:
B(int a, int b, int c, int d) : A(a, b), b1(c)
{
cout<<“\n Derive Constructor”;
b2 = d;
}
void dispB()
{
cout<<“\n b1 =“<<b1<<“\t b2 =“<<b2;
}
};
void main()
{
B x(10,15,20,25);
x.dispA();
x.dispB();
}
Inheritance
Member Classes (Nesting Of Classes):
-> Inheritance is the mechanism of deriving certain properties of one class
into another.
-> C++ supports yet another way of inheriting properties of one class into
another.
-> This approach takes a vie that an object cn be a collection of many
other objects.
-> This is a class can contain objects of other classes as its members as
shown below:
class A { …. };
class B { …. };
class C
{
A a;
B b;
…………..
};
-> All objects of C class will contain the objects a and b.
Inheritance
Member Classes (Nesting Of Classes):
-> This kind of relationship is called containership or nesting.
-> Creation of an object that contains another object is very
different than the creation of an independent object.
-> An independent object is created by its constructor when it is
declared with arguments.
-> On the other hand, a nested object is created in two stages.
-> First, the member objects are created using their respective
constructors and then the other ‘ordinary’ members are created.
-> This means, constructor of all the member objects should be
called before its own constructor body is executed.
-> This is accomplished using an initialization list in the constructor
of the nested class.
Inheritance
Constructors In Derived Classes:
For Example:
class A
{
int a;
public:
A(int x)
{
cout<<“\n Base Constructor”;
a = x;
}
void dispA()
{
cout<<“\n a =“<<a;
}
};
class B
{
int b;
A m;
Inheritance
Constructors In Derived Classes:
For Example:
public:
B(int x, int y) : m(x)
{
cout<<“\n Derive Constructor”;
b = y;
}
void dispB()
{
m.dispA();
cout<<“\n b =“<<b;
}
};
void main()
{
B x(10,20);
x.dispB();
}

You might also like