C++ Polymorphism

Polymorphism is a core concept in object-oriented programming, and it is supported in C++ through the use of virtual functions and function overloading. In this article, we will explore the different types of polymorphism in C++ and how they can be implemented in your code.

Function Overloading

Function overloading, also known as compile-time polymorphism, allows multiple functions with the same name to be defined within a class, as long as their parameter lists are different. The correct function to be called is determined at compile-time based on the types and number of arguments passed to the function. Here is an example of function overloading in C++:

class Calculator {
   public:
      int add(int a, int b) {
         return a + b;
      }
      float add(float a, float b) {
         return a + b;
      }
};
int main() {
Calculator c;
cout << c.add(1, 2) << endl; // Output: 3
cout << c.add(1.5f, 2.5f) << endl; // Output: 4.0
return 0;
}

In this example, the class Calculator has two functions named add(), one that takes two integers as arguments and returns their sum, and another that takes two floats as arguments and returns their sum. The correct function to be called is determined at compile-time based on the types of arguments passed to the function. In the main function, the first call to add() with integer arguments causes the first function to be called, and the second call with float arguments causes the second function to be called.

Virtual Functions

Virtual functions, also known as runtime polymorphism, allow a base class pointer or reference to point to an object of a derived class, and the correct function to be called is determined at runtime based on the type of the object being pointed to. To create a virtual function in C++, the virtual keyword is used in the function declaration within the base class. Here is an example of virtual functions in C++:

class Shape {
   public:
      virtual double getArea() {
         return 0;
      }
};
class Rectangle: public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) {
width = w;
height = h;
}
double getArea() {
return width * height;
}
};

class Circle: public Shape {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double getArea() {
return 3.14159 * radius * radius;
}
};

int main() {
Shape *shape;
Rectangle rect(3, 4);
Circle circle(5);
shape = &rect;
cout << "Rectangle area: " << shape->getArea() << endl;
shape = &circle;
cout << "Circle area: " << shape->getArea() << endl;
return 0;
}

In this example, we have a base class Shape with a virtual function named getArea(). The class Rectangle and Circle are derived from Shape and they both have their own implementation of the getArea() function. In the main function, we create a pointer of type Shape and initialize it with the address of a Rectangle and Circle object respectively. When we call the getArea() function on the shape pointer, the correct implementation of the function is called at runtime based on the type of the object being pointed to. In the first call, the getArea() function of the Rectangle class is called, and in the second call, the getArea() function of the Circle class is called.

Pure Virtual Functions

A pure virtual function is a virtual function that has no implementation in the base class, and it is used to create an abstract base class. An abstract base class cannot be instantiated, and it can only be used as a base class for other classes. To create a pure virtual function in C++, the virtual keyword is used in the function declaration in the base class, followed by the = 0 notation. Here is an example of pure virtual functions in C++:

class Shape {
   public:
      virtual double getArea() = 0;
};
class Rectangle: public Shape {
private:
double width;
double height;
public:
Rectangle(double w, double h) {
width = w;
height = h;
}
double getArea() {
return width * height;
}
};
class Circle: public Shape {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double getArea() {
return 3.14159 * radius * radius;
}
};

In this example, the class Shape is an abstract base class with a pure virtual function named getArea(). The class Rectangle and Circle are derived from Shape and they both provide an implementation of the getArea() function. Because the Shape class has a pure virtual function, it cannot be instantiated, and it can only be used as a base class for other classes. The derived classes Rectangle and Circle can be instantiated, and they provide the necessary implementation for the pure virtual function in the base class.

Conclusion

Polymorphism is a powerful feature in C++ that allows you to write code that is more flexible and extensible. By using function overloading, virtual functions, and pure virtual functions, you can create classes that can be used in a variety of situations, and the correct behavior is determined at compile-time or runtime based on the types of objects or arguments being used. Understanding and utilizing polymorphism in your code can lead to more efficient and maintainable codebase.

Leave a Reply

Your email address will not be published. Required fields are marked *