An Objective-C Class for Working with Fractions

Now it’s time to define an actual class in Objective-C and learn how to work with instances of the class.

Once again, you’ll learn procedure first. As a result, the actual program examples might not seem very practical. We get into more practical stuff later.

Suppose that you need to write a program to work with fractions. Maybe you need to deal with adding, subtracting, multiplying, and so on. If you didn’t know about classes, you might start with a simple program that looked like this.

Program 3.1

// Simple program to work with fractions#import <Foundation/Foundation.h>int main (int argc, char * argv[]){   @autoreleasepool {      int  numerator = 1;      int  denominator ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.