Polymorphism in C++: Function Overloading
Polymorphism is a core concept in object-oriented programming, allowing methods to perform in different ways based on the object that calls them. In C++, function overloading is a type of polymorphism that enables multiple functions to have the same name but differ in parameters. This capability enhances code readability and flexibility.
Here, we present three practical examples of polymorphism in C++ through function overloading.
Example 1: Calculate Area
Context
This example demonstrates how to calculate the area of different shapes (circle and rectangle) using function overloading.
#include <iostream>
#include <cmath>
class Area {
public:
// Function to calculate area of a circle
double calculateArea(double radius) {
return M_PI * radius * radius;
}
// Function to calculate area of a rectangle
double calculateArea(double length, double width) {
return length * width;
}
};
int main() {
Area shape;
double circleArea = shape.calculateArea(5.0); // Area of circle with radius 5
double rectangleArea = shape.calculateArea(4.0, 6.0); // Area of rectangle with length 4 and width 6
std::cout << "Area of Circle: " << circleArea << std::endl;
std::cout << "Area of Rectangle: " << rectangleArea << std::endl;
return 0;
}
Notes
- In this example, the
calculateAreafunction is overloaded to handle different types of shape calculations. The method distinguishes between a circle (one parameter) and a rectangle (two parameters).
Example 2: Print Details
Context
This example illustrates how to print details of different data types using the same function name, enhancing user experience by providing flexibility.
#include <iostream>
#include <string>
class Printer {
public:
// Print integer
void print(int value) {
std::cout << "Integer: " << value << std::endl;
}
// Print double
void print(double value) {
std::cout << "Double: " << value << std::endl;
}
// Print string
void print(const std::string &value) {
std::cout << "String: " << value << std::endl;
}
};
int main() {
Printer printer;
printer.print(42);
printer.print(3.14);
printer.print("Hello, World!");
return 0;
}
Notes
- This example shows how the
printfunction is overloaded to handle different data types: integers, doubles, and strings. It demonstrates the ability to use the same function name for distinct functionalities, making the code more intuitive.
Example 3: Overloaded Constructors
Context
This example highlights how constructors can be overloaded to initialize an object in different ways, depending on the parameters provided.
#include <iostream>
#include <string>
class Book {
private:
std::string title;
std::string author;
int year;
public:
// Constructor for title and author only
Book(std::string t, std::string a) : title(t), author(a), year(0) {}
// Constructor for title, author and year
Book(std::string t, std::string a, int y) : title(t), author(a), year(y) {}
void display() {
std::cout << "Title: " << title << ", Author: " << author;
if (year != 0)
std::cout << ", Year: " << year;
std::cout << std::endl;
}
};
int main() {
Book book1("1984", "George Orwell");
Book book2("Brave New World", "Aldous Huxley", 1932);
book1.display();
book2.display();
return 0;
}
Notes
- In this example, the
Bookclass demonstrates constructor overloading. One constructor initializes the object with a title and author, while the other includes the publication year. This allows for flexible object creation depending on the available data.
Related Topics
Polymorphism in C++: Function Overloading
Dynamic Memory Allocation in C++: New and Delete
C# Variable Declaration and Initialization Examples
C++ Operator Overloading: Custom Operators Examples
C# Conditional Statements: Examples & Explanations
C# Exception Handling: 3 Practical Examples
Explore More C++ Code Snippets
Discover more examples and insights in this category.
View All C++ Code Snippets