ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Object Oriented Programming
    COMP2111
    Progress0 / 23 topics
    Topics
    1. Introduction to object oriented design2. History and advantages of object oriented design3. Introduction to object oriented programming concepts4. Classes and objects5. Data encapsulation6. Constructors and destructors7. Access modifiers8. Const vs non-const functions9. Static data members & functions10. Function overloading11. Operator overloading12. Identification of classes and their relationships13. Composition and aggregation14. Inheritance15. Multiple inheritance16. Polymorphism17. Abstract classes and interfaces18. Generic programming concepts19. Function & class templates20. Standard template library21. Object streams22. Data and object serialization using object streams23. Exception handling
    COMP2111›Function overloading
    Object Oriented ProgrammingTopic 10 of 23

    Function overloading

    4 minread
    749words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count749
      Code examples0
      DifficultyBeginner