In: Computer Science
6.
Assignment Instructions:
Please respond with at least 100 words. Your initial post should address each of the topic below thoughtfully.
Topic: Do you think there might be a way to directly access commonly used operators, such as the arithmetic operators or the stream operators, with enumeration types. For example, would overloading these operators be a good solution? Why or why not?
Step(1)
What is Operator overloding.........???
Operator overloading allows C/C++ operators to have user-defined meanings on user-defined types (classes). Overloaded operators are syntactic sugar for function calls.
Or we can also Say
In C++, we can make operators to work for user defined classes.
This means C++ has the ability to provide the operators with a
special meaning for a data type, this ability is known as operator
overloading.
For example, we can overload an operator ‘+’ in a class like String
so that we can concatenate two strings by just using +.
Other example classes where arithmetic operators may be overloaded
are Complex Number, Fractional Number, Big Integer, etc.
Step(2)
Operator overloding of "+" oprator
#include <iostream>
using namespace std;
class A
{
int x;
public:
A(){}
A(int i)
{
x=i;
}
void operator+(A);
void display();
};
void A :: operator+(A a)
{
int m = x+a.x;
cout<<"The result of the addition of two objects is : "<<m;
}
int main()
{
A a1(5);
A a2(4);
a1+a2;
return 0;
}
Output
The result of the addition of two objects is : 9
Step(3)
let's Discuss your Question
Operator or fumction overloads have No effect on performance, whatsoever. Overload resolution is done at compile time. Overloading won't cause any drop in execution speeds. This person may have been confusing virtual functions, which are typically implemented with a pointer indirection.
And it also reduces documentation complexity. Programmer uses the memory in better way and it is easier for him to trace the function by its name. programmer got the advantage of defining the near similar functionality with the same name.
I tihink There is no real downside to it other than losing short-circuiting on logical operators, at least in C++.
As long as it follows the expected behavior of the operator, overloads don't cause any problems. Operator overloading is just syntatic sugar for a function call. There is nothing magical about it. It just makes some code more idiomatic.
IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I
WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK
YOU!!!!!!!!----------