× back Operator Overloading Can we overload all operator? Operators that can be overloaded
Next Topic → ← Previous Topic

Operator Overloading

            
class A
{
    statements;
};
int main()
{
    A a1, a2, a3;
    a3 = a1 + a2;
    return 0;
}
            
        

Example ↓

            
#include <iostream>
using namespace std;
class Marks
{
    int intmark;
    int extmark;

public:
    Marks()
    {
        intmark = 0;
        extmark = 0;
    }
    Marks(int im, int em)
    {
        intmark = im;
        extmark = em;
    }
    void display()
    {
        cout << intmark << endl
             << extmark << endl;
    }
    Marks operator+(Marks m)
    {
        Marks temp;
        temp.intmark = intmark + m.intmark;
        temp.extmark = extmark + m.extmark;
        return temp;
    }
};
int main()
{
    Marks m1(10, 20), m2(30, 40);
    Marks m3 = m1 + m2;
    m3.display();
    return 0;
}
            
        

Can we overload all operator?

Operators that can be overloaded

We can overload

                
Operators that can be overloaded       Example
   Binary Arithmetic                 +, -, *, /, %
   Unary Arithmetic                  +, -, ++, --
   Assignment                        =, +=, *=, /=, -=, %=
   Bitwise                           &, |, <<, >>, ~, ^
   De-referencing                    (->)
   Dynamic memory allocation,         New, delete
   De-allocation    
   Subscript                          []
   Function call                      ()
   Logical                            &, |, !
   Relational                         <, >, = =, <=, >=
                
            

Programs

                   
#include <iostream>
using namespace std;
class Myclass
{
private:
    int value;

public:
    Myclass() {} // default constructor which do nothing, but it is here for the objects which we don't initialize at the time of creation, we initialize them later.
    Myclass(int val)
    {
        value = val;
    }
    int getValue()
    {
        return value;
    }
    friend Myclass operator+(Myclass, Myclass); // friend function
};

Myclass operator+(Myclass obj1, Myclass obj2) /// friend function definition
{
    Myclass obj3;
    obj3.value = obj1.value + obj2.value;
    return obj3;
}

int main()
{
    Myclass obj1(20), obj2(20), obj3;
    obj3 = obj1 + obj2;
    cout << "The value of obj3 is : " << obj3.getValue();
    return 0;
}
                   
               
                        
#include <iostream>
using namespace std;
class Myclass
{
private:
    int value;

public:
    Myclass() {}
    Myclass(int val)
    {
        value = val;
    }
    int getValue()
    {
        return value;
    }
    friend Myclass operator-(Myclass, Myclass); // friend function
};

Myclass operator-(Myclass obj1, Myclass obj2) /// friend function definition
{
    Myclass obj3;
    obj3.value = obj1.value - obj2.value;
    return obj3;
}

int main()
{
    Myclass obj1(20), obj2(20), obj3;
    obj3 = obj1 - obj2;
    cout << "The value of obj3 is : " << obj3.getValue();
    return 0;
}
                        
                    
                        
#include <iostream>
using namespace std;

class Myclass
{
private:
    int value;

public:
    Myclass() {}

    Myclass(int val)
    {
        value = val;
    }

    int getValue()
    {
        return value;
    }

    // Friend function for overloading the * operator
    friend Myclass operator*(Myclass, Myclass);

    // Friend function for overloading the % operator
    friend Myclass operator%(Myclass, Myclass);

    // Friend function for overloading the / operator
    friend Myclass operator/(Myclass, Myclass);
};

Myclass operator*(Myclass obj1, Myclass obj2)
{
    int product = obj1.value * obj2.value;
    return Myclass(product);
}

Myclass operator%(Myclass obj1, Myclass obj2)
{
    int remainder = obj1.value % obj2.value;
    return Myclass(remainder);
}

Myclass operator/(Myclass obj1, Myclass obj2)
{
    int quotient = obj1.value / obj2.value;
    return Myclass(quotient);
}

int main()
{
    Myclass obj1(30);
    Myclass obj2(10);

    Myclass obj3 = obj1 * obj2;
    cout << "Product: " << obj3.getValue() << endl;

    Myclass obj4 = obj1 % obj2;
    cout << "Remainder: " << obj4.getValue() << endl;

    Myclass obj5 = obj1 / obj2;
    cout << "Quotient: " << obj5.getValue() << endl;

    return 0;
}
                        
                    

Overloading unary operator ↓

                   
#include <iostream>
using namespace std;

class Myclass
{
private:
    int value;

public:
    Myclass() {}

    Myclass(int val)
    {
        value = val;
    }

    int getValue() const
    {
        return value;
    }
    // Overloading the unary minus (-) operator
    friend Myclass operator-(Myclass);

    // Overloading the unary plus (+) operator
    friend Myclass operator+(Myclass);
};
Myclass operator-(Myclass x)
{
    Myclass y;
    y.value = -(x.value);
    return y;
}

Myclass operator+(Myclass x)
{
    return x;
}
int main()
{
    Myclass obj1(10);

    Myclass obj2 = -obj1; // Using the unary minus operator
    cout << "Unary Minus: " << obj2.getValue() << endl;

    Myclass obj3 = +obj1; // Using the unary plus operator
    cout << "Unary Plus: " << obj3.getValue() << endl;

    return 0;
}
                   
               
                
#include <iostream>
using namespace std;

class Myclass {
private:
 int value;

public:
 Myclass() {}

 Myclass(int val) {
     value = val;
 }

 int getValue() const {
     return value;
 }

 // Declare the friend functions
 friend Myclass& operator++(Myclass& obj); // prefix
 friend Myclass operator++(Myclass& obj, int); // postfix, for postfix we to have 'int' just to differentiate between pre and post
};

// Overloading prefix increment operator (++obj)
Myclass& operator++(Myclass& obj) {
 obj.value++;
 return obj;
}

// Overloading postfix increment operator (obj++)
Myclass operator++(Myclass& obj, int) {
 Myclass temp(obj); // previous value will be return and actual object value will be incremented
 // we just used copy constructor to create a copy of object passed in argument
 obj.value++;
 return temp;
}

int main() {
 Myclass obj(5);

 cout << "Initial Value: " << obj.getValue() << endl;

 // Prefix increment
 ++obj;
 cout << "After Prefix Increment: " << obj.getValue() << endl;

 // Postfix increment
 obj++;
 cout << "After Postfix Increment: " << obj.getValue() << endl;

 return 0;
}
                
            
                    
 #include <iostream>
 using namespace std;
 
 class Myclass {
 private:
     int value;
 
 public:
     Myclass() {}
 
     Myclass(int val) {
         value = val;
     }
 
     int getValue() const {
         return value;
     }
 
     // Declare the friend functions
     friend Myclass& operator--(Myclass& obj); // prefix
     friend Myclass operator--(Myclass& obj, int); // postfix
 };
 
 // Overloading prefix decrement operator (--obj)
 Myclass& operator--(Myclass& obj) {
     obj.value--;
     return obj;
 }
 
 // Overloading postfix decrement operator (obj--)
 Myclass operator--(Myclass& obj, int) {
     Myclass temp(obj); // previous value will be return and actual object value will be decremented
     obj.value--;
     return temp;
 }
 
 int main() {
     Myclass obj(5);
 
     cout << "Initial Value: " << obj.getValue() << endl;
 
     // Prefix decrement
     --obj;
     cout << "After Prefix Increment: " << obj.getValue() << endl;
 
     // Postfix decrement
     obj--;
     cout << "After Postfix Increment: " << obj.getValue() << endl;
 
     return 0;
 }
                    
                

Reference ↓