Polymorphism in Object Oriented Programming c++

 Polymorphism is a fundamental concept in object-oriented programming (OOP) and is one of the four pillars of OOP, along with encapsulation, inheritance, and abstraction. The term polymorphism comes from the Greek words "poly" meaning many and "morph" meaning form. In the context of OOP, polymorphism refers to the ability of an object to take on many forms. In other words, it allows an object to be treated as an instance of its base class or any of its derived classes. In this article, we will discuss polymorphism in the context of C++, one of the most popular programming languages used for OOP.

C++ is an object-oriented programming language that is widely used for developing applications in various domains such as gaming, scientific computing, and artificial intelligence. C++ is known for its powerful features, such as its support for inheritance and polymorphism. In C++, polymorphism is achieved through the use of virtual functions and function overloading.

Virtual Functions

In C++, a virtual function is a function that is declared as virtual in the base class and overridden in the derived class. A virtual function allows an object to take on different forms depending on the type of the object. For example, consider a base class called Shape that has a virtual function called Draw(). The Draw() function is overridden in two derived classes, Circle and Square. When an object of the Circle class is created, the Draw() function in the Circle class is called, and when an object of the Square class is created, the Draw() function in the Square class is called.

The following is an example of how virtual functions are used in C++ to achieve polymorphism:

class Shape {
public:
virtual void Draw() = 0;
};

class Circle : public Shape {
public:
void Draw() {
cout << "Drawing a Circle" << endl;
}
};

class Square : public Shape {
public:
void Draw() {
cout << "Drawing a Square" << endl;
}
};

int main() {
Shape* s1 = new Circle();
Shape* s2 = new Square();
s1->Draw();
s2->Draw();
return 0;
}


In this example, the Shape class is the base class, and the Circle and Square classes are the derived classes. The Draw() function is declared as virtual in the Shape class and overridden in the Circle and Square classes. When an object of the Circle class is created, the Draw() function in the Circle class is called, and when an object of the Square class is created, the Draw() function in the Square class is called. This is an example of how polymorphism is achieved through the use of virtual functions in C++.

Function Overloading

In C++, function overloading is another way to achieve polymorphism. Function overloading is a feature that allows multiple functions to have the same name but with different parameters. The correct function to call is determined at compile-time based on the parameters passed to the function. For example, consider a class called Calculator that has two functions called Add() with different parameters.

The following is an example of how function overloading is used in C++ to achieve polymorphism:

class Calculator {
public:
int Add(int a, int b) {
return a + b;
}

double Add(double a, double b) { return a + b; }

};

int main() { Calculator c

 

Polymorphism in Object Oriented Programming c++