answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write c plus plus code to overload operator to find maximum between two objects?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Explain how objects are created in Java?

with new operator


Why is it necessary to overload an operator?

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).


Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


What are the disadvantage of operator overloading?

The only disadvantage of operator overloading is when it is used non-intuitively. All operators must behave with predictable results, thus it makes no sense to implement the plus (+) operator so that it behaves like a subtract (-) operator, or a multiply (*) operator, or indeed anything other than the intuitive sum of two objects.


Can memory which has been allocated by new be freed by free?

No, you have to use the operator delete to objects created by new.

Related questions

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&lt;iostream&gt; // 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&amp; copy): m_data (copy.m_data) {} // accessor function (interface) unsigned get_data() const { return m_data; } // operator overloads... A&amp; operator= (const A&amp; rhs) { m_data = rhs.m_data; } A&amp; operator= (const unsigned rhs) { m_data = rhs; } }; std::ostream&amp; operator&lt;&lt; (std::ostream&amp; os, const A&amp; a { os &lt;&lt; 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 &lt;&lt; a &lt;&lt; std::endl; std::cout &lt;&lt; b &lt;&lt; std::endl; } Output: 42 42


Where can one learn more about the between operator in SQL?

w3schools is a website with free tutorials on a variety of internet-based languages, including HTML, CSS, Javascript, and SQL. It includes an article on the between operator. Briefly, the between operator is used to select objects in a database that are between given values, eg finding all products in a catalog with prices between X and Y.


Explain how objects are created in Java?

with new operator


Why is it necessary to overload an operator?

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).


Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


What is the point of overloaded operators?

Operator overloads are intended to provide an intuitive interface to your user-defined types (classes). So just as you can add two integers together using the plus operator, you should be able to do the same with your user-defined data types. However, not all operators will make sense to all objects. For instance, if the sum of two objects cannot be easily defined then it makes no sense to provide an addition operator. E.g., fruit = apple + orange would not make any sense in the real world, but float = apple + 1.25 might make sense if the primary member of apple were its price or its weight, or some other numeric value. Ultimately, an operator must be both intuitive and predictable in order to be useful. If not, then a more self-explanatory method (function) would be better than an operator overload.


What is the maximum number of objects that can be had in memorandum of association?

4


What are the disadvantage of operator overloading?

The only disadvantage of operator overloading is when it is used non-intuitively. All operators must behave with predictable results, thus it makes no sense to implement the plus (+) operator so that it behaves like a subtract (-) operator, or a multiply (*) operator, or indeed anything other than the intuitive sum of two objects.


Can memory which has been allocated by new be freed by free?

No, you have to use the operator delete to objects created by new.


Which operator is allow to access hidden global variable?

The scope resolution operator, ::, overrides local scope and allows access to objects that are hidden due to global to local scope rules.


Which operator require to call c function using object name?

The operator required to call c function using object name is function object. Other operator names that deal with objects are structure dereference, structure reference, and indirection


What is the use of new operator?

A new operater is used to allocating a memory space for a particular object.