Lecture08 Unit01 Function Over Loading
Lecture08 Unit01 Function Over Loading
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20, z = 30;
float p = 5.5, q = 2.3;
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>
int main() {
int side = 5;
cout << "Area of rectangle: " << area(length, breadth) << 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.
#include <iostream>
class Shape {
public:
};
int main() {
int side = 5;
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