y we use operators in C++

tell me plss that y we use operator in c++ with example
These are from Professional C++ by Nicholas A. Solter and Scott J. Kleper

About overloading an operator:
• to make classes behave like built-in types
note: the closer the classes are to built-in types, the easier it will be for clients to use.
• to gain greater control over behavior in your program.
main purpose
• to make things easier for clients of the class.

for example:
1
2
3
4
5
6
//Global Operator+
 //from Professional C++
aThirdCell = myCell + 4; // Works fine.
aThirdCell = myCell + 5.6; // Works fine.
aThirdCell = 4 + myCell; // FAILS TO COMPILE!
aThirdCell = 5.6 + myCell; // FAILS TO COMPILE! 

implicit conversion works fine when the SpreadSheetCell object is on the left of the operator, but doesn't work when it's on the right.

There's no way you can get the above code to work with an operator+ method

However, you can get it to work if you replace the in-class operator+ with a global operator+ function that is not tied to any particular object.
The function looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//how to compile it with operator+
const SpreadsheetCell operator+(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
    SpreadsheetCell newCell;
    newCell.set(lhs.mValue + rhs.mValue); // Call set to update mValue and mString.
    return (newCell);
}
class SpreadsheetCell
{
    public:
    // ...
    friend const SpreadsheetCell operator+(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
    //Omitted for brevity
};

//now
aThirdCell = myCell + 4; // Works fine.
aThirdCell = myCell + 5.6; // Works fine.
aThirdCell = 4 + myCell; // Works fine.
aThirdCell = 5.6 + myCell; // Works fine.

Note that the implementation of the global operator+ accesses protected data members of
SpreadsheetCell objects. Therefore, it must be a friend function of the SpreadsheetCell class:

Hope this helps. By the way, the examples above are from Professional C++ by Solter and Kleper. I shared this because it is easy to understand what operators are.
I would say that it gives classes algebraic consistency. Let's see... In algebra you have numbers (I would not be very technical). Numbers by themselves aren't very useful. You can do nothing with just numbers in your real life.

But then, there are operators defined which can be applied between these numbers. For example, we have the add operator. Applying the add operator to the pair (2,3) returns 5. We write it this way in maths: 2+3=5. Similarly, we define comparision operators and so on.

Suddenly, numbers with their operations have become a very useful tool. We use it in our day to day constantly. Numbers alone are useless. Numbers with operations are basic tool in our lives.

Now bring that back to programming. Imagine you have a class representing light colors (I mean green, blue, red and so on). That class named Color (yes, I'm sooo original). What is it useful for? Well, it is a color and... nothing else. But add operators to this class and see!!

What happens in real life if you mix red and green lights? You get yellow. So define the operator + for your Color class in such a way that, when mixing red and green instances of the Color class, you get a yellow instance of the class. Also, define a minus operator so that doing red-red returns black. With this kind of class (and tons of additional work) you can build a lightning engine for a game.
I would say someone is probably trolling, and hence you probably don't need to waste your time giving an answer.
Topic archived. No new replies allowed.