Polymorphism
Polymorphism
FYBCA - D
Group Members
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
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
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
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
C++ Polymorphism:
https://www.geeksforgeeks.org/polymorphism-
in-c/
Function Overloading:
https://www.geeksforgeeks