0% found this document useful (0 votes)
17 views21 pages

Polymorphism

The document discusses polymorphism in Object-Oriented Programming (OOP), specifically in C++, highlighting both compile-time and runtime polymorphism. Compile-time polymorphism is achieved through function and operator overloading, while runtime polymorphism is achieved through function overriding and virtual functions. The document provides examples for each type of polymorphism to illustrate their implementation.

Uploaded by

zdeshmukh26
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)
17 views21 pages

Polymorphism

The document discusses polymorphism in Object-Oriented Programming (OOP), specifically in C++, highlighting both compile-time and runtime polymorphism. Compile-time polymorphism is achieved through function and operator overloading, while runtime polymorphism is achieved through function overriding and virtual functions. The document provides examples for each type of polymorphism to illustrate their implementation.

Uploaded by

zdeshmukh26
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
You are on page 1/ 21

Polymorphism

FYBCA - D
Group Members

Zeenat Deshmukh – 220105011417


Shraddha Waghmare – 220105011416
Sanika Puri – 220105011414
Pratik Chaudhary - 220105011415
Introduction

Polymorphism is an important concept in Object-


Oriented Programming (OOP)
C++ supports both compile-time and runtime
polymorphism
Polymorphism allows the same method to be used
with different data types
Compile-time Polymorphism

 Also known as Static Polymorphism.


 Achieved through Function Overloading and Operator
Overloading
 Function Overloading allows multiple functions to have the same
name but different arguments
 Operator Overloading allows an operator to have multiple
meanings based on the context of its usage
Function Overloading

Syntax: void functionName(int a), void functionName(double b)


Example

void display(int a)
{
cout << "The integer value is: " << a << endl;
}
void display(double b)
{
cout << "The double value is: " << b << endl;
}
The appropriate function is called
based on the argument type passed
during function call.
Operator Overloading

Syntax: returnType operator symbol (parameters)


Example

class Box
{
public: double getVolume()
{
return length * breadth * height;
}
Box operator+(const Box& b)
{
Box box;
box.length = this->length + b.length;
box.breadth = this->breadth + b.breadth;
box.height = this->height + b.height;
return box;
}
private: double length;
double breadth;
double height;
};
The + operator is overloaded
to add two Box objects.
Run-time Polymorphism

Also known as Dynamic Polymorphism


Achieved through Function Overriding
Function Overriding allows a derived class to
provide its own implementation of a method
that is already defined in the base class
Function Overriding

Syntax: returnType functionName(parameters) { }


Example

class Animal
{ public:
virtual void makeSound()
{ cout << "The animal makes a sound" << endl; } };
class Dog : public Animal
{ public:
void makeSound()
{ cout << "The dog barks" << endl; } };
The makeSound() method is
overridden in the derived class
Dog.
Virtual Functions

A virtual function is a function that is declared in the


base class and is redefined in the derived class
The virtual keyword is used to declare a function as
virtual
Virtual functions allow the correct function to be
called at runtime
Virtual Functions Example

class Shape {
public:
virtual double getArea() {
return 0;
}
};
class Circle : public Shape {
public:
Circle(double r) {
radius = r;
}
double getArea() {
return 3.14 * radius * radius;
}
private:
double radius;
};
The getArea() method is declared
as virtual in the base class and is
overridden in the derived class
Circle.
Conclusion

Polymorphism is an important concept in OOP.


C++ supports both compile-time and runtime
polymorphism.
Compile-time polymorphism is achieved through Function
Overloading and Operator Overloading.
Runtime polymorphism is achieved through Function
Overriding and Virtual Functions.
References

C++ Polymorphism:
https://www.geeksforgeeks.org/polymorphism-
in-c/
Function Overloading:
https://www.geeksforgeeks

You might also like