C++ 31: Binary Operator Overloading

 Binary Operator: The operator which need at least two operands to operate are called as binary operators.

+,-,*,<,>,==,+=,etc.

To overload binary operator the class must contain a special operator function as follows

Synatx:

        return_type operator op(data_type arg)

        {        

                    ........

                    ........

        }

Note:

1. This binary operator function accepts only one arguments.

2. While using binary operator the frist operand must be object. 

For example:

                c = a + b;

Here Frist Operand i.e. a must be a object.

Then Second Operand i.e. b can be anything.

3. This operator function will be automically invoke for the frist operand(i.e. frist object) and the second operand will be transferred as argument to the operator function.

Example:

Overloading > Operator

Design a class Distance having datamembers Feet and Inches. 
Define a member function showdata() whose objects can be created by passing data.
Overload > (greater than ) operator which will
compare data of two distance objecta and object will return true(1) or false(2).
#include <iostream>
using namespace std;

class Distance
{
private:
    int FIdata;

public:
    Distance(int f = 0int i = 0)
    {
        F = f;
        I = i;
        data = f * 12 + i;
    }
    void showdata()
    {
        cout << "Distance is : " << F << " Feet and " << I << " inches" << endl;
    }
    int operator>(Distance b)
    {
        if (data > b.data)
            return 1;
        else
            return 0;
    }
};
int main()
{
    Distance a(26), b(89);
    a.showdata();
    b.showdata();

    if (a > b)
    {
        cout << "A is Greater!";
    }
    else
    {
        cout << "B is greater";
    }
    return 0;
}

Output:

Distance is : 2 Feet and 6 inches
Distance is : 8 Feet and 9 inches
B is greater


Design a class Distance having data members Feet and Inches. Define 
data member function define member function showdata(). The objects 
of the class can be created using passing the data. Overload the 
-(minus operator which will return the difference between two distinct
objects in inches(1 feet = 12 inches).

#include <iostream>
#include <math.h>

using namespace std;

class Distance
{
private:
    int feetinches;

public:
    Distance(int f, int i)
    {
        feet = f;
        inches = i;
    }
    void showData()
    {
        cout << "The distance is : " << feet << " feet and " << inches << " inches." << endl;
    }
    int operator-(Distance b)
    {
        int x = feet * 12 + inches;
        int y = b.feet * 12 + b.inches;
        int diff = x - y;
        diff = abs(diff);
        return diff;
    }
};
int main()
{
    Distance a(2010), b(105);
    int c;
    a.showData();
    b.showData();
    c = a - b;
    cout << "The difference is : " << c << " inches" << endl;
    return 0;
}

Output:

The distance is : 20 feet and 10 inches.
The distance is : 10 feet and 5 inches.
The difference is : 125 inches

Design a class Distance having data member Feet and inches. Define data 
member function showdata(). The objects of class must be created by 
passing out data. Overload == operator which will return true (1)
if the data o two object is same else it will return false(0).


#include <iostream>
using namespace std;

class Distance
{
private:
    int feetinches;

public:
    Distance(int f, int i)
    {
        feet = f;
        inches = i;
    }
    void showData()
    {
        cout << "The distance is : " << feet << " feet and " << inches << " inches." << endl;
    }
    int operator==(Distance b)
    {
        if (feet == b.feet && inches == b.inches)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }
};

int main()
{

    Distance a(2030), b(2090);
    if (a == b)
    {
        cout << "Both distaces are equal!!" << endl;
    }
    else
    {
        cout << "Both are not equal" << endl;
    }

    return 0;
}

Output:

Both are not equal


Design a class MyString having data member to store text and member
function showdata(). the objects of the class must be created by using 
1 or 0 arguments. Overload the + operator which will 
which will concatenate data of two MyString objects.
#include <iostream>
#include <string.h>

using namespace std;

class MyString
{
private:
    char text[30];

public:
    MyString(char a[] = "")
    {
        strcpy(text, a);
    }
    void showData()
    {
        cout << "The Text is : " << text << endl;
    }
    MyString operator+(MyString b)
    {
        MyString c;
        strcpy(c.texttext);
        strcat(c.text, b.text);
        return c;
    }
};
int main()
{

    MyString a("Ayushismyname");
    a.showData();
    MyString b("Codes");
    b.showData();

    MyString c;

    c = a + b;
    c.showData();

    return 0;
}

Output:

The Text is : Ayushismyname
The Text is : Bulbule
The Text is : AyushismynameCodes


WAP to overload subscript operator-- >  [ ] 

#include <iostream>
using namespace std;

class MyArray
{
private:
    int arr[5];

public:
    MyArray()
    {
        for (int i = 0i < 5i++)
        {
            arr[i= i;
        }
    }
    void showData()
    {
        cout << "Array is As Follows: " << endl;

        for (int i = 0i < 5i++)
        {
            cout << arr[i<< "\t";
        }
        cout << endl;
    }
    int operator[](int i)
    {
        if (i < 0 || i > 4)
        {
            cout << "Exeption Out Of array bounding!!" << endl;
            return -1;
        }
        else
        {
            return arr[i];
        }
    }
};
int main()
{

    MyArray a;
    a.showData();
    cout << "a[1] = " << a[1] << endl;
    cout << "a[2] = " << a[2] << endl;
    cout << "a[3] = " << a[3] << endl;
    cout << "a[4] = " << a[4] << endl;

    return 0;
}

Output:

Array is As Follows:
0       1       2       3       4
a[1] = 1
a[2] = 2
a[3] = 3
a[4] = 4

Design a class Myinteger with data member N to store int value.
Define member function showdata().whose objects can be created 
by passing the data. Overload the ^(i.e caret ) operator to find 
raised to power b.

#include <iostream>
#include <math.h>

using namespace std;

class MyInteger
{
private:
    int N;

public:
    MyInteger(int n = 0)
    {
        N = n;
    }
    void showData()
    {
        cout << "The int value: " << N << endl;
    }
    MyInteger operator^(MyInteger b)
    {
        MyInteger c;
        c.N = pow(N, b.N);
        return c;
    }
};
int main()
{
    MyInteger a(20);
    a.showData();
    MyInteger b(3);
    b.showData();
    MyInteger c = a ^ b;
    cout << "a^b is: " << endl;
    c.showData();
    return 0;
}

Output:

The int value: 20
The int value: 3
a^b is:
The int value: 8000


Binary Operator overloading using friend function

To overload a binary operator using friend function ,the operator function
will accept two arguments.

Here the frist oprand will come as first argument and second operand will 
come as second argument.It in not necassasey that the object should be an object.
Gneral Synatax:

class C1
{
    ---------
    friend return_type op(data_type_arg_1, data_type_rag1);
};
class C2
{
    ---------
    friend return_type op(data_type_arg_1, data_type_rag1);
};
 
Design a class Alpha with data member A to Store a value.Design a class 
Beta with data member B to store int int value.Overload the + operator 
by using friend function which will add Alpha objject with Beta objects and 
return the results.
#include <iostream>
using namespace std;

class Beta;
class Alpha
{
private:
    int A;

public:
    Alpha(int a)
    {
        A = a;
    }
    friend int operator+(AlphaBeta);
};
class Beta
{
private:
    int B;

public:
    Beta(int b)
    {
        B = b;
    }
    friend int operator+(AlphaBeta);
};

int operator+(Alpha a, Beta b)
{
    return (a.A + b.B);
}

int main()
{

    Alpha a(10);
    Beta b(20);
    int c = a + b;
    cout << "C = " << c << endl;
    return 0;
}

Output:

C = 30

Design a class Time with data member hours and & minutes.
Design a member function showdata();whose objects acn be 
created by passing the data can be created by passing 2 or
zero arguments. Overloadt the + operator friend function 
which will add n hours with time object.

#include <iostream>
using namespace std;

class Time
{
private:
    int hours;
    int minutes;

public:
    Time(int h = 0int m = 0)
    {
        hours = h;
        minutes = m;
    }
    void showData()
    {
        cout << "Hours: " << hours << " and Minutes: " << minutes << endl;
    }
    friend Time operator+(Timeint);
    friend Time operator+(intTime);
};
Time operator+(Time p, int n)
{
    Time t;
    t.hours = p.hours + n;
    t.minutes = p.minutes + n;
    return t;
}
Time operator+(int n, Time p)
{
    Time t;
    t.hours = p.hours + n;
    t.minutes = p.minutes + n;
    return t;
}
int main()
{
    Time a(2030), b;
    a.showData();
    b.showData();
    b = a + 3; //Implicit Calls
    b.showData();

    Time c(225), d;
    c.showData();
    d.showData();
    d = operator+(4c); //Explicit call
    d.showdata();
    return 0;
}

Output:

Hours: 20 and Minutes: 30
Hours: 0 and Minutes: 0
Hours: 23 and Minutes: 33
Hours: 2 and Minutes: 25
Hours: 0 and Minutes: 0
Hours: 6 and Minutes: 29

Stream class:  C++ I/O system contains a hierarchy of
classes that are used to defined various streams to deal with both
the console & disk files. Thes class are called stream classes.
ios -->input output  stream

Unformatted i/o operators:
Overload operators >> & <<
cin and cout objects are used for i/p & o/p
of data of diff. type
cin>>item1>>...>>item n;
cout<<item..<<item n;


#include <iostream>
using namespace std;

class Distance
{
private:
    int feet;
    int inches;

public:
    Distance()
    {
        feet = 0;
        inches = 0;
    }
    Distance(int f, int i)
    {
        feet = f;
        inches = i;
    }
    friend ostream &operator<<(ostream &output, const Distance &D)
    {
        output << "F : " << D.feet << "I : " << D.inches;
        return output;
    }
    friend istream &operator>>(istream &input, Distance &D)
    {
        input >> D.feet >> D.inches;
        return input;
    }
};
int main()
{
    Distance D1(1110), D2(511), D3;

    cout << "Enter the value of objects: " << endl;
    cin >> D3;

    cout << "Frist Distance: " << D1 << endl;
    cout << "Second Distance: " << D2 << endl;
    cout << "Third Distance: " << D3 << endl;

    return 0;
}

Output:

Enter the value of objects:
16 23
Frist Distance: F : 11I : 10
Second Distance: F : 5I : 11
Third Distance: F : 16I : 23






Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected