O'Reilly logo

Swift 3 ObjectOriented Programming - Second Edition by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Declaring operator functions for specific subclasses

We already declared an operator function that allows any instance of Animal or its subclasses to use the postfix increment (++) operator. However, sometimes we want to specify a different behavior for one of the subclasses and its subclasses.

For example, we might want to express the age of dogs in the age value that is equivalent to humans. We can declare an operator function for the postfix increment (++) operator that receives a Dog instance as an argument and increments the age value 7 years instead of just one. The following lines show the code that achieves this goal. The code file for the sample is included in the swift_3_oop_chapter_04_14 folder:

 public postfix func ++ (dog: Dog) { dog.age ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required