[go: up one dir, main page]

0% found this document useful (0 votes)
8 views10 pages

Function Overloading

This lab manual focuses on function overloading in C++, explaining its concept, objectives, and providing examples and tasks for practice. Function overloading allows multiple functions with the same name but different parameter types or counts, enhancing code readability. The manual includes various coding examples and tasks for students to implement overloaded functions for different scenarios.

Uploaded by

ziomlubda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Function Overloading

This lab manual focuses on function overloading in C++, explaining its concept, objectives, and providing examples and tasks for practice. Function overloading allows multiple functions with the same name but different parameter types or counts, enhancing code readability. The manual includes various coding examples and tasks for students to implement overloaded functions for different scenarios.

Uploaded by

ziomlubda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Lab Manual: Object Oriented Programming

Lab 09: Function Overloading

1. Introduction:
you have already studied that a sub/derived class is able to access all non-private data
members and member functions of the base/parent class. Moreover, besides inheriting
attributes and behaviors from base class(es), the derived class also contains its own data
members and member functions.

Function overloading is the availability of various functions within a class that differ
from each other in function signature i.e. various functions share same name with
different parameter types or number of parameters. Inheritance is not necessarily
required to overload functions within a program.

Function Overloading is a feature in C++ that allows you to define multiple functions with
the same name but with different parameter lists. The correct function is chosen by the
compiler based on the number and type of arguments passed.
 Function overloading allows the creation of multiple functions with the same name but
with different signatures.
 It helps in increasing the readability of the code and reduces the need for multiple
function names that perform similar tasks.
Key Points:
 Function overloading is resolved at compile time.
 The return type of functions cannot be used to overload functions.

2. Objective of the Experiment:


After completing this lab, students should be able to:
 Understand the concept of function overloading.
 Create and implement overloaded functions.
 Distinguish between overloaded functions based on parameters and argument type

3. Concept Map:
 Function Overloading:
Function overloading allows the same function name to be used for different types or numbers
of parameters. The compiler differentiates between the functions by examining their argument
list.
Rules for Function Overloading:
1. The number of parameters can differ.
2. The type of parameters can differ.
3. Function overloading cannot be done solely based on the return type.

Department of Computer Science, UMT, Lahore. 1 Iqra Khalil


Lab Manual: Object Oriented Programming

Example 1: Overloading Based on Number of Parameters


#include <iostream>
using namespace std;

void print(int i) {
cout << "Here is int: " << i << endl;
}

void print(double f) {
cout << "Here is float: " << f << endl;
}

void print(int i, double f) {


cout << "Here is int and float: " << i << " and " << f << endl;
}

int main() {
print(10); // Calls print(int)
print(12.5); // Calls print(double)
print(5, 2.3); // Calls print(int, double)
return 0;
}

Example 2: Overloading Based on Parameter Type


#include <iostream>
using namespace std;

void display(int num) {


cout << "Integer: " << num << endl;
}

void display(double num) {


cout << "Double: " << num << endl;
}

int main() {
display(10); // Calls display(int)
display(10.5); // Calls display(double)
return 0;
}

Department of Computer Science, UMT, Lahore. 2 Iqra Khalil


Lab Manual: Object Oriented Programming

Example 3: Overloading Based on Number and Type of Parameters

#include <iostream>
using namespace std;

void calculateArea(int side) {


cout << "Area of square: " << side * side << endl;
}

void calculateArea(int length, int breadth) {


cout << "Area of rectangle: " << length * breadth << endl;
}

void calculateArea(double radius) {


cout << "Area of circle: " << 3.14 * radius * radius << endl;
}

int main() {
calculateArea(5); // Calls square area function
calculateArea(4, 5); // Calls rectangle area function
calculateArea(3.5); // Calls circle area function
return 0;
}
Example 4: Overloading Functions for Printing Values (Using a Class)
#include <iostream>
using namespace std;

class Printer {
public:
void print(int i) {
cout << "Integer: " << i << endl;
}

void print(double d) {
cout << "Double: " << d << endl;
}

void print(string s) {
cout << "String: " << s << endl;
}
};

int main() {
Printer printer;

printer.print(10); // Calls print(int)

Department of Computer Science, UMT, Lahore. 3 Iqra Khalil


Lab Manual: Object Oriented Programming
printer.print(3.14); // Calls print(double)
printer.print("Hello!"); // Calls print(string)

return 0;
}
In this example, the class Printer contains three overloaded print() functions that take different
types of parameters: an integer, a double, and a string. The correct function is called depending
on the argument passed.

Example 5: Overloading Functions for Calculating Areas (Using a Class)


#include <iostream>
using namespace std;

class Shape {
public:
// Overloaded function to calculate area of a square
void calculateArea(int side) {
cout << "Area of Square: " << side * side << endl;
}

// Overloaded function to calculate area of a rectangle


void calculateArea(int length, int width) {
cout << "Area of Rectangle: " << length * width << endl;
}

// Overloaded function to calculate area of a circle


void calculateArea(double radius) {
cout << "Area of Circle: " << 3.14 * radius * radius << endl;
}
};

int main() {
Shape shape;

shape.calculateArea(4); // Square
shape.calculateArea(5, 10); // Rectangle
shape.calculateArea(3.5); // Circle

return 0;
}
In this example, the Shape class contains three overloaded calculateArea() functions. These
functions calculate the area of different shapes based on the parameters provided (square,
rectangle, and circle).

Department of Computer Science, UMT, Lahore. 4 Iqra Khalil


Lab Manual: Object Oriented Programming

4. Walkthrough Tasks:
Task 01: Overload a Function for Printing Values
Write a function named print() that can:
 Print an integer.
 Print a floating-point number.
 Print a string.
Code Example:
#include <iostream>
using namespace std;

void print(int i) {
cout << "Integer: " << i << endl;
}

void print(double d) {
cout << "Double: " << d << endl;
}

void print(string s) {
cout << "String: " << s << endl;
}

int main() {
print(5); // Integer version
print(3.1415); // Double version
print("Hello!"); // String version
return 0;
}

Task 02: Overload Functions for Area Calculation


Overload a function area() to:
 Calculate the area of a circle.
 Calculate the area of a rectangle.
 Calculate the area of a triangle.
Code Example:
#include <iostream>
using namespace std;

void area(int radius) {


cout << "Area of Circle: " << 3.14 * radius * radius << endl;
}

void area(int length, int breadth) {

Department of Computer Science, UMT, Lahore. 5 Iqra Khalil


Lab Manual: Object Oriented Programming
cout << "Area of Rectangle: " << length * breadth << endl;
}

void area(double base, double height) {


cout << "Area of Triangle: " << 0.5 * base * height << endl;
}

int main() {
area(5); // Circle
area(5, 10); // Rectangle
area(4.0, 6.0); // Triangle
return 0;
}

Task 03: Overload Functions for Addition


Write a function add() that:
 Adds two integers.
 Adds two floating-point numbers.
 Adds three integers.
Code Example:
#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

double add(double a, double b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

int main() {
cout << "Sum of two integers: " << add(5, 7) << endl;
cout << "Sum of two doubles: " << add(5.5, 6.3) << endl;
cout << "Sum of three integers: " << add(1, 2, 3) << endl;
return 0;
}

Department of Computer Science, UMT, Lahore. 6 Iqra Khalil


Lab Manual: Object Oriented Programming

Task 04: Overloading Functions for Different Data Types (Using a Class)
Create a class Calculator with the following overloaded functions:
o add(int, int) – Adds two integers.
o add(double, double) – Adds two floating-point numbers.
o add(string, string) – Concatenates two strings.
Code Example:
#include <iostream>
#include <string>
using namespace std;

class Calculator {
public:
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}

string add(string a, string b) {


return a + b;
}
};

int main() {
Calculator calc;

cout << "Sum of integers: " << calc.add(10, 20) << endl; // Calls add(int, int)
cout << "Sum of doubles: " << calc.add(10.5, 20.3) << endl; // Calls add(double, double)
cout << "Concatenated strings: " << calc.add("Hello", "World") << endl; // Calls add(string,
string)

return 0;
}
Here, we’ve created a class Calculator that contains three add() methods. Each method is
overloaded to handle integers, doubles, and strings.

Department of Computer Science, UMT, Lahore. 7 Iqra Khalil


Lab Manual: Object Oriented Programming

Task 05: Overloading Functions for Area Calculations (Using a Class)


Write a class Geometry that contains overloaded methods area() to:
 Calculate the area of a square, given its side.
 Calculate the area of a rectangle, given its length and width.
 Calculate the area of a triangle, given its base and height.
Code Example:
#include <iostream>
using namespace std;

class Geometry {
public:
// Area of Square
void area(int side) {
cout << "Area of Square: " << side * side << endl;
}

// Area of Rectangle
void area(int length, int width) {
cout << "Area of Rectangle: " << length * width << endl;
}

// Area of Triangle
void area(double base, double height) {
cout << "Area of Triangle: " << 0.5 * base * height << endl;
}
};

int main() {
Geometry geometry;

geometry.area(5); // Calls area(int) - Square


geometry.area(4, 6); // Calls area(int, int) - Rectangle
geometry.area(4.5, 7.0); // Calls area(double, double) - Triangle

return 0;
}
In this task, you’ve created a Geometry class with three overloaded area() functions, each
calculating the area of different shapes.

Department of Computer Science, UMT, Lahore. 8 Iqra Khalil


Lab Manual: Object Oriented Programming

Task 06:
#include <iostream>
#include <cmath>
using namespace std;
class OverloadingConcept{
//no datamember public:
void Find_Area(int radius){
cout<<"\nArea of the Circle:::"<<3.14*radius*radius;
}
void Find_Area(int base,int height){
cout<<"\nArea of the triangle:::"<<(base*height)/2;
}
void Find_Area(double length,float width){
cout<<"\nArea of the Rectangle:::"<<length*width;
}
};
int main()
{
OverloadingConcept circle,triangle,rectangle;
circle.Find_Area(76);
triangle.Find_Area(45,56);
rectangle.Find_Area(45.3,67.6);
return 0;
}

5. Practice Tasks:
Task 1:
Write a program that overloads a function subtract() to:
 Subtract two integers.
 Subtract two floating-point numbers.
 Subtract three integers.
Task 2:
Write a program that overloads a function concatenate() to:
 Concatenate two strings.
 Concatenate three strings.
 Concatenate a string and an integer (the integer should be converted to a string).
Task 3:
Write a program that defines a class Multiplier with overloaded multiply() methods to:
 Multiply two integers.
 Multiply two floating-point numbers.
 Multiply three integers.
Task 4:
Write a program that defines a class Converter with overloaded convert() methods to:
 Convert an integer to a string.
 Convert a double to a string.

Department of Computer Science, UMT, Lahore. 9 Iqra Khalil


Lab Manual: Object Oriented Programming
 Convert a string to uppercase.
Task 5:
Write a program that defines a class Comparator with overloaded compare() methods to:
 Compare two integers and return the larger one.
 Compare two floating-point numbers and return the larger one.
 Compare two strings lexicographically and return the one that comes first in dictionary
order.

Department of Computer Science, UMT, Lahore. 10 Iqra Khalil

You might also like