- What Is an Object, Anyway?
- Instances and Methods
- An Objective-C Class for Working with Fractions
- The @interface Section
- The @implementation Section
- The program Section
- Accessing Instance Variables and Data Encapsulation
- Summary
- Exercises

## 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 = 3; NSLog (@"The fraction is %i/%i", numerator, denominator); } return 0; }

#### Program 3.1. Output

The fraction is 1/3

In Program 3.1, the fraction is represented in terms of its numerator and denominator. After the `@autoreleasepool` directive, the two lines in `main` both declare the variables `numerator` and `denominator` as integers and assign them initial values of `1` and `3`, respectively. This is equivalent to the following lines:

int numerator, denominator; numerator = 1; denominator = 3;

We represented the fraction `1/3` by storing `1` in the variable `numerator` and `3` in the variable `denominator`. If you needed to store a lot of fractions in your program, this could be cumbersome. Each time you wanted to refer to the fraction, you’d have to refer to the corresponding numerator and denominator. And performing operations on these fractions would be just as awkward.

It would be better if you could define a fraction as a single entity and collectively refer to its numerator and denominator with a single name, such as `myFraction`. You can do that in Objective-C, and it starts by defining a new class.

Program 3.2 duplicates the functionality of Program 3.1 using a new class called `Fraction`. Here, then, is the program, followed by a detailed explanation of how it works.

#### Program 3.2.

// Program to work with fractions – class version #import <Foundation/Foundation.h> //---- @interface section ---- @interface Fraction: NSObject -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; @end //---- @implementation section ---- @implementation Fraction { int numerator; int denominator; } -(void) print { NSLog (@"%i/%i", numerator, denominator); } -(void) setNumerator: (int) n { numerator = n; } -(void) setDenominator: (int) d { denominator = d; } @end //---- program section ---- int main (int argc, char * argv[]) { @autoreleasepool { Fraction *myFraction; // Create an instance of a Fraction myFraction = [Fraction alloc]; myFraction = [myFraction init]; // Set fraction to 1/3 [myFraction setNumerator: 1]; [myFraction setDenominator: 3]; // Display the fraction using the print method NSLog (@"The value of myFraction is:"); [myFraction print]; } return 0; }

#### Program 3.2. Output

The value of myFraction is: 1/3

As you can see from the comments in Program 3.2, the program is logically divided into three sections:

`@interface`section`@implementation`section`program`section

The `@interface` section describes the class and its methods, and the `@implementation` section describes the data (the *instance variables* that objects from the class will store) and contains the actual code that implements the methods declared in the interface section. The `program` section contains the program code to carry out the intended purpose of the program.

Each of these sections is a part of every Objective-C program, even though you might not need to write each section yourself. As you’ll see, each section is typically put in its own file. For now, however, we keep it all together in a single file.