Exercises

Add the following operators to work with both MutableVector3D<T> and ImmutableVector3D<T>:

  • ==: This determines whether all the elements that compose a 3D vector (x, y, and z) are equal.
  • +: This sums each element that composes a 3D vector and saves the result in each element or in the new returned instance according to the class version (mutable or immutable). The new x must have the result of the left-hand side x + right-hand side x, the new y must be that of the left-hand side y + right-hand side y, and the new z must be that of the left-hand side z + right-hand side z.

In Chapter 4, Inheritance, Abstraction and Specialization, we created an Animal class and then defined specific operator functions to allow us to use operators with instances of this class. Redefine this class to conform to both the Comparable and Equatable protocols.

The following lines show the source code for the Equatable protocol:

    public protocol Equatable { 
      static func ==(lhs: Self, rhs: Self) -> Bool 
    } 

The following lines show the source code for the Comparable protocol, which inherits from the Equatable protocol:

    public protocol Comparable : Equatable { 
      static func <(lhs: Self, rhs: Self) -> Bool 
      static func <=(lhs: Self, rhs: Self) -> Bool 
      static func >=(lhs: Self, rhs: Self) -> Bool 
      static func >(lhs: Self, rhs: Self) -> Bool 
    } 

Implement all the necessary operator functions to make the Animal class conform to both the protocols.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.75.59