More OO




CS152

Chris Pollett

Apr. 1, 2009

Outline

Introduction

Message Passing and OO

Preserving State the Fortran Way

But Every Class Would be a Singleton

Some Differences With Between Message Passing and the ADT Style

Objective-C

The Basics of an Objective-C Program

Implementation Files

To implement the functions you use an implementation file and the syntax:

@implementation classname
+classMethod {
    // implementation
}
-instanceMethod {
    // implementation
}
@end

Invoking Methods

Example Objective-C Program

// Interface File
#import <objc/Object.h>

@interface MyHello : Object {
   int myNumber;
}
-setNumber:(int)aNumber;
-sayHello;

@end

Example Program cont'd

// implementation file
#import <stdio.h>
#import "MyHello.h" 
@implementation MyHello
-setNumber:(int)aNumber {    myNumber = aNumber;
} 
-sayHello{    
   printf("Hello! %d\n", myNumber);
} 
-(id) init { //self is like this in Java
    self = [super init];
    if (self) { myNumber = 0; }
    return self;}  
@end

Objective-C Example Last Part.

#import "MyHello.h"
int main(void){
MyHello *hello = [MyHello new];  
[hello setNumber:10]; 
   //set the number to echo
[hello sayHello];
return 0;
}
/* To compile in gcc could type:
gcc -x objective-c -Wno-import main.m MyHello.m -lobjc   */

iPhone/Android Class Plug