Unit 5th Computer Programing IVth Sem EE Branch
Unit 5th Computer Programing IVth Sem EE Branch
Unit 5th
Introduction:
Polymorphism: Binding,
Computer Programming 1
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 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.
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.
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.
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.
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.
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.
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.
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.
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.
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:
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.
Example:
Explanation:
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:
Unit Vth
Basics of Polymorphism and Overloading:
Assignment:
1.
.