Вы находитесь на странице: 1из 27

Object Oriented concepts

Inheritance and Polymorphism

LEVEL – LEARNER

1
Icons Used

Hands-on Exercise Reference Questions Points To Ponder

Coding Standards Lend A Hand Summary Test Your Understanding

2
Overview

The session on Inheritance and Polymorphism will


provide knowledge and understanding of the concepts
by which one can implement and improve the design,
structure, and reusability of the code.

3
Objectives

• At the end of this session you will be


able to:
— Define Inheritance and its
implementation
— Define Polymorphism and its
implementation
— Describe Overloading
— Describe Overriding

4
Inheritance

• What is Inheritance? Explain by using the given images.

5
Inheritance (Contd.)

• Inheritance means deriving a new class (sub class) from an existing class (base class
or super class).
• Inheritance creates a hierarchy of related classes (types) which share code and
interface.

Father Example:
State: The father is called the
Assets “Super” class and the
Car Father is the
son is called the
Behaviour: parent class.
Exercise Daily “Sub” class.
Do Work

Son inherits the assets - car


Son and the behavior (Exercise
Daily, Do Work) from his
father

6
Check Your Understanding

Base and Derived Class

What is Super What is Sub


Class? Class?

In this example, what is


Class A and Class B?

7
Lend A Hand: Forms of Inheritance

• Single Inheritance is the method in which a derived class has only one base class.

#include <iostream>
class Shape // Base class
{
public:
void setWidth(int w)
{
width = w; What will be the
}
output of the code
void setHeight(int h)
{ when you execute it?
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape // Derived class
{

public:
int getArea()
8 {
return (width * height);
Lend A Hand: Forms of Inheritance

• Identify the Inheritance and output of the code?


#include <iostream> // Base class Shape
class Shape
{ public:
void setWidth(int w)
{
width = w;
} What will be the
void setHeight(int h) output of the code
{
height = h; when you execute it?
}
protected:
int width;
int height;
};
class PaintCost // Base class PaintCost
{ public:
int getCost(int area)
{
return area * 70;
}
};
class Rectangle: public Shape, public PaintCost // Derived class
{
public:
int getArea()
{
return (width * height);
}
9 };
How to Make a Private Member Inheritable?

• A private member of a base class cannot be inherited and therefore it is not available
for the derived class directly.
• This can be done by modifying the visibility limit of the private member by making it
public. This would make it accessible to all the functions of the programs.

class alpha
{
private: // with in the class

public: // out side the class

};

10
Polymorphism

• What is Polymorphism?
— The ability to take on different
forms.
— Manipulate objects of various
classes and invoke methods on an
object without knowing that
object’s type.

11
Polymorphism (Contd.)

• Polymorphism is the feature that allows one interface to be used for general class of
action. The specific action is determined by the exact nature of the situation.
• By these you got an idea that, if there is a one interface then there can be many
implementation classes for it. This depends upon:
— the Object type instantiation
— the particular behavior that will be invoked.

Can you tell me about a


scenario where Polymorphism
will be used in real time or
projects?

12
Polymorphism (Contd.)

• Polymorphism is only concerned with the application of specific implementations to an


interface or a more generic base class.
• It is obvious that during Inheritance between two classes, all the methods and
properties of the first class are derived to the second class. Hence, the second class
becomes the child class which inherits all the functionalities of the base class. It can
also possess its own separate methods.

13
Polymorphism Types

• There are three different types of Polymorphism:

— Method Overloading: Two versions of the same method available in the same class.

— Method Overriding: Methods of a sub class override the methods of a super class.

— Dynamic Method Binding: At run time the interpreter will find out which object’s
method needs to be executed.

14
Overloading

• There are two types of Overloading:


— Function Overloading
— Operator Overloading

• Overloading enables several functions of the same name to be defined, as long as these
functions have different sets of parameters. This capability is called function
overloading.
• Overloading selects the proper function by examining the number, types, and order of
the arguments in the call. Function overloading is commonly used to create several
functions of the same name that perform similar tasks but on different data types.
•Example:
— void info(char *name); // if we call info(Bob) this function is executed
— void info(char *name, int age); // if we call info(Bob, 18) this
function is executed

15
Lend A Hand: Function Overloading

• Consider a function print, which displays an int.


• As shown in the following example, you can overload the function print to display other
types, for example, double and char*. You can have three functions with the same
name, each performing a similar operation on a different data type.:

#include <iostream>
class printData
{ public:
void print(int i) {
cout << "Printing int: " << i << endl;
}
void print(double f) {
cout << "Printing float: " << f << endl;
}
void print(char* c) {
cout << "Printing character: " << c << endl;
}
};
What will be the output
of the code when you
int main(void) execute it?
{
printData pd;
pd.print(5); // Call print to print integer
16 pd.print(500.263); // Call print to print float
Operator Overloading

• Operator Overloading is basically using the same operator to perform different


operations.
• A popular example is using “+” operator to perform addition of numbers and
concatenation of strings.
Example:
addition = 2 + 2
string = "Hello" + "world“
FullName = FName + Lname

• Here is an example to show the concept of operator overloading using a member


function. An object is passed as an argument whose properties will be accessed using
this object. The object which will call this operator can be accessed using this operator
as explained in next slide.

17
Lend A Hand: Operators
Overloading in C++
•What will be the output of the code?

#include <iostream>
class Shape // Base class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
class PaintCost // Base class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};

class Rectangle: public Shape, public PaintCost // Derived class


{
public:
int getArea()
18 {
return (width * height);
Overloadable/Non-Overloadable
Operators
• Following is the list of operators which can be overloaded:
+ - * / % ^

& | ~ ! , =

< > <= >= ++ --

<< >> == != && ||

+= -= /= %= ^= &=

|= *= <<= >>= [] ()

-> ->* new new [] delete delete []

• Following is the list of operators which cannot be overloaded:

:: .* . ?:

19
Overriding

• Son is the Sub Class and he inherits the


• Father is the Super Class
properties of the super class (father)

• Father likes to read books. His favorite • Son also likes to read books. His favourite
genre is medical fiction genre is modern fantasy comics

• Here, the son has inherited the father’s behavior of reading books, but with a difference in the
type of book. Here the son is overriding the behavior of his father.

20
Questions

21
Check Your Understanding

22
Summary

• In this session, you have learned the


following:
— Inheritance is the concept of a
child class (sub class) automatically
inheriting the variables and
methods defined in a parent class
(super class).
— Classes can inherit attributes and
behavior from pre-existing classes
called Base Classes. The resulting
classes are known as Derived
Classes, Sub Classes, or Child
Classes.

23
Summary (Contd.)

— During function overloading,


multiple functions can be written
under the same name, provided
that they differ in argument types
and/or numbers.
— Operator overloading is basically
using the same operator to
perform different operations.

24
Source

• http://www.programcall.com/20/csnet/types-of-inheritance-in-csnet.aspx
• http://en.kioskea.net/contents/poo/polymorp.php3
• http://atomicobject.com/pages/Inheritance
• http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm
• http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm

Disclaimer: Parts of the content of this course are based on the materials available from the websites and books listed
above. The materials that can be accessed from the linked sites are not maintained by Cognizant Academy and we are
not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are the marks
of the respective owner(s).

25
Change Log

Version Changes made


Number
V1.0 Initial Version
V2.0 Slide No.
Changed By Effective Date Changes
Effected
1-27 • Learning 17.05.2013 Base-lining
Content Content
Team
• CATP
Technical
Team
• CI Team

26
Object – Oriented Concepts

You have successfully completed the


session on Inheritance and
Polymorphism

27

Вам также может понравиться