0% found this document useful (0 votes)
69 views7 pages

Assignment 6 Polymorphism Arid

1. The document discusses polymorphism and provides examples of different types including function overloading and overriding. 2. Function overloading and operator overloading are examples of compile-time polymorphism, while function overriding is an example of runtime polymorphism. 3. Three tasks are provided as examples: the first demonstrates function overriding using classes for computers, desktops, and laptops; the second demonstrates overriding using classes for patients and medicare patients; the third demonstrates function overloading by implementing math functions for int and double types.

Uploaded by

Rana Awais
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)
69 views7 pages

Assignment 6 Polymorphism Arid

1. The document discusses polymorphism and provides examples of different types including function overloading and overriding. 2. Function overloading and operator overloading are examples of compile-time polymorphism, while function overriding is an example of runtime polymorphism. 3. Three tasks are provided as examples: the first demonstrates function overriding using classes for computers, desktops, and laptops; the second demonstrates overriding using classes for patients and medicare patients; the third demonstrates function overloading by implementing math functions for int and double types.

Uploaded by

Rana Awais
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/ 7

Riphah International University

I-14 Main Campus


Faculty of Computing

Class: Spring-2020 Subject: OOP


Course Code: CS2104 Class/Lab Awais Nawaz
Instructor:

-------------------- LAB 10 --------------------


Polymorphism
Learning Objective:
1. What is Polymorphism
2. Types of Polymorphism
3. Function Overloading and Operator Overloading
4. What is Function Overriding

What is Polymorphism:

The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of a message to be displayed in more than one form. Real life
example of polymorphism, a person at the same time can have different characteristic. Like a
man at the same time is a father, a husband, an employee. So, the same person poses different
behavior in different situations. This is called polymorphism. Polymorphism is considered as one
of the important features of Object-Oriented Programming.

Types of Polymorphism:

In C++ polymorphism is mainly divided into two types:

 Compile time Polymorphism (This type of polymorphism is achieved by function


overloading or operator overloading.)
 Runtime Polymorphism (This type of polymorphism is achieved by Function
Overriding.)
Function Overloading:
Function overloading is a type of compile time polymorphism. When there are multiple functions
with same name but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of arguments or/and change in type of
arguments.
Operator Overloading:
Operator overloading is a type of compile time polymorphism. C++ also provide option to
overload operators. For example, we can make the operator (‘+’) for string class to concatenate
two strings. We know that this is the addition operator whose task is to add two operands. So a
single operator ‘+’ when placed between integer operands, adds them and when placed between
string operands, concatenates them.
Function is Overriding:
Function overriding is a type of runtime polymorphism. When there are multiple functions with
same name with same parameters then these functions are said to be overridden. If you create an
object of the derived class and call the member function which exists in both classes (base and
derived), the member function of the derived class is invoked and the function of the base class is
ignored.
Lab Task(s)

Task 1:

Consider a class Computer having

 Two fields (i.e. companyName, price) and


 A single function named show()

A class named Desktop inherits Computer class and adds fields representing

 color, monitor size, and processor type and


 Override function named show() to display values of its all attributes

A class named Laptop inherits Computer class and adds fields representing

 color, size, weight, and processor type and


 Override function named show() to display values of its all attributes

Write a main() function that instantiates objects of derived classes to access respective show()
function using dynamic binding.

Task 2:

Create a class named Person, which contains

 A function named print()


 Two data fields i.e. personName and age

A class named Patient inherits Person class, which contains

 Two data fields i.e. diseaseType and recommendedMedicine


 Overridden function print() to display all details relevant to a patient

A class named MedicarePatient inherited from class Patient, which holds

 A data field representing the name of the hospital


 A data filed representing the name of the ward
 A data field representing room number
 Overridden function print() to display all details relevant to a patient
In the main function, create instances of derived classes to access respective print() function
using dynamic binding.

Task 3:

Consider a class named Calculator with typical four specific functionalities i.e. addition,
subtraction, multiplication, and division. Implement these functionalities as four functions with
two parameters. It is also required to overload all these functions for int and double data types.
In the main function, create an object of class Calculator and invoke its member functions while
passing parameters of int and double type.

 A person has a name, address, phone number, and email address.


 A student has a status (String)
 An employee has an office, salary, and date hired. Use the Date class to create an object for
date hired.
 A faculty member has office hours and a rank.
 A staff member has a title.
 Create display method in each class

You might also like