In what ways does an overloaded operator differ from a built-in operator? In what ways are overloaded operators the same as the built-in operators?
Differ
- We can call an overloaded operator function directly.
- An overloaded operator function must either be a member of a class or have at least one parameter of class type.
- A few operators guarantee the order in which operands are evaluated. These overloaded versions of these operators do not preserve order of evaluation and/or short-circuit evaluation, it is usually a bad idea to overload them.
In particular, the operand-evaluation guarantees of the logical
AND
, logicalOR
, and comma operators are not preserved, Moreover, overloaded versions of&&
or||
operators do not preserve short-circuit evaluation properties of the built-in operators. Both operands are always evaluated.
Same
- An overloaded operator has the same precedence and associativity as the corresponding built-in operator.
Write declarations for the overloaded input, output, addition, and compound-assignment operators for
Sales_data
.
Both
string
andvector
define an overloaded == that can be used to compare objects of those types. Assumingsvec1
andsvec2
arevectors
that holdstrings
, identify which version of == is applied in each of the following expressions:
- (a)
"cobble" == "stone"
- (b)
svec1[0] == svec2[0]
- (c)
svec1 == svec2
- (d)
"svec1[0] == "stone"
(a) neither. (b) string
(c) vector
(d) string
Reference
Explain how to decide whether the following should be class members:
- (a) %
- (b) %=
- (c) ++
- (d) ->
- (e) <<
- (f) &&
- (g) ==
- (h) ()
(a) symmetric operator. Hence, non-member
(b) changing state of objects. Hence, member
(c) changing state of objects. Hence, member
(d) = [] () -> must be member
(e) non-member
(f) symetric , non-member
(g) symetric , non-member
(h) = [] () -> must be member
In exercise 7.40 from 7.5.1 (p. 291) you wrote a sketch of one of the following classes. Decide what, if any, overloaded operators your class should provide.
Such as Book
Define an output operator for your
Sales_data
class.
see [Exercise 14.2](#Exercise 14.2).
Define an output operator for you String class you wrote for the exercises in 13.5 (p. 531).
Define an output operator for the class you chose in exercise 7.40 from 7.5.1 (p. 291).
see [Exercise 14.5](#Exercise 14.5)
Define an input operator for your Sales_data class.
see [Exercise 14.2](#Exercise 14.2).
Describe the behaviour of the Sales_data input operator if given the following input:
-
(a) 0-201-99999-9 10 24.95
-
(b) 10 24.95 0-210-99999-9
-
(a) correct format.
-
(b) ilegal input. But
0-210-99999-9
will be converted to a float stored in this object. As a result, the data inside will be a wrong one. Output:10 24 22.8 0.95
check Test
What, if anything, is wrong with the following Sales_data input operator? What would happen if we gave this operator the data in the previous exercise?
istream& operator>>(istream& in, Sales_data& s)
{
double price;
in >> s.bookNo >> s.units_sold >> price;
s.revenue = s.units_sold * price;
return in;
}
no input check. nothing happend.
Define an input operator for the class you used in exercise 7.40 from 7.5.1 (p. 291). Be sure the operator handles input errors.
see [Exercise 14.5](#Exercise 14.5)
Which other arithmetic operators (Table 4.1 (p. 139)), if any, do you think Sales_data ought to support? Define any you think the class should include.
no others.
Why do you think it is more efficient to define
operator+
to calloperator+=
rather than the other way around?
Discussing on SO.
Should the class you chose for exercise 7.40 from 7.5.1 (p. 291) define any of the arithmetic operators? If so, implement them. If not, explain why not.
Define equality and inequality operators for your
StrBlob
(12.1.1, p. 456),StrBlobPtr
(12.1.6, p. 474),StrVec
(13.5, p.526), andString
(13.5, p. 531) classes.