C++ 30: Operator Overloading

 The compiler Know how to use an operator(i.e +,-,++,--,<,.) with the variable of built in (i.e int float,char,etc) data_type but,the compiler don't konw how to use an operator with user defined datatypes i.e. objects.

Operator overloading is a process to inform compiler about how to use an operator with objects

Example:

Design a class Circle with data member Radius. Define member function 
area()The objects of the class must be created by passing the data.
Overload the ++ operator as prefix and postfix which will increase 
the radius of circle by one.
#include <iostream>
using namespace std;
class Circle
{
private:
    float R;

public:
    Circle(int r)
    {
        R = r;
        cout << "R = " << r << endl;
    }
    void area()
    {
        double a = R * 3.114 * R;
        cout << "R = " << R << endl;
        cout << "Area of Circle is " << a << endl;
    }

    //Prefix Operator function

    void operator++()
    {
        R++;
    }
    void operator--()
    {
        --R;
    }
};
main()
{
    Circle a(5);
    a.area();
    ++a;
    a.area();

    Circle b(5);
    b.area();
    --b;
    b.area();
}

Output:

R = 5
R = 5
Area of Circle is 77.85
R = 6
Area of Circle is 112.104
R = 5
R = 5
Area of Circle is 77.85
R = 4
Area of Circle is 49.824


Unary Operator Overloading:

Unary Operator: The operator which can be use with a single variable/ operand are called as an unary operator.
Example:  ++, --, !,=,+,etc.

Design a Class Date with data member Day, Month,Year. Define Data 
member function showdata(). The objects of the class can be crested 
by passing the data. Overload ++ and -- operator as prefix and
which will only increase or decrease the year of Date by 1 respectively.

#include <iostream>
using namespace std;

class Date
{
private:
    int day;
    int month;
    int year;

public:
    Date(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }
    void display();
    void operator++(int)
    {
        month++;
        day++;
        year++;
    }
    void operator--(int)
    {
        year--;
    }
};
void Date::display()
{
    cout << "Date is: " << day << " : " << month << " : " << year << endl;
}
main()
{
    Date a(2022020);
    a.display();
    a++;
    cout << "After a++ :" << endl;
    a.display();

    cout << "Second Object" << endl;
    Date b(1042007);
    b.display();
    b--;
    b.display();
}

Output:

Date is: 20 : 2 : 2020
After a++ :
Date is: 21 : 3 : 2021
Second Object
Date is: 10 : 4 : 2007
Date is: 10 : 4 : 2006


Design a Class Distance with data member Feet and inches. Decline member
function showdata().The objects of the class must be created by passing 
the data. Overloading ++ and -- operators as prefix which will increase 
and decrease the data only Feet of the Distance object by 1 respectively.


#include <iostream>
using namespace std;

class Distance
{
private:
    float feetinches;

public:
    Distance(int f, int i)
    {
        feet = f;
        inches = i;
    }
    void showdata()
    {
        cout << "The distance in Feet: " << feet << " inches: " << inches << endl;
    }
    void operator++()
    {
        ++feet;
    }
    void operator--()
    {
        --feet;
    }
};
main()
{
    Distance d1(203);
    d1.showdata();
    ++d1;
    d1.showdata();

    Distance d2(405);
    d2.showdata();
    --d2;
    d2.showdata();
}

Output:

The distance in Feet: 20 inches: 3
The distance in Feet: 21 inches: 3
The distance in Feet: 40 inches: 5
The distance in Feet: 39 inches: 5 

Design a class Currency with data members Rupees and Paise. Define 
a data member function showdata().The objects of the class can be
created by passing data. Overload the ++ and -- operator as prefix 
which will increase or decrease the value Rupees only in Currency 
object by 1.

#include <iostream>
using namespace std;

class currency
{
private:
    int rupeespaise;

public:
    currency(int r, int p);
    void showdata();
    void operator++()
    {
        ++rupees;
    }
    void operator--()
    {
        --rupees;
    }
};

currency::currency(int r, int p)
{
    rupees = r;
    paise = p;
}
void currency::showdata()
{
    cout << "The Currency  is " << rupees << " and " << paise << " paise." << endl;
}
main()
{
    currency c1(1275);
    c1.showdata();
    ++c1; //operator overloading
    c1.showdata();
}

Output:

Enter your age: 56
You can vote


Operator Function Returning Value:
If the function id returning an Object then Create a temporary object 
in operator Function, store the result in temporary object and return the
temporary object.

Design a class Point with data member X and Y. Define member function 
showdata(). The objects of the class can be created by passing 2 or 0 
arguments. Overload unary ! operator which will change the sign of 
co-ordinates in the Point object and will return a new object in result.

#include <iostream>
using namespace std;

class Point
{
private:
    int XY;

public:
    Point(int x = 0int y = 0)
    {
        X = x;
        Y = y;
    }
    void showdata();
    Point operator!()
    {
        Point temp;

        temp.X = X * (-1);
        temp.Y = Y * (-1);
        return temp;
    }
};
void Point::showdata()
{
    cout << "Value of X : " << X << endl;
    cout << "Value of Y : " << Y << endl;
}
main()
{
    Point a(24), b;
    a.showdata();
    b.showdata();

    b = !a;
    b.showdata();
    return 0;
}

Output:

Value of X : 2
Value of Y : 4
Value of X : 0
Value of Y : 0
Value of X : -2
Value of Y : -4


Design a class MyString with a data member N to store text.
Define member function showdata().The objects of the class
can be created by passing 1 or 0 arguments.Overload ~(i.e tilde)
operator which will revrse the content of MyStrig object nd 
will return a new MyString object in result.


#include <iostream>
#include <string.h>

using namespace std;

class MyString
{
    char N[20];

public:
    MyString(char st[] = "AYUSH")
    {
        strcpy(N, st);
    }
    void showdata()
    {
        cout << "The Text is: " << N << endl;
    }
    MyString operator~()
    {

        MyString temp;
        strcpy(temp.NN);
        strrev(temp.N);
        return temp;
    }
};
main()
{

    MyString a("Jack");
    a.showdata();

    MyString b;
    b.showdata();
    b = ~a;
    b.showdata();
}

Output:

The Text is: Jack
The Text is: AYUSH
The Text is: kcaJ







Comments

Popular posts from this blog

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