ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2111Function Overloading
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2111

    Object Oriented Programming

    Progress0 / 23 done
    Topics23
    1Introduction to object oriented design2History and advantages of object oriented design3Introduction to object oriented programming concepts4Classes and objects5Data encapsulation6Constructors and destructors7Access modifiers8Const vs non-const functions9Static data members & functions10Function overloading11Operator overloading12Identification of classes and their relationships13Composition and aggregation14Inheritance15Multiple inheritance16Polymorphism17Abstract classes and interfaces18Generic programming concepts19Function & class templates20Standard template library21Object streams22Data and object serialization using object streams23Exception handling
    4 min read
    Topic 10 of 23
    Topic Study Note Illustration
    10

    Function overloading

    Function Overloading in C++

    Function Overloading in C++

    Function overloading is a feature in C++ that allows you to define multiple functions with the same name but different parameters. These functions can perform similar tasks but with different types or numbers of inputs. The compiler distinguishes between overloaded functions based on the number or type of parameters.

    Key Points:

    • Functions must have the same name.
    • The parameter list (type, number, or order of parameters) must be different.
    • Return type alone is not enough to distinguish overloaded functions.
    • Overloading allows you to use a single function name to perform different tasks depending on the arguments passed.

    Why Use Function Overloading?

    1. Cleaner Code: You don’t need to create multiple function names for similar actions. For example, you can have a single function name like add() that works with integers, floats, etc.
    2. Improves Readability: It makes the code easier to understand since the function name remains consistent across different types of operations.

    Example of Function Overloading:

    #include <iostream>
    using namespace std;
    
    // Function to add two integers
    int add(int a, int b) {
        return a + b;
    }
    
    // Overloaded function to add three integers
    int add(int a, int b, int c) {
        return a + b + c;
    }
    
    // Overloaded function to add two floats
    float add(float a, float b) {
        return a + b;
    }
    
    int main() {
        cout << "Sum of two integers: " << add(5, 10) << endl;     // Calls int add(int, int)
        cout << "Sum of three integers: " << add(5, 10, 15) << endl; // Calls int add(int, int, int)
        cout << "Sum of two floats: " << add(5.5f, 10.5f) << endl;  // Calls float add(float, float)
    
        return 0;
    }
    

    Explanation of Example:

    • We have three add() functions in this example:
      • One that adds two integers (int add(int, int)).
      • One that adds three integers (int add(int, int, int)).
      • One that adds two floating-point numbers (float add(float, float)).

    Each function has the same name add, but they perform slightly different tasks based on the arguments passed.


    Important Rules for Function Overloading:

    1. Different number of parameters:

      • Functions can be overloaded by changing the number of parameters.
      void print(int a) { cout << "Integer: " << a << endl; }
      void print(int a, int b) { cout << "Two Integers: " << a << " " << b << endl; }
      
    2. Different types of parameters:

      • Functions can be overloaded by changing the type of parameters.
      void print(int a) { cout << "Integer: " << a << endl; }
      void print(float a) { cout << "Float: " << a << endl; }
      
    3. Different order of parameters:

      • You can overload functions by changing the order of parameters.
      void print(int a, float b) { cout << "Int and Float: " << a << ", " << b << endl; }
      void print(float a, int b) { cout << "Float and Int: " << a << ", " << b << endl; }
      

    Overloading Rules to Keep in Mind:

    1. Return type cannot be used to differentiate overloaded functions:

      • You cannot overload functions based on the return type alone.
      int add(int a, int b);        // Valid
      float add(int a, int b);      // Invalid — only the return type is different
      
    2. Parameter types, number, or order must differ:

      • You cannot have two functions that differ only by const, volatile, or function pointer types.
    3. Overloading and Default Arguments:

      • If a function has default arguments, care should be taken as it can affect overloading resolution. Default arguments are considered in function overloading, so you may need to provide the exact number of parameters while calling.

    Benefits of Function Overloading:

    1. Code Reusability: You don’t have to create separate function names for the same operation with different input types or numbers.
    2. Flexibility: You can extend functionality without modifying existing code.
    3. Cleaner Interface: Having one function name for similar operations makes the code cleaner and more understandable.

    Summary:

    Function overloading allows you to define multiple functions with the same name but different parameter lists (number or type of parameters). This is useful for performing similar tasks on different types or numbers of data without having to create different function names for each case.

    ← Previous topic 9
    Static data members & functions
    Next topic 11 →
    Operator overloading

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    749words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Object Oriented Programming