Function Overloading, Friend class and Function and Operator Overloading in C++

1. Operator Overloading in C++: This involves redefining the behavior of operators (like +, -, *, /, etc.) for user-defined data types (classes or structures). https://images.app.goo.gl/1ce85vWQajVWTXcp9

2. Function Overloading in C++: This allows multiple functions with the same name to be defined, but with different parameter lists.

3. Friend Class and Function in C++: A friend class or function is a class or function that has access to the private and protected members of another class.

Operator overloading in C++ allows you to define the behavior of operators (like +, -, *, etc.) when they are applied to objects of user-defined classes. By overloading operators, you can extend their functionality to work with complex data types and make your code more intuitive.
#include <iostream>
using namespace std;

class Complex {
private:
    float real;
    float imag;
public:
    Complex() : real(0), imag(0) {}

    // Overloading '+' operator
    Complex operator + (const Complex &obj) {
        Complex temp;
        temp.real = real + obj.real;
        temp.imag = imag + obj.imag;
        return temp;
    }

    void setValues(float r, float i) {
        real = r;
        imag = i;
    }

    void display() {
        cout << real << " + " << imag << "i" << endl;
    }
};

int main() {
    Complex c1, c2, result;
    c1.setValues(3.4, 5.6);
    c2.setValues(1.2, 2.3);

    result = c1 + c2;  // Calls the overloaded + operator
    result.display();

    return 0;
}

Explanation:

The + operator is overloaded to add two Complex objects by adding their respective real and imaginary parts.

The operator+ function returns a new Complex object with the sum of the real and imaginary components.

Function overloading in C++ allows multiple functions with the same name but different parameter lists. The compiler distinguishes them based on the number and types of parameters passed to the function.
#include <iostream>
using namespace std;

class Calculator {
public:
    // Overloaded 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 floating-point numbers
    float add(float a, float b) {
        return a + b;
    }
};

int main() {
    Calculator calc;

    // Calling the overloaded add function
    cout << "Sum of two integers: " << calc.add(3, 5) << endl;
    cout << "Sum of three integers: " << calc.add(1, 2, 3) << endl;
    cout << "Sum of two floats: " << calc.add(2.5f, 3.1f) << endl;

    return 0;
}

Explanation:

The add function is overloaded with different parameter types and counts (two integers, three integers, and two floating-point numbers).

The correct version of add is invoked depending on the arguments passed during the function call.

  1. In C++, a friend class or friend function is one that can access the private and protected members of another class. By declaring a function or another class as a friend, you provide it with special access rights.
#include <iostream>
using namespace std;

// Forward declaration of class B
class B;

class A {
private:
    int a;
public:
    A() : a(10) {}

    // Declare class B as a friend
    friend class B;
};

class B {
public:
    void display(A &obj) {
        // Accessing private member 'a' of class A
        cout << "Value of a: " << obj.a << endl;
    }
};

int main() {
    A objA;
    B objB;

    // Class B is able to access private member 'a' of class A
    objB.display(objA);

    return 0;
}

Explanation:

In this example, class B is declared as a friend of class A, meaning that B can access the private members of A.

The display function in class B is able to access the private variable a of class A and print its value.

Conclusion

  • These C++ features—operator overloading, function overloading, and friend classes/functions—are powerful tools that enhance the flexibility and readability of your code. Operator and function overloading allow you to define custom behavior for operators and functions, while friend functions and classes enable special access to the private and protected members of other classes. Understanding these concepts is crucial for mastering object-oriented programming in C++.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top