Operator Overloading Example in C++


Description:

Operator overloading is a compile-time polymorphism in which the operator is overloaded to provide the special meaning to the user-defined data type. Operator overloading is used to overload or redefines most of the operators available in C++. It is used to perform the operation on the user-defined data type.

Example Program:

Here is the simple program using operator overloading in C++.

Source Code

#include<iostream>
using namespace std;

class Complex
{
public :
    int real;
    int img;


    Complex operator +(Complex c)
    {
        Complex temp;
        temp.real=real+c.real;
        temp.img=img+c.img;
        return temp;
    }

};

int main()
{
    Complex c1,c2,c3;
    c1.real=5;
    c1.img=3;
    c2.real=10;
    c2.img=5;
    c3=c1+c2;
    cout<<"\nReal : "<<c3.real;
    cout<<"\nImg : "<<c3.img;
    return 0;
}
To download raw file Click Here

Output

Real : 15
Img : 8

Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes