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

1.- Inheritance and polymorphism

this is the slide for the material sciences for student of masters
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

1.- Inheritance and polymorphism

this is the slide for the material sciences for student of masters
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

1.

Inheritance and
polymorphism

1/19
3. Inheritnce

Inheritance

 Inheritance is the capability of one class to acquire properties and


characteristics from another class.

 Inheritance makes the code reusable; when we inherit an existing class, all
its methods and fields become available in the new class, hence the code is
reused

NOTE: All members of a class except the private ones, are inherited
2/19
3. Inheritance

Introductory example to inheritance:

Hierarchy:

• Parent, Base class or Super class  Class whose properties


are inherited by another class

• Child, Derived class or Sub class  Class which inherits


properties of another class

3/19
3. Inheritance

Different kinds of inheritance:

Depending on the access modifier of the parent class, the type of inheritance
can be of one type or of another.

Types of inheritance according to accessibility:

1) Public Inheritance

2) Protected Inheritance

3) Priavte Inheritance

4/19
3. Inheritance

1) Public Inheritance

It is the most used inheritance mode.

A Public access specifier results in all public members of the base class becoming public members of
the derived class and all protected members of base class becoming protected members of the
derived class

5/19
3. Inheritance

2) Private Inheritance

A Private member access specifier results in all public and protected members
of the base class becoming private members in the derived class

Note: if we do not specify it, by default the visibility is Private for the inherited
class.

6/19
3. Inheritance

3) Protected Inheritance

A Protected member access specifier results in all public and protected


members of the base class becoming protected members in the derived class

7/19
3. Inheritance

Example of the syntax of Inheritance:


Note we inherit another class’ properties by stating which is the
access mode of the parent (private, public or protected).

output

8/19
3. Inheritance

 Constructors

• Special member function to initialize the objects


of its class

• Data members can be initialized through


constructors Syntax:
class scaler {
• The name of the constructor will be the same as public:
// Constructor
the name of the class
scaler() {
• Constructors can be defined either inside the // Constructor body.
}
class definition or outside the class definition };
using the class name and the scope resolution
“::” operator

• We can have a constructors with arguments.


Code
Output

10
3. Inheritance

 Destructors

• Destructor is also a special member function like constructor. Destructor destroys


the class objects created by constructor.
• Destructor has the same name as their class name preceded by a tilde (~) symbol.
•Destructors never have any arguments.

11/19
3. Inheritance

The role of constructors and destructors in inheritance

Constructors:
• Constructors are not inherited.

• Constructor for base class is called before constructor


for derived class

• Separate constructors for each class (Base and derived)

12/19
3. Inheritance

The role of constructors and destructors in inheritance

• The destructors of base classes and members are called in the reverse order of the completion of
their constructor:

13/19
Code:
#include<iostream> Output
using namespace std;
int count=0;
class Test
{
public:
Test()
{
count++;
cout<<"\n No. of Object
created:\t"<<count;
}

~Test()
{
cout<<"\n No. of Object
destroyed:\t"<<count;
count--;
}
};

main()
{
Test t,t1,t2,t3;
return 0; 14
}
3. Inheritance

Polymorphism

• Sending the same message to the objects of different classes and each respond
differently depending upon its class

• It works only in the classes that are derived from the same base class

15/19
3. Inheritance

Types of Polymorphism

They are categorized into two types:

1. Compile Time Polymorphism

2. Runtime Polymorphism

16/19
3. Inheritance

Compile Time Polymorphism

In compile-time polymorphism, a function is called at the time of program


compilation. We call this type of polymorphism as early binding or Static binding.
It has two types:

• Function Overloading

• Operator Overloading

17/19
3. Inheritance

Function Overloading

• Function overloading means one function can perform many tasks.

• In the function overloading function will call at the time of program compilation.
It is an example of compile-time polymorphism.

18/19
Output

Code
19
3. Inheritance

Operator Overloading

• Operator overloading means defining additional tasks to operators without


changing its actual meaning. We do this by using operator function.

• The purpose of operator overloading is to provide a special meaning to the user-


defined data types.

20/19
Output

Code

Code

21
3. Inheritance

Runtime Polymorphism

• In a Runtime polymorphism, functions are called at the time the program execution. Hence, it is known as
late binding or dynamic binding.

• It can be performed using function overriding and virtual functions.

• It provides slow execution as it is known at the run time. Thus, It is more flexible as all the things
executed at the run time.

22/19
3. Inheritance

Function overriding

• We give the new definition to base class function in the derived class

• In function overriding, we have two definitions of the same function, one in the superclass and one in the
derived class.

• The decision about which function definition requires calling happens at runtime

23/19
3. Inheritance

Virtual member function

• Function in base class, which is overrided in a derived class, that tells the compiler to perform Late
Binding on this function

• They ensure that the correct function is called for an object, regardless of the type of reference or pointer
used for function call.

• Functions are declared with a virtual keyword in base class

24/19
Output

Code

25
3. Inheritance

Pure virtual member functions

• They are virtual functions with no definition.

• They always start with virtual keyword and they are finished with “=0”.

• They can give rise to abstract classes and to interface classes.

26/19
3. Inheritance

Abstract classes

• Classes which contain at least one pure virtual function in it.

• We cannot create objects out of base classes that are abstract.

• This is why attempting to instantiate an object out of an abstract class causes a


compilation error.

• The purpose of an abstract class is to provide an appropriate base class from


which other classes can inherit.

• if we do not override the pure virtual function in derived class, then derived class
also becomes abstract class.

27/19
3. Inheritance

Thank you!

28/19

You might also like