Programming in C

(Barry) #1

414 Chapter 19 Object-Oriented Programming


Program 19.1 Working with Fractions in C
// Simple program to work with fractions
#include <stdio.h>

typedef struct {
int numerator;
int denominator;
} Fraction;

int main (void)
{
Fraction myFract;

myFract.numerator = 1;
myFract.denominator = 3;

printf ("The fraction is %i/%i\n", myFract.numerator, myFract.denominator);

return 0;
}

Program 19.1 Output
The fraction is 1/3

The next three sections illustrate how you might work with fractions in Objective-C,
C++, and C#, respectively.The discussion about OOP that follows the presentation of
Program 19.2 applies to OOP in general, so you should read these sections in order.

Defining an Objective-C Class to Work with
Fractions
The Objective-C language was invented by Brad Cox in the early 1980s.The language
was based on a language called SmallTalk-80 and was licensed by NeXT Software in
1988.When Apple acquired NeXT in 1988, it used NEXTSTEP as the basis for its Mac
OS X operating system. Most of the applications found today on Mac OS X are written
in Objective-C.
Program 19.2 shows how you can define and use a Fractionclass in Objective-C.

Program 19.2 Working with Fractions in Objective-C
// Program to work with fractions – Objective-C version

#import <stdio.h>
#import <objc/Object.h>
Free download pdf