Basic Objective C object

[c collapse=”false”]

#import <Foundation/Foundation.h>

// the interface
@interface Car : NSObject {
int wheels;
int doors;
}

-(void) setNumWheels: (int) w;
-(void) setNumDoors: (int) d;
-(void) print;

@end

// the implementation
@implementation Car

-(void) seNumWheels: (int) w{
wheels = w;
}

-(void) setNumDoors: (int) d{
doors = d;
}

-(void) print {
NSLog (@"My car has %i wheels and %i doors.", doors, wheels);
}

@end

// the program
int main(int argc, const char * argv[])
{
@autoreleasepool {
Car *myCar;
myCar = [Car alloc];
myCar = [myCar init];

[myCar seNumWheels:4 ];
[myCar setNumDoors:4];
[myCar print];

}
}

[/c]

Post a comment

You may use the following HTML:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>