Overloading in C++ • Overloading allows multiple functions or operators with the same name but different parameters. • Types of Overloading in C++: • 1. Function Overloading • 2. Operator Overloading
Function Overloading • Function Overloading: Multiple functions with the same name but different parameter types or counts. • Example: • int add(int a, int b); • float add(float a, float b);
Operator Overloading • Operator Overloading: Redefining the behavior of operators for user-defined types. • Example: • calculation = add(divide(a, b), multiply(a, b)); • can be replaced with: • calculation = (a/b)+(a*b);
Operators That Cannot Be Overloaded • 1. Scope Resolution Operator (::) • 2. Size Operator (sizeof) • 3. Member Selector (.) • 4. Member Pointer Selector (*) • 5. Ternary Operator (?:)
Syntax of Operator Overloading • returntype classname::operator op(argumentlist) • { • // function body • }
Operator Function Types • Operator function must be either: • 1. Non-static Member Function • 2. Friend Function • Friend functions can access private and protected members of the class.
Member vs Non-Member Functions • Member Function: • - Can be defined inside or outside the class. • - Must be qualified with class name if defined outside. • Non-Member Function: • - Always defined outside the class.
Friend Functions • Friend function is defined outside the class but can access private/protected members. • Declared in class but not a member function.
Operator vs Normal Functions • Operator Functions are similar to normal functions. • Difference: Name starts with 'operator' followed by the operator symbol. • Called when the corresponding operator is used.
Approaches to Operator Overloading • 1. Overloading Unary Operator • 2. Overloading Binary Operator
Overloading Unary Operator • Unary operators operate on one operand. • Example: Overloading '-' to change sign of an object. • Similar to how it works with int or float.
Overloading Binary Operator • Binary operators operate on two operands. • Example: Overloading '+' to add two objects.
Rules for Operator Overloading • 1. Only existing operators can be overloaded. • 2. At least one operand must be a user-defined type. • 3. Friend functions are not used for all operators. • 4. Unary operator overloading: • - Member function: no explicit arguments • - Friend function: one argument • 5. Binary operator overloading: • - Member function: one explicit argument • - Friend function: two explicit arguments

Operator overloading in C++ allows developers to redefine the behavior of operators

  • 1.
    Overloading in C++ •Overloading allows multiple functions or operators with the same name but different parameters. • Types of Overloading in C++: • 1. Function Overloading • 2. Operator Overloading
  • 2.
    Function Overloading • FunctionOverloading: Multiple functions with the same name but different parameter types or counts. • Example: • int add(int a, int b); • float add(float a, float b);
  • 3.
    Operator Overloading • OperatorOverloading: Redefining the behavior of operators for user-defined types. • Example: • calculation = add(divide(a, b), multiply(a, b)); • can be replaced with: • calculation = (a/b)+(a*b);
  • 4.
    Operators That CannotBe Overloaded • 1. Scope Resolution Operator (::) • 2. Size Operator (sizeof) • 3. Member Selector (.) • 4. Member Pointer Selector (*) • 5. Ternary Operator (?:)
  • 5.
    Syntax of OperatorOverloading • returntype classname::operator op(argumentlist) • { • // function body • }
  • 6.
    Operator Function Types •Operator function must be either: • 1. Non-static Member Function • 2. Friend Function • Friend functions can access private and protected members of the class.
  • 7.
    Member vs Non-Member Functions •Member Function: • - Can be defined inside or outside the class. • - Must be qualified with class name if defined outside. • Non-Member Function: • - Always defined outside the class.
  • 8.
    Friend Functions • Friendfunction is defined outside the class but can access private/protected members. • Declared in class but not a member function.
  • 9.
    Operator vs NormalFunctions • Operator Functions are similar to normal functions. • Difference: Name starts with 'operator' followed by the operator symbol. • Called when the corresponding operator is used.
  • 10.
    Approaches to Operator Overloading •1. Overloading Unary Operator • 2. Overloading Binary Operator
  • 11.
    Overloading Unary Operator •Unary operators operate on one operand. • Example: Overloading '-' to change sign of an object. • Similar to how it works with int or float.
  • 12.
    Overloading Binary Operator •Binary operators operate on two operands. • Example: Overloading '+' to add two objects.
  • 13.
    Rules for OperatorOverloading • 1. Only existing operators can be overloaded. • 2. At least one operand must be a user-defined type. • 3. Friend functions are not used for all operators. • 4. Unary operator overloading: • - Member function: no explicit arguments • - Friend function: one argument • 5. Binary operator overloading: • - Member function: one explicit argument • - Friend function: two explicit arguments