Skip to content

Commit

Permalink
refactor 14.1
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Mar 9, 2015
1 parent 6632dbb commit cbc982e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 283 deletions.
67 changes: 67 additions & 0 deletions ch14/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Chapter 14. Overloaded Operations and Conversions

## Exercise 14.1:
>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**
1. We can call an overloaded operator function directly.
2. An overloaded operator function must either be a member of a class or have at least one parameter of class type.
3. 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`, logical `OR`, 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.

## Exercise 14.2:
>Write declarations for the overloaded input, output, addition, and compound-assignment operators for `Sales_data`.
[hpp](ex14_02.h) | [cpp](ex14_02.cpp)

## Exercise 14.3:
>Both `string` and `vector` define an overloaded == that can be used to compare objects of those types. Assuming `svec1` and `svec2 `are `vectors` that hold `strings`, 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**
- [Why does the following not invoke the overloaded operator== (const String &, const String &)? “cobble” == “stone”](http://stackoverflow.com/questions/2690737/why-does-the-following-not-invoke-the-overloaded-operator-const-string-con)

## Exercise 14.4:
>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

## Exercise 14.5:
>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`

[hpp](ex14_05.h) | [cpp](ex14_05.cpp)
14 changes: 0 additions & 14 deletions ch14/ex14.1.2.3.4.5/Alan.h

This file was deleted.

82 changes: 0 additions & 82 deletions ch14/ex14.1.2.3.4.5/Sales_data.cc

This file was deleted.

98 changes: 0 additions & 98 deletions ch14/ex14.1.2.3.4.5/Sales_data.h

This file was deleted.

89 changes: 0 additions & 89 deletions ch14/ex14.1.2.3.4.5/main.cpp

This file was deleted.

Loading

0 comments on commit cbc982e

Please sign in to comment.