Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY

Hamdard Institute of Engineering & Technology


Hamdard University
Lab # 08

Introduction to Object Oriented Programming


(Polymorphism)

Objective
 To get introduced with the concept of polymorphism.
 Explore the different types of polymorphism.

Theory

Polymorphism
Polymorphism is a concept by which we can perform a single action in different ways.
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many
and "morphs" means forms. So polymorphism means many forms.

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 possess different behavior in different situations. This is called polymorphism.

In any language, polymorphism is mainly divided into two types:

 Compile time Polymorphism


 Compile time polymorphism: It is also known as static polymorphism. This type of
polymorphism is achieved by function overloading or operator overloading.

Page | 1
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Method Overloading
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.

Example # 1:
#include <iostream>
#include <string>

class Printdata {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
}

void print(double f) {
cout << "Printing float: " << f << endl;
}

void print(const std::string& s) {


cout << "Printing string: " << s << endl;
}
};

int main() {
Printdata p;

// Call print to print integer


p.print(5);

// Call print to print float


p.print(500.263);

// Call print to print string


p.print("Hello C++");

return 0;
}

Output:

Method Overriding

Page | 2
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Method overriding is a concept of object oriented programming that allows us to change the
implementation of a function in the child class that is defined in the parent class. Here, the
method in a derived class has the same name and the same number of arguments as the base
class. Base class's method is called overridden method and the derived class method is called
overriding method.

Example # 2
using System;
namespace MyApplication
{
class Animal // Base class (parent)
{
public void animalSound()
{
Console.WriteLine("The animal makes a sound");
}
}
class Dog : Animal // Derived class (child)
{
public void animalSound()
{
Console.WriteLine("The dog says: bow bow");
}
}
class Cat : Animal // Derived class (child)
{
public void animalSound()
{
Console.WriteLine("The Cat says: meow meow");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal();
Animal myDog = new Dog(); // Create a Pig object
Animal myCat = new Cat(); // Create a Dog object

myAnimal.animalSound();
myDog.animalSound();
myCat.animalSound();
}
}
}

Page | 3
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Output:
The animal makes a sound
The dog says: bow bow
The cat says: meow meow

The animalSound() function is called on three different objects: myAnimal of type Animal,
myDog of type Dog, and myCat of type Cat. Since animalSound() is overridden in both Dog
and Cat classes, the appropriate implementation based on the object's type will be called.

The virtual keyword is not necessary in this example as we're not dealing with polymorphism or
overriding functions.

C++ provides an option to override the base class method, by adding the virtual keyword to the
method inside the base class, and by using the override keyword for each derived class methods:

Example # 3
#include <iostream>
using namespace std;

class Animal {
public:
virtual void animalSound() {
cout << "The animal makes a sound\n";
}
};

class Dog : public Animal {


public:
void animalSound() override {
cout << "The dog says: bow bow\n";
}
};

class Cat : public Animal {


public:
void animalSound() override {
cout << "The cat says: meow meow\n";
}
};

int main() {
Animal* myAnimal = new Animal();
Animal* myDog = new Dog();
Animal* myCat = new Cat();

myAnimal->animalSound();

Page | 4
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
myDog->animalSound();
myCat->animalSound();

delete myAnimal;
delete myDog;
delete myCat;

return 0;
}

Output:

The animal makes a sound

The dog says: bow bow

The cat says: meow meow

In the C++ code, we use the virtual keyword in the base class Animal for the animalSound()
function, and in the derived classes Dog and Cat, we use the override keyword to explicitly
indicate that we are overriding the base class function. We also use dynamic memory allocation
(new) for creating objects, and at the end, we delete the allocated objects using delete.

Explanation:

The animalSound() function in the Animal class is called using the myAnimal object, and it
prints "The animal makes a sound".
The animalSound() function in the Dog class is called using the myDog object, and it prints "The
dog says: bow bow".
The animalSound() function in the Cat class is called using the myCat object, and it prints "The
cat says: meow meow".
Since the animalSound() function is marked as virtual in the base class and overridden in the
derived classes, the appropriate version of the function is called based on the actual object type
during runtime (polymorphism).

Page | 5
FACULTY OF ENGINEERING SCIENCES AND TECHNOLOGY
Hamdard Institute of Engineering & Technology
Hamdard University
Lab Task
6.1 Develop a class „Animal‟ that takes name of animal as an argument to default constructor. The
class should be implemented in such a way that it becomes mandatory for all child classes to
implement „talk‟ method if they want to instantiate the object. The child classes (cat and dog) should
return the relevant voices through „talk‟ method

6.2 Develop a class „Person‟. The super class has a method „ticket_price‟ which should be
implemented in such a way that it becomes mandatory for all child classes to implement „ticket_price‟
method if they want to instantiate the object. The child classes (Employed_personand student) should
return the relevant ticket prices after concession applicable to these categories.

NOTE: Attach printouts of above mentioned task with your name and roll number in header or
footer.

Learning outcomes:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Page | 6

You might also like