0% found this document useful (0 votes)
3 views31 pages

Polymorphism (2)

The document outlines the concepts of object-oriented programming, focusing on polymorphism, inheritance, and function overloading and overriding. It describes a scenario with a base class 'Employee' and derived classes 'Developer' and 'Tester', detailing their specific functionalities and behaviors. Additionally, it explains the importance of parameterized constructors, default arguments, and the distinctions between compile-time and run-time polymorphism.

Uploaded by

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

Polymorphism (2)

The document outlines the concepts of object-oriented programming, focusing on polymorphism, inheritance, and function overloading and overriding. It describes a scenario with a base class 'Employee' and derived classes 'Developer' and 'Tester', detailing their specific functionalities and behaviors. Additionally, it explains the importance of parameterized constructors, default arguments, and the distinctions between compile-time and run-time polymorphism.

Uploaded by

voyofof931
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TAke Out A paper and solve the task.

It’s a marked Activity

Ma
Suppose you are a programmer working for a software house and you have a base class called
Employee with a data member emp_id and member function work().
The work() function performs the tasks assigned to the employee based on their job
responsibilities. //just print I am working

You have two types of employees: Developer and Tester. Both types of employees inherit from
the Employee base class.

Developer has a member function called write_code() that takes in a programming language
parameter and writes code in the specified language (print I am developing). Tester has a
member function called test_code() that tests the code(print I am testing).

Make Parameterized constructors for all classes.


Polymorphism
Polymorphism
The process of representing one Form in multiple forms is
known as Polymorphism

Polymorphism is derived from 2 Greek words: poly and morphs.


The word "poly" means many and morphs means forms. So
polymorphism means many forms.
Real life example of Polymorphism
Suppose if you are in class room that time you behave like a
student, when you are in market at that time you behave like
a customer, when you at your home at that time you behave
like a son or daughter, Here one person have
different-different behaviors.
Polymorphism in OOP
Polymorphism refers to the ability of a method to be used in different
ways, that is, it can take different forms at different times (poly +
morphos).

Polymorphism enables us to “program in the general” rather than


“program in the specific.”
Types Of Polymorphism
Compile time / Static / Early Binding polymorphism
Static / Compile time polymorphism
It happens where more than one methods share the same name
with different parameters or signature and different return
type.

It is known as Early Binding because the compiler is aware


of the functions with same name and also which overloaded
function is to be called is known at compile time.
Constructor OverLoading

We discussed this already.


Function OverLoading
C++ enables several functions of the
same name to be defined, as long as they
have different parameter signatures.

The compiler selects the proper function


to call by examining the number, types
and order of the arguments in the call.
Function OverLoading
Function overloading allows the program
to follow the principle of
“Polymorphism”

The return type or name of parameters


don’t help the compiler in selecting
which overloaded function to call.
Overload the function write code with 1 programming language or
2 programming language to develop.

Overload the function test code with 1 programming language or 2


programming language to test.
Default Argument
A default argument is a value provided
in a function declaration that is
automatically assigned by the compiler
if the caller of the function doesn’t
provide a value for the argument with a
default value.

Default arguments must be the rightmost


(trailing) arguments in a function’s
parameter list. default arguments are
assigned from right to left.

Once we provide a default value for a


parameter, all subsequent parameters
must also have default values.
Function OverLoading
Avoid using default arguments with overloaded functions as it can be dangerous
i.e. can lead to error conditions that are hard to trace.
#include <iostream>
using namespace std;

class printData {
public:
void print() {
cout << "Printing default " << endl;}
void print(float f) {
cout << "Printing float: " << f << endl; }};

int main(void) {
printData pd;
[Link](); // Call print to print default
[Link](5.6); // Call print to print float
return 0;}
Function OverLoading
Avoid using default arguments with overloaded functions as it can be dangerous
i.e. can lead to error conditions that are hard to trace.
#include <iostream>
using namespace std;

class printData {
public:
void print() {
cout << "Printing default " << endl;}
void print(float f=0) {
cout << "Printing float: " << f << endl; }};

int main(void) {
printData pd;
[Link](); //[Error] call of overloaded 'print()' is ambiguous
[Link](5.6); // Call print to print float
return 0;}
Function OverLoading
A const member function can be overloaded with a non-const
version.

The compiler chooses which overloaded member function to use


based on the object on which the function is invoked. If the
object is const,the compiler uses the const version. If the
object is not const, the compiler uses the non-const
version.
Function OverLoading
#include <iostream>
using namespace std;

class printData {
public:
void print() {
cout << "Printing default Function" << endl;}
void print() const{
cout << "Printing const Function " << endl;}
};

int main(void) {
printData pd;
const printData p;
[Link](); // Call print to print default
[Link](); // Call print to const Function
return 0;}
Operator OverLoading

We will discuss this next week.


Templates

We will discuss this later.


Run time / Dynamic / late Binding polymorphism
Run time / Dynamic / late Binding polymorphism
This refers to the entity which changes its form depending
on circumstances at runtime. This concept can be adopted as
analogous to a chameleon changing its color at the sight of
an approaching object.

Method Overriding uses runtime Polymorphism.

It is also called Late Binding.


Function OverRiding
When overriding a method, the behavior of the method is
changed for the derived class.
A class may need to override the default behavior provided by its base class.
• Provide behavior specific to a derived class
• Extend the default behavior
• Restrict the default behavior
• Improve performance
Function OverRiding
Function OverRiding
Function OverRiding - Specific Behaviour
When overriding a method, the behavior of the method is
changed for the derived class.
Function OverRiding - Improve Performance
When overriding a method, the behavior of the method is
changed for the derived class.
Function Overriding
#include <iostream>
using namespace std;

class base_class
{ int main()
public: {
void findArea(int side) derived_class obj;
{cout << "Base::The area is: "<<(side * side) << "\n\n"; } [Link](5);
void findArea(int x, int y) [Link](5,4);
{cout << "Base::The area is: "<<(x * y) << "\n\n"; }
obj.base_class::findArea(6
};
);
class derived_class : public base_class return 0;
{
public: }
void findArea(int side)
{ cout << "Derived::The area is: "<<(side * side) << "\n\n"; }
void findArea(int x, int y)
{cout << "Derived::The area is: "<<(x * y) << "\n\n"; }

};
Make a function attend_meeting() in Programmer class. (print I
am attending as a Programmer)

Both Developer and Tester will override the function called


attend_meeting() that allows them to attend team meetings and
provide updates on their progress and any issues they may be
facing.
(print I am attending as a Developer/Tester)
However, Developer has an overloaded attend_meeting() function
that takes in a presentation parameter and presents their work
to the team, while Tester has an overloaded attend_meeting()
function that takes in a question parameter and answers any
questions related to testing or quality assurance.
OverLoading Vs OverRiding

You might also like