Function overloading and function overriding are two important concepts in C++ that allow us to write more flexible and modular code. Although they share similarities in the name and the fact that they involve multiple functions with the same name, they are different concepts with different purposes and implementations.
Function Overloading:
Function overloading allows us to define multiple functions with the same name but different parameters. The compiler selects the appropriate function to call based on the number, order, and types of arguments passed to it. Function overloading is a form of compile-time polymorphism or static polymorphism because the function to be called is determined at compile time.
Here is an example of function overloading:
csharpCopy codeint sum(int a, int b) {
return a + b;
}
double sum(double a, double b) {
return a + b;
}
int main() {
int x = sum(3, 4);
double y = sum(3.5, 4.5);
return 0;
}
In this example, we define two sum()
functions, one for integers and one for doubles. When we call the sum()
function with integers, the first function is called, and when we call it with doubles, the second function is called.
Function Overriding:
Function overriding allows a derived class to provide its own implementation of a virtual function that is already defined in the base class. The virtual function in the base class is marked with the virtual
keyword, and the function in the derived class is marked with the override
keyword to indicate that it is intended to override the base class function. Function overriding is a form of run-time polymorphism or dynamic polymorphism because the function to be called is determined at run time.
Here is an example of function overriding:
arduinoCopy codeclass Shape {
public:
virtual double area() {
return 0.0;
}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) {
radius = r;
}
double area() override {
return 3.14159 * radius * radius;
}
};
class Rectangle : public Shape {
private:
double length;
double width;
public:
Rectangle(double l, double w) {
length = l;
width = w;
}
double area() override {
return length * width;
}
};
int main() {
Shape* s;
Circle c(5);
Rectangle r(4, 6);
s = &c;
cout << "Circle area: " << s->area() << endl;
s = &r;
cout << "Rectangle area: " << s->area() << endl;
return 0;
}
In this example, we define a base class Shape
with a virtual function area()
, and two derived classes Circle
and Rectangle
that provide their own implementation of the area()
function. When we create a Circle
or Rectangle
object and assign it to a Shape
pointer, the appropriate area()
function is called based on the actual type of the object.
Conclusion:
Function overloading and function overriding are both important concepts in C++ that allow us to write more flexible and modular code. Function overloading is used to define multiple functions with the same name but different parameters, while function overriding is used to provide a new implementation of a virtual function in a derived class. Understanding these concepts is essential for writing effective and maintainable object-oriented code.
We appreciate you reading our blog, and we hope to welcome you again soon for more interesting posts. We appreciate your participation in our community and look forward to hearing from you.