answersLogoWhite

0


Best Answer

The assignment is done explicit without internal operation.

Subject to the programming language, explicit assignment operators are needed wherever implicit ones are insufficient. Implicit assignment is typically implemented as a flat copy, while explicit overloading of the assignment operator allows for any other suitable behavior.

Consider this example in pseudocode similar to C++:

class Demo {

int* valuepointer;

...

};

Demo a, b;

...

b = a;

Assigning a to b using implicit assignment means that a.valuepointer and b.valuepointer share the same value. Both a and b can change the pointed-to value, and the either will "see" the change.

This is the required behavior in some cases, but often, you'd want to explicitly assign a to b such that each has its own pointer, accessing different copies of the value. This behavior would require an explicit assignment operator (or copy constructor).

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

Java does not support operator overloading. Operator overloading is the scenario where you overload a particular operator to do something that it is not designed to do.

Ex: if you make the operator "*" do addition or the operator "-" do multiplication, imagine the chaos that would ensue in your program. So the java designers blocked this feature of operator overloading.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

It is necessary whenever you wish to provide an operation which doesn't currently exist, using an operator that is both familiar and intuitive. For instance, if you design a class for which it would be desirable to return the product of an instance of that class with another instance of the same class, or perhaps an instance of another class, or even a primitive, then you would overload the plus operator accordingly, providing sufficient overloads to cater for your object whether it is the l-value, or the r-value, or indeed both.

Not all operators makes sense to all objects, however. Therefore you should only provide operator overloads where it would make sense. You should never overload operators if the operation would be ambiguous or unpredictable, nor should you alter the core operation. While it can be fun to overload the plus operator to perform a subtraction or a division, it's hardly an intuitive use of the operator. If there is any ambiguity, then use a method instead (one that clearly describes the operation). Unpredictable code is, at best, difficult to read, but is ultimately useless as other users will be wary of any other code you produce.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Java does not support operator overloading. Operator overloading is the scenario where you overload a particular operator to do something that it is not designed to do.

Ex: if you make the operator "*" do addition or the operator "-" do multiplication, imagine the chaos that would ensue in your program. So the java designers blocked this feature of operator overloading.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Java does not support operator Overloading.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why is it necessary to overload an operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the declaration of the function to overload output operator inside class in c plus plus?

You can't overload the insertion operator (<<) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value. You must overload the insertion operator from outside of the class, like so: ostream& operator<<(ostream& lhs, const MyObject& rhs) { lhs << rhs.get_data(); return( lhs ); }


Is sizeof an operator or function why?

You cannot overload the sizeof() operator because that could introduce uncertainty in its evaluation. The sizeof() operator must always produce an accurate and logically predictable result, thus all user-intervention is completely forbidden.


Write c plus plus code to overload operator to find maximum between two objects?

The only operator you need to overload is the greater-than operator. You can then use the std::max function from the standard template library. #include<iostream> #include<algorithm> // for std::max() class my_object { private: int m_data; public: my_object(int data=0): m_data(data) {} my_object(const my_object& copy): m_data(copy.m_data) {} my_object& operator=(const my_object& other) { m_data = other.m_data; } my_object& operator=(const int) { m_data = int; } // greater-than operator overload: const bool my_object::operator> (const my_object& other) { return this->m_data > other.m_data; } }; int main() { my_object a = 42; my_object b = 0; my_object c = std::max(a, b); // alternatively: my_object d = a > b ? a : b; } Note that the alternative method shown above is slightly less obvious than calling std::max, but both do exactly the same thing. However, both methods require you to overload the greater-than operator.


How operator overloading differ from function overloading?

Function overloading is multiple definition with different signatures(the parameters should be different) for the same function. The parameter list have to be different in each definition. The compiler will not accept if the return type alone is changed. Operator overloading is defining a function for a particular operator. The operator loading function can not be overloaded through function overloading.


C plus plus program for overloading operator?

#include<iostream> #include<string> // a simple class class my_class { private: std::string m_caption; public: // default constructor my_class (std::string caption): m_caption (caption) {} // read-only accessor const std::string& get_caption() const { return m_caption; } }; // output stream insertion operator overload std::ostream& operator<< (std::ostream& os, const my_class& obj) { os << obj.get_caption(); return os; } int main() { // call default constructor my_class object1 ("This is the caption for object1"); // exercise output stream insertion operator overload std::cout << object1 << std::endl; }

Related questions

What is the operator that cannot be overloaded in c plus plus and java?

conditional operator , size of operator , membership operator and scope resulation operator can not be overload in c++


How unary minus can be overload in c plus plus?

type operator- ();


What is the declaration of the function to overload output operator inside class in c plus plus?

You can't overload the insertion operator (<<) from inside a class because the l-value must be an ostream object, but operator overloads implemented within classes always implicate the class instance itself as being the l-value. You must overload the insertion operator from outside of the class, like so: ostream& operator<<(ostream& lhs, const MyObject& rhs) { lhs << rhs.get_data(); return( lhs ); }


What is meant when you say you overload an operator?

All operators are built-in but not all operators can operate upon data types they know absolutely nothing about. There are some exceptions such as the new operator and the sizeof operator -- both will work on any datatype. However, for those that cannot, operator overloads allow you to cater specifically for those types. An operator overload is implemented just as you would overload a function, but it is not a function per se because operators have different calling conventions to functions. It is also important to keep in mind that just because you can overload an operator, it does not mean that you should. Operators should only be overloaded when the overload would allow a user to interact with your data type in an intuitive manner; a manner that is consistent with the operator's intended purpose. So while it can be amusing to overload the plus (+) operator to perform a subtraction (-), it could hardly be called intuitive. The assignment operator is the most overloaded operator of them all. This is because it is extremely useful to be able to copy the members of one object and assign those values to another object of the same type. We can also overload the assignment operator to cater for objects of different types, but such assignments are rarely intuitive. Does it make sense to assign the properties of a banana object to a person object? Probably not. Even if you could find a practical reason for doing so, would it be an intuitive operation? Definitely not. Therefore there's no point in providing an operator to cater for this. To create an operator overload, the declaration will often be placed inside the class it pertains to. However there are exceptions. The output stream insertion operator is a good example of this. The following example demonstrates how we can overload an internal operator (the assignment operator) as well as an external operator (output stream insertion operator). #include<iostream> // required to make use of I/O streams class A { private: unsigned m_data; public: // constructors... A (const unsigned data = 0): m_data (data) {} A (const A& copy): m_data (copy.m_data) {} // accessor function (interface) unsigned get_data() const { return m_data; } // operator overloads... A& operator= (const A& rhs) { m_data = rhs.m_data; } A& operator= (const unsigned rhs) { m_data = rhs; } }; std::ostream& operator<< (std::ostream& os, const A& a { os << a.get_data(); return os; } int main() { A a, b; // invoke default constructors a = 42; // call assignment operator overload b = a; // call default assignment operator overload // call insertion operator overload std::cout << a << std::endl; std::cout << b << std::endl; } Output: 42 42


Difference between new operator and operator new?

new operator allows to allocate a memory from the heap..... so a new instance of a class is created.......... but operator new is used to overload the (new) operator........ juast like overloading of other operators


Is sizeof an operator or function why?

You cannot overload the sizeof() operator because that could introduce uncertainty in its evaluation. The sizeof() operator must always produce an accurate and logically predictable result, thus all user-intervention is completely forbidden.


Why should you have to use comparison operator overloading in pairs in c?

You cannot overload operators in C. This is a C++ thing only.


Write c plus plus code to overload operator to find maximum between two objects?

The only operator you need to overload is the greater-than operator. You can then use the std::max function from the standard template library. #include<iostream> #include<algorithm> // for std::max() class my_object { private: int m_data; public: my_object(int data=0): m_data(data) {} my_object(const my_object& copy): m_data(copy.m_data) {} my_object& operator=(const my_object& other) { m_data = other.m_data; } my_object& operator=(const int) { m_data = int; } // greater-than operator overload: const bool my_object::operator> (const my_object& other) { return this->m_data > other.m_data; } }; int main() { my_object a = 42; my_object b = 0; my_object c = std::max(a, b); // alternatively: my_object d = a > b ? a : b; } Note that the alternative method shown above is slightly less obvious than calling std::max, but both do exactly the same thing. However, both methods require you to overload the greater-than operator.


What is operator function?

An operator function implements a particular operator symbol. The database server provides special SQL-invoked functions, called operator functions, that implement operators. An operator function processes one to three arguments and returns a value. When an SQL statement contains an operator, the database server automatically invokes the associated operator function. The association between an operator and an operator function is called operator binding. You can overload an operator function to provide the operator for a UDT. The SQL user can then use the operator with the UDT as well as with the built-in data types. When an SQL statement contains an operator, the database server automatically invokes the associated operator function.


How can you can create a new operator through operator overloading?

You cannot create any new operators in C++. You can only overload the existing ones (although some, such as sizeof, new and delete cannot be overloaded). The only way to create a new operator is to implement it as a standard function with a named identifier. For instance, sqrt() is the standard library function that provides the square root operator, for which no real operator exists.


Can vehicles be parked within the CMA or on a taxiway if the operator deems it necessary for mission completion?

can be parked on a taxiway if the operator deems it neccessary for mission completion.


Can vehicles be parked within the CMA or on a taxiway if the operator deems it necessary for mission completion.?

can be parked on a taxiway if the operator deems it neccessary for mission completion.