Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Operators are overloaded by means of operator functions, which are regular functions with special names: their name begins by the operator keyword followed by the

Operators are overloaded by means of operator functions, which are regular functions with special names: their name begins by the operator keyword followed by the operator sign that is overloaded. The syntax is:
type operator sign (parameters)/*... body ...*/
Consider a Cartesian vector example in 2D that are sets of two coordinates: x and y. The addition operation of two cartesian vectors is defined as the addition both x coordinates together, and both y coordinates together. For example, adding the cartesian vectors
(3,1) and (1,2) together would result in (3+1,1+2)=(4,3). This could be implemented in C++ with overloading + operators.
Similarly, the subtraction operation of two cartesian vectors is defined as the subtraction
of x coordinates, and y coordinates of each vector. For example, subtracting the cartesian
vectors (3,1) and (1,2) together would result in (3-1,1-2)=(2,-1). This could be implemented in C++ with overloading - operators.
c = a + b;
c = a.operator+(b); Both expressions are equivalent!
The operator overloads are just regular functions which can have any behavior; there is actually no requirement that the operation performed by that overload bears a relation to the mathematical or usual meaning of the operator, although it is strongly recommended. For example, a class that overloads operator+ to actually subtract or that overloads opera- tor== to fill the object with zeros, is perfectly valid, although using such a class could be challenging.
The parameter expected for a member function overload for operations such as operator + is naturally the operand to the right hand side of the operator. This is common to all binary operators (those with an operand to its left and one operand to its right). But operators can come in diverse forms. Here you have a table with a summary of the parameters needed for each of the different operators than can be overloaded (please, replace @ by the operator in each case): Where a is an object of class A, b is an object of class B and c is an object
Expression Operator
Member function
Non-member function
@a
-* & !-++
A::operatore()
operator@(A)
a@
A:: operator@(int)
operator@(A, int)
a@b
/%* & |<>==!=<=>=<<>> && ||
,A::operator@(B)
operator@(A,B)
a@b
=+=-=*=/=%=*= &=|=<<=>>=[]
A::operator@(B)
a(b,c...)()
A::operator()(B, C...
a->b
->
A::operator->()
(TYPE) a
TYPE
A:: operator TYPE()
of class C. TYPE is just any type (that operators overloads the conversion to type TYPE).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

More Books

Students also viewed these Programming questions

Question

So what disadvantages have you witnessed? (specific)

Answered: 1 week ago

Question

(1) What is your current leadership development strategy?

Answered: 1 week ago