Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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'll get into more practical stuff later.

Suppose you needed to write a program to work with fractions. Maybe you needed to deal with adding, subtracting, multiplying them, 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 <stdio.h>

int main (int argc, char *argv[])
{
 int numerator = 1;
 int denominator = 3;
 printf ("The fraction is %i/%i\n", 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. The first 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 into the variable numerator and 3 into 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 <stdio.h>
#import <objc/Object.h>

//------- @interface section -------

@interface Fraction: Object
{
 int numerator;
 int denominator;
}

-(void)  print;
-(void)  setNumerator: (int) n;
-(void)  setDenominator: (int) d;

@end

//------- @implementation section -------


@implementation Fraction;
-(void) print
{
 printf (" %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[])
{
 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

 printf ("The value of myFraction is:");
 [myFraction print];
 printf ("\n");
 [myFraction free];

 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, as depicted in Figure 3.1:

  • @interface section

  • @implementation section

  • program section

Figure 3.1Figure 3.1 Program structure.

The @interface section describes the class, its data components, and its methods, whereas the @implementation section contains the actual code that implements these methods. Finally, 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'll keep it all together in a single file.

  • + Share This
  • 🔖 Save To Your Account