Paper Code: CIC-211
Subject: Object-Oriented Programming Using C++
Unit01, Lecture 08: Topics Covered: Function over loading
Function overloading in C++ allows you to define multiple functions with the same name but
different parameter lists. The compiler differentiates these functions based on the number, types,
or order of the parameters. Function overloading provides flexibility and enhances code
readability.
#include <iostream>
using namespace std;
// Function to add two integers
int add(int a, int b) {
return a + b;
}
// Function to add two floating-point numbers
float add(float a, float b) {
return a + b;
}
// Function to add three integers
int add(int a, int b, int c) {
return a + b + c;
}
int main() {
int x = 10, y = 20, z = 30;
float p = 5.5, q = 2.3;
// Calls the function that adds two integers
cout << "Sum of x and y: " << add(x, y) << endl;
// Calls the function that adds two floats
cout << "Sum of p and q: " << add(p, q) << endl;
// Calls the function that adds three integers
cout << "Sum of x, y, and z: " << add(x, y, z) << endl;
return 0;
}
Explanation:
1. Function Signatures:
o int add(int a, int b) - Adds two integers.
o float add(float a, float b) - Adds two floating-point numbers.
o int add(int a, int b, int c) - Adds three integers.
2. Overloading:
o The functions add are overloaded based on the number and type of parameters.
3. Function Calls:
o The compiler chooses the correct function based on the arguments passed when
calling the add function.
Example -2
#include <iostream>
using namespace std;
// Function to calculate the area of a square
int area(int side) {
return side * side;
// Function to calculate the area of a rectangle
int area(int length, int breadth) {
return length * breadth;
// Function to calculate the area of a circle
double area(double radius) {
return 3.14159 * radius * radius;
}
int main() {
int side = 5;
int length = 10, breadth = 4;
double radius = 7.0;
// Calls the function that calculates the area of a square
cout << "Area of square: " << area(side) << endl;
// Calls the function that calculates the area of a rectangle
cout << "Area of rectangle: " << area(length, breadth) << endl;
// Calls the function that calculates the area of a circle
cout << "Area of circle: " << area(radius) << endl;
return 0;
}
Explanation:
1. Function Signatures:
o int area(int side) - Calculates the area of a square.
o int area(int length, int breadth) - Calculates the area of a rectangle.
o double area(double radius) - Calculates the area of a circle.
2. Overloading:
o The area function is overloaded for different shapes, with each shape's area
calculated using a different number and type of parameters.
3. Function Calls:
o The correct version of the area function is selected based on the shape and
corresponding parameters.
Example: Function Overloading in a Class
#include <iostream>
using namespace std;
class Shape {
public:
// Function to calculate the area of a square
int area(int side) {
return side * side;
// Function to calculate the area of a rectangle
int area(int length, int breadth) {
return length * breadth;
// Function to calculate the area of a circle
double area(double radius) {
return 3.14159 * radius * radius;
};
int main() {
Shape shape; // Creating an object of the Shape class
int side = 5;
int length = 10, breadth = 4;
double radius = 7.0;
// Calls the function that calculates the area of a square
cout << "Area of square: " << shape.area(side) << endl;
// Calls the function that calculates the area of a rectangle
cout << "Area of rectangle: " << shape.area(length, breadth) <<
endl;
// Calls the function that calculates the area of a circle
cout << "Area of circle: " << shape.area(radius) << endl;
return 0;
}
Explanation:
1. Class Shape:
o The class Shape contains three overloaded area functions.
o Each function has the same name, area, but different parameters to handle the
calculation for different shapes.
2. Overloaded Functions:
o int area(int side): Calculates the area of a square.
o int area(int length, int breadth) : Calculates the area of a rectangle.
o double area(double radius): Calculates the area of a circle.
3. Main Function:
o An object of the Shape class (shape) is created.
o The overloaded area functions are called through this object, and the appropriate
function is selected based on the parameters passed.
4. Output:
o The program will output the area of the square, rectangle, and circle based on the
corresponding function call:
References:-
Online
1. Tutorialspoint OOP Tutorial: Comprehensive tutorials on OOP concepts with examples. Link
2. GeeksforGeeks OOP Concepts: Articles and examples on OOP principles and applications.
Link
Book
1. "Object-Oriented Analysis and Design with Applications" by Grady Booch: Classic book
covering fundamental OOP concepts and real-world examples.
2. "Object-Oriented Programming in C++" by Robert Lafore: Detailed guide to OOP in C++,
explaining concepts with a practical example