Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

OOPS Notes Unit-6

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

OOPS Notes Unit-6

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

Course Name: Object Oriented Programming Systems (OOPS)

Course Code : CS/IT103

Credits: 4

Prepared by: Dr. Rakesh Chowdhury

Assistant Professor (EEE)


Email: rakeshc@iiitm.ac.in
UNIT- 6: Polymorphism in OOPS
Polymorphism >> Introduction

• Polymorphism is the ability of any data to be processed in more than one form.

• The word itself indicates the meaning as poly means “many” and morphism
means “types”

• The most common use of polymorphism in object-oriented programming occurs


when a parent class reference is used to refer to a child class object. Here we
will see how to represent any function in many types and many forms.

• In a real-life example of polymorphism, a person at the same time can have


different roles to play in life. Like a woman, at the same time is a mother, a wife,
an employee and a daughter.

• So, the same person has to have many features but has to implement each as
per the situation and the condition. Also called as static polymorphism
Polymorphism >> Types
Polymorphism >> Functional Overloading

Function Overloading

• When there are multiple functions with the same name but different parameters, then the functions are said to
be overloaded, hence this is known as Function Overloading.
• Functions can be overloaded by changing the number of arguments or/and changing the type of arguments.
In simple terms, it is a feature of object-oriented programming providing many
• There are certain Rules of Function Overloading that should be followed while overloading a function.
Polymorphism >> Functional Overloading

•Parameters should have a different type •Parameters should have a different number

#include <iostream>
#include <iostream> using namespace std;
using namespace std;
void add(int a, int b)
{
void add(int a, int b)
cout << "sum = " << (a + b);
{
}
cout << "sum = " << (a + b);
}
void add(int a, int b, int c)
void add(double a, double b)
{
{
cout << endl << "sum = " << (a + b + c);
cout << endl << "sum = " << (a + b);
}
}
// Driver code
int main()
int main()
{
{
add(10, 2);
add(10, 2);
add(5.3, 6.2);
add(5, 6, 4);
return 0;
return 0;
}
}
Polymorphism >> Functional Overloading

•Parameters should have a different sequence of parameters.


•Example of Function overloading

#include<iostream>
using namespace std;

void add(int a, double b)


{
cout<<"sum = "<<(a+b);
}

void add(double a, int b)


{
cout<<endl<<"sum = "<<(a+b);
}
int main()
{
add(10,2.5);
add(5.5,6);

return 0;
}
Polymorphism >> Functional Overloading

What is the output???


Polymorphism >> Operator Overloading

In this example, we have 3 variables “a1”, “a2” and “a3” of


type “class A”. Here we are trying to add two objects “a1”
// C++ Program to Demonstrate the
and “a2”, which are of user-defined type i.e. of type “class
// working/Logic behind Operator
A” using the “+” operator. This is not allowed, because the
// Overloading
addition operator “+” is predefined to operate only on built-
class A {
in data types. But here, “class A” is a user-defined type, so
statements;
the compiler generates an error. This is where the concept
};
of “Operator overloading” comes in.
int main()
Now, if the user wants to make the operator “+” add two
{
class objects, the user has to redefine the meaning of the
A a1, a2, a3;
“+” operator such that it adds two class objects. This is
done by using the concept of “Operator overloading”. So
a3 = a1 + a2;
the main idea behind “Operator overloading” is to use C++
operators with class variables or class objects. Redefining
return 0;
the meaning of operators really does not change their
}
original meaning; instead, they have been given additional
meaning along with their existing ones.
Polymorphism >> Operator Overloading
Polymorphism >> Operator Overloading

Why can’t the above-stated operators be overloaded?

1. sizeof Operator
This returns the size of the object or datatype entered as the operand. This is evaluated by the compiler and
cannot be evaluated during runtime. The proper incrementing of a pointer in an array of objects relies on the
sizeof operator implicitly. Altering its meaning using overloading would cause a fundamental part of the
language to collapse.

2. Scope resolution (::) Operator


This helps identify and specify the context to which an identifier refers by specifying a namespace. It is
completely evaluated at runtime and works on names rather than values. The operands of scope resolution are
note expressions with data types and CPP has no syntax for capturing them if it were overloaded. So it is
syntactically impossible to overload this operator.

3. Class member access operators (.(dot), .* (pointer to member operator))


The importance and implicit use of class member access operators can be understood through the following
example:
Polymorphism >> Operator Overloading Overloading + or – Binary Operator

#include <iostream> demo operator + (demo bb)


using namespace std; {
demo cc;
Class Demo cc.a = a + bb.a;
{ return cc;
int a; }
Public: };
void getdata()
{
cout << “\n enter a number:”; int main()
cin >> a; {
} demo aa, bb, cc;
aa.getdata();
void putdata() bb.getdata();
{
cout << “\n Value = “ << a; cc = aa + bb;
} return 0;
}
Polymorphism >> Operator Overloading

Overloading ++ or –- Unary Operator

#include <iostream> void operator ++ ( )


using namespace std; {
x =x +1;
Class Demo }
{ };
int a;
Public:
void getdata() int main()
{ {
cout << “\n enter a number:”; demo aa;
cin >> x; aa.getdata();
} aa.putdata();

void putdata() ++aa;


{ return 0;
cout << “\n Value = “ << x; }
}
Polymorphism >> Function Overriding

A function is a block of statements that together performs a specific task by taking some input and producing a
particular output. Function overriding in C++ is termed as the redefinition of base class function in its derived class
with the same signature i.e. return type and parameters. It falls under the category of Runtime Polymorphism.

Function overriding:
Method overriding is a feature that allows you to redefine the parent class method in the child class based on its
requirement. In other words, whatever methods the parent class has by default are available in the child class.
But, sometimes, a child class may not be satisfied with parent class method implementation. The child class is
allowed to redefine that method based on its requirement. This process is called method overriding.

Rules for method overriding:


• The method of the parent class and the method of the child class must have the same name.
• The method of the parent class and the method of the child class must have the same parameters.
• It is possible through inheritance only.
Polymorphism >> Function Overriding

You might also like