0% found this document useful (0 votes)
4 views17 pages

Unit 5th Computer Programing IVth Sem EE Branch

The document covers the concepts of polymorphism and overloading in object-oriented programming, explaining static (compile-time) and dynamic (run-time) polymorphism. It details method and operator overloading, including their advantages and disadvantages, as well as the significance of function overriding. The document emphasizes the importance of these concepts in enhancing code flexibility, readability, and reusability.

Uploaded by

vedumonyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Unit 5th Computer Programing IVth Sem EE Branch

The document covers the concepts of polymorphism and overloading in object-oriented programming, explaining static (compile-time) and dynamic (run-time) polymorphism. It details method and operator overloading, including their advantages and disadvantages, as well as the significance of function overriding. The document emphasizes the importance of these concepts in enhancing code flexibility, readability, and reusability.

Uploaded by

vedumonyt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

COMPUTER PROGRAMMING

Electrical Engineering Branch IVth Sem 4EE4-05

Unit 5th

Basics of Polymorphism and Overloading

Introduction:
Polymorphism: Binding,
Computer Programming 1

Static polymorphism and Dynamic polymorphism Overloading (Operator and Function)


This pointer,
Applications of this pointer,
Operator function,
Member and nonmember operator function,
Function overloading,
Operator overloading

Computer Programming By Kapil Raj Pokhra


Computer Programming 2

Polymorphism
Polymorphism is a core concept in object-oriented programming (OOP) that allows objects
of different classes to be treated as objects of a common superclass. The word polymorphism
means "many forms," and in programming, it refers to the ability of different classes to respond
to the same method call in a way appropriate to their own behavior.

Overloading (Compile-time Polymorphism)


Method Overloading is when a class has multiple methods with the same name but different
parameters.

Overloading allows you to define multiple methods with the same name but different
parameter lists (number, type, or order of parameters) within the same class. The
compiler selects the correct method to invoke based on the arguments passed during the
method call. Overloading is a form of compile-time polymorphism.

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. Polymorphism
is one of the most important concepts of object-oriented programming languages. 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. Polymorphism is considered
as one of the important features of Object Oriented Programming.
Polymorphism is the key power of object-oriented programming. It is so important
that languages that don't support polymorphism cannot advertise themselves as Object-
Oriented languages. Languages that possess classes but have no ability of polymorphism are
called object-based languages. Thus it is very vital for an object-oriented programming
language. It is the ability of an object or reference to take many forms in different instances.
It implements the concept of function overloading, function overriding and virtual functions.

Computer Programming By Kapil Raj Pokhra


Computer Programming 3

Types of Polymorphism
1. Compile-Time Polymorphism (Static Binding / early binding): Compile time
polymorphism. Also known as static polymorphism, compile time polymorphism is
common in OOP languages like Java. It uses method overloading to create multiple
methods that have the same name in the same class, but a different number of
parameters. They may also have parameters for different data types.
o Achieved through method overloading or operator overloading.
o Decision is made at compile time.

Static polymorphism is implemented in Object-Oriented Programming languages through


Method Overloading, which allows the programmer to implement numerous methods.
Although their names may be the same, their specifications are not. Static polymorphism
thrives under the following conditions:
1. Types of All Parameters should be different.
2. The sequence of the Parameters can be different.
3. The number of parameters of one method should differ from the other method.

A. Function Overloading:
Function overloading is a feature of object-oriented programming where two or more
functions can have the same name but behave differently for different parameters. Such
functions are said to be overloaded; hence, this is known as Function Overloading.
Functions can be overloaded either by changing the number of arguments or changing the
type of arguments.

Computer Programming By Kapil Raj Pokhra


Computer Programming 4

C++ function overloading allows you to define multiple functions with the same name but
different parameters. It is a form of compile time polymorphism in which a function can
perform different jobs based on the different parameters passed to it. It is a feature of object-
oriented programming that increases the readability of the program.

How Function Overloading Works?


Function overloading in C++ allows multiple functions to have the same name but differ in
the number or type of their parameters. The compiler determines the correct overloaded
function by evaluating the function signature which consist of the function's name and
parameter types and matches it with the passed arguments in the function call.

 During this process the compiler tries to find an exact match where both the number of
arguments and the type of the arguments exactly match the passed parameters.
 If the compiler doesn't find an exact match, then it tries to find a match by performing
type promotion or type conversion of the data type of the passed parameters.
 If the compiler is unable to find a match for the called function or finds multiple matches
for the called function, then it generates an error.

Computer Programming By Kapil Raj Pokhra


Computer Programming 5

Different Ways of Function Overloading


A function in C++ can be overloaded in three different ways:
 By having different number of parameters.
 By having different types of parameters.
 By having both different number and types of parameters.
Example 2:

Computer Programming By Kapil Raj Pokhra


Computer Programming 6

B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for particular data type,
this ability is known as operator overloading. For example, we can make use of
the addition operator (+) for string to concatenate two strings and for integer to add two
integers. The << and >> operator are binary shift operators but are also used with input
and output streams. This is possible due to operator overloading.
Operator overloading in object-oriented programming (OOP) allows you to redefine
the behavior of standard operators (like +, -, *, /, ==, etc.) when they are used with user-
defined data types (objects). This enables you to make your classes behave more intuitively
and naturally, similar to how built-in data types work.

Other Operators that can be Overloaded:


 Arithmetic operators: +, -, *, /, %

Computer Programming By Kapil Raj Pokhra


Computer Programming 7

 Comparison operators: ==, !=, <, >, <=, >=


 Assignment operators: =, +=, -=, *=, /=, etc.
 Increment/decrement operators: ++, --
 Input/output stream operators: <<, >>

Advantages of Operator Overloading:


 Improved Readability and Intuitiveness: Operator overloading can make the code more
readable and intuitive. For example, using + to add two complex numbers is more
intuitive than calling a method like addComplex.
 Polymorphism: Operator overloading supports polymorphism, allowing the same
operator to have different meanings based on the data types of operands.

Computer Programming By Kapil Raj Pokhra


Computer Programming 8

 Consistency with Built-in Types: It allows user-defined types to behave like built-in
types, providing a consistent interface. This can make it easier for users of the class to
understand and use the class without learning a new set of methods.
 Code Reusability: By overloading operators, we can reuse the same operators for
different data types, reducing code redundancy. For example, the + operator can be used
for both integers and complex numbers.
 Encapsulation and Abstraction: It allows complex operations to be encapsulated within
classes, promoting abstraction and hiding implementation details. Users can interact with
objects using simple operators without needing to know the underlying implementation.
Disadvantages of Operator Overloading:
 Complexity and Maintenance: Overloading too many operators can make the code
complex and harder to maintain. It can be challenging to understand the behavior of
overloaded operators, especially in large codebases or when multiple operators are
overloaded.
 Potential for Misuse: There is a risk of misuse, where operators are overloaded in ways
that are not intuitive or logical. This can lead to code that is difficult to read and
understand, as the overloaded operators may not behave as expected.
 Performance Overhead: Operator overloading can introduce a slight performance
overhead because the overloaded operators are essentially function calls. While this
overhead is usually minimal, it can be a concern in performance-critical applications.

Conclusion:
Operator overloading is a powerful tool in object-oriented programming, providing a way to
extend the capabilities of custom objects to support intuitive operations. By defining how
operators work with custom types, developers can write more natural and readable code.
However, it should be used judiciously to maintain code clarity and prevent
misunderstandings.

2. Run-Time Polymorphism (Dynamic Binding/ Late binding/ Method overriding): Also


known as dynamic polymorphism, runtime polymorphism uses method overriding to let a child
class have its own definition of a method belonging to the parent class. This type of
polymorphism is often associated with upcasting, which happens when a parent class points to
an instance of the child’s class.
 Achieved through method overriding.

Computer Programming By Kapil Raj Pokhra


Computer Programming 9

 Decision is made at runtime based on the object.

In runtime polymorphism, the compiler resolves the object at run time and then it decides
which function call should be associated with that object. It is also known as dynamic or late
binding polymorphism. This type of polymorphism is executed through virtual functions and
function overriding. All the methods of runtime polymorphism get invoked during the run
time.
Method overriding is an application of run time polymorphism where two or more
functions with the same name, arguments, and return type accompany different classes of the
same structure. This method has a comparatively slower execution rate than compile-time
polymorphism since all the methods that need to be executed are called during run time.
Runtime polymorphism is known to be better for dealing with complex problems since all the
methods and details turn up during the runtime itself.

The implementation of run time polymorphism can be achieved in two ways:


1. Function overriding
2. Virtual functions

A. Function overloading:
Function overloading is an important pillar of polymorphism in C++. When the same function
name is overloaded with multiple tasks, that function gets overloaded. In other words, function
overloading takes place when you create two functions of the same name and these functions
serve different purposes. Both functions should have the same return types and strictly different
arguments from each other. It is achieved during the compile time. Function overloading can
take place both in the base class as well as in the derived class.
All the functions that have been overloaded share the same scope. Since it is
achievable in both the base class and the derived class, you do not specifically need
inheritance.

Computer Programming By Kapil Raj Pokhra


Computer Programming 10

B: Operator overloading:
Function overriding occurs when a derived class provides a specific implementation for a
function already defined in its base class. This allows objects of the derived class to use their
version of the function, providing a way to achieve runtime Polymorphism.
Example:

Computer Programming By Kapil Raj Pokhra


Computer Programming 11

Computer Programming By Kapil Raj Pokhra


Computer Programming 12

Explanation:
This code has a base class Car with a virtual function ‘displayColor()’. Two derived
classes, RedCar and BlueCar, override this function to provide their implementations of
displaying the car color.

In the main() function, we create pointers to objects of type Car pointing to instances of
RedCar and BlueCar. When calling the displayColor() function through these pointers, the
appropriate overridden version of the function is invoked based on the actual object type,
demonstrating runtime Polymorphism.

Summary Table
Feature Polymorphism Overloading (Compile- Overriding (Runtime
time Polymorphism) Polymorphism)
Concept Ability of an object/method Multiple methods with same Subclass provides specific
to take many forms. name, different params. implementation of superclass
method.
Mechanism Achieved through Method signature (name + Same method signature as
Overloading and Overriding. parameters) matters. superclass.
Binding Compile-time (for Compile-time (Early Runtime (Late Binding).
Time Overloading), Runtime (for Binding).
Overriding).
Relationship Between methods within the Between methods in a
same class. superclass and its subclass.
Inheritance Not directly required for Required for overriding.
overloading.
Use Case Flexibility and code Providing multiple ways to Customizing inherited behavior
reusability. perform a similar operation. for specific subclasses.

Operator function in oops with example


In Object-Oriented Programming (OOP), operator functions, also known as operator
overloading, allow you to extend the meaning of built-in operators to work with user-
defined data types (classes). This means you can use operators like +, -, =, etc., on
objects of your classes in a way that makes sense for those objects.

Example:

Computer Programming By Kapil Raj Pokhra


Computer Programming 13

Explanation:

Computer Programming By Kapil Raj Pokhra


Computer Programming 14

1. Complex operator+(const Complex& other):


This defines the overloaded + operator. It takes a Complex object (other) as an argument and
returns a new Complex object representing the sum of the current object and other.
2. friend std::ostream& operator<<(std::ostream& os, const Complex& c):
This defines the overloaded << operator for output. It allows you to print Complex objects
directly using std::cout.
3. c3 = c1 + c2;:
This line uses the overloaded + operator to add the c1 and c2 objects. The operator+ function
is called, and the result is assigned to c3.
4. std::cout << "c3 (c1 + c2): " << c3 << std::endl;:
This line uses the overloaded << operator to print the c3 object to the console.
Key Concepts:

Member and nonmember operator function


A non-member function always appears outside of a class. The member function can appear outside
of the class body (for instance, in the implementation file or the . cpp file). But, when you do this, the
member function must be qualified by the name of its class.
There are two major differences.
1. A non-member function always appears outside of a class.
The member function can appear outside of the class body (for instance, in the
implementation file or the .cpp file). But, when you do this, the member function must
be qualified by the name of its class. This is to identify that that function is a member of
a particular class.
For instance, if you took our MyArray class and wanted to define the implementation of
a new member function called myFunction in the .cpp file.

2. Another difference between member functions and non-member functions is how they are
called (or invoked) in the main routine. Consider the following segment of code:

Computer Programming By Kapil Raj Pokhra


Computer Programming 15

Computer Programming By Kapil Raj Pokhra


Computer Programming 16

Unit Vth
Basics of Polymorphism and Overloading:
Assignment:
1.
.

Computer Programming By Kapil Raj Pokhra

You might also like