Software Development Exam  >  Software Development Notes  >  DSA in C++  >  Function Overloading in C++

Function Overloading in C++ | DSA in C++ - Software Development PDF Download

Introduction

In the world of programming, functions are an essential part of any language. They allow us to encapsulate a set of instructions and reuse them multiple times. Function overloading is a powerful feature in C++ that enables us to define multiple functions with the same name but different parameter lists. This article aims to provide a beginner-friendly explanation of function overloading in C++, along with examples and sample problems to reinforce the concepts.

What is Function Overloading?

Function overloading is a feature in C++ that allows multiple functions to have the same name but different parameter lists. With function overloading, you can define functions with the same name that perform similar tasks but operate on different data types or accept different numbers of arguments.

How does Function Overloading Work?

C++ determines which function to call during compile-time based on the number, types, and order of arguments passed to the function. When you call an overloaded function, the compiler selects the appropriate function by matching the arguments provided with the function signatures.

Benefits of Function Overloading

  • Code Reusability: Function overloading allows you to reuse function names for similar tasks, reducing the need for creating separate functions with different names.
  • Readability and Understandability: Overloaded functions make the code more readable and self-explanatory, as the function name conveys the intent of the operation.
  • Flexibility: Function overloading provides flexibility in handling different types of data and varying numbers of arguments, enhancing the versatility of your code.

Rules for Function Overloading

  • To successfully overload a function in C++, you need to follow these rules:
  • The functions must have the same name.
  • The functions must have different parameter lists, either in terms of the number of parameters or the data types of parameters.
  • The return type of the functions can be the same or different.

Examples of Function Overloading

Let's explore a couple of examples to illustrate how function overloading works.

Example 1: Adding Two Integers

#include <iostream>


// Function to add two integers

int add(int a, int 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 = 5, y = 10, z = 15;

    

    std::cout << "Sum of two numbers: " << add(x, y) << std::endl;

    std::cout << "Sum of three numbers: " << add(x, y, z) << std::endl;

    

    return 0;

}

Output:

Sum of two numbers: 15

Sum of three numbers: 30

Explanation:
In this example, we have defined two overloaded functions named 'add'. The first function 'add(int a, int b)' accepts two integers and returns their sum. The second function 'add(int a, int b, int c)' accepts three integers and returns their sum. The appropriate function is called based on the number of arguments provided.

Example 2: Finding the Maximum of Two Numbers

#include <iostream>


// Function to find the maximum of two integers

int findMax(int a, int b) {

    return (a > b) ? a : b;

}


// Function to find the maximum of two floats

float findMax(float a, float b) {

    return (a > b) ? a : b;

}


int main() {

    int x = 5, y = 10;

    float a = 3.14, b = 2.71;

    

    std::cout << "Maximum of two integers: " << findMax(x, y) << std::endl;

    std::cout << "Maximum of two floats: " << findMax(a, b) << std::endl;

    

    return 0;

}

Output:

Maximum of two integers: 10

Maximum of two floats: 3.14

Explanation:
In this example, we have defined two overloaded functions named 'findMax'. The first function 'findMax(int a, int b)' accepts two integers and returns the maximum of the two. The second function 'findMax(float a, float b)' accepts two floats and returns the maximum of the two. The appropriate function is called based on the data types of the arguments.

Sample Problems

Problem 1: Write a C++ program to calculate the area of a rectangle and a circle using function overloading. The rectangle function should accept the length and width as parameters, while the circle function should accept the radius.

#include <iostream>


// Function to calculate the area of a rectangle

int calculateArea(int length, int width) {

    return length * width;

}


// Function to calculate the area of a circle

float calculateArea(float radius) {

    return 3.14 * radius * radius;

}


int main() {

    int length = 5, width = 3;

    float radius = 2.5;

    

    std::cout << "Area of rectangle: " << calculateArea(length, width) << std::endl;

    std::cout << "Area of circle: " << calculateArea(radius) << std::endl;

    

    return 0;

}

Output:

Area of rectangle: 15

Area of circle: 19.625

Problem 2: Implement a C++ program to concatenate two strings using function overloading. The first function should concatenate two character arrays, while the second function should concatenate two 'std::string' objects.

#include <iostream>

#include <string>


// Function to concatenate two character arrays

char* concatenate(char* str1, char* str2) {

    int len1 = strlen(str1);

    int len2 = strlen(str2);

    

    char* result = new char[len1 + len2 + 1];

    

    strcpy(result, str1);

    strcat(result, str2);

    

    return result;

}


// Function to concatenate two std::string objects

std::string concatenate(const std::string& str1, const std::string& str2) {

    return str1 + str2;

}


int main() {

    char str1[] = "Hello, ";

    char str2[] = "world!";

    

    std::string s1 = "Hello, ";

    std::string s2 = "world!";

    

    char* result1 = concatenate(str1, str2);

    std::string result2 = concatenate(s1, s2);

    

    std::cout << "Concatenated string (char*): " << result1 << std::endl;

    std::cout << "Concatenated string (std::string): " << result2 << std::endl;

    

    delete[] result1;

    

    return 0;

}

Output:

  • Concatenated string (char*): Hello, world!
  • Concatenated string (std::string): Hello, world!

Conclusion

Function overloading is a powerful feature in C++ that allows you to define multiple functions with the same name but different parameter lists. It provides code reusability, improves readability, and offers flexibility in handling different data types and argument variations. By following the rules of function overloading, you can make your code more concise and efficient. Understanding this concept is crucial for mastering data structures and algorithms in C++, as it enables you to write modular and versatile code.

The document Function Overloading in C++ | DSA in C++ - Software Development is a part of the Software Development Course DSA in C++.
All you need of Software Development at this link: Software Development
153 videos|115 docs|24 tests

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Function Overloading in C++ | DSA in C++ - Software Development

,

Objective type Questions

,

ppt

,

MCQs

,

Important questions

,

Viva Questions

,

Exam

,

study material

,

pdf

,

Function Overloading in C++ | DSA in C++ - Software Development

,

Previous Year Questions with Solutions

,

Free

,

past year papers

,

Extra Questions

,

Function Overloading in C++ | DSA in C++ - Software Development

,

Summary

,

practice quizzes

,

video lectures

,

Sample Paper

,

Semester Notes

,

shortcuts and tricks

,

mock tests for examination

;