Basic Objective C object

Posted by on Apr 12, 2012 in apps, code | No Comments


#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];
        
    }
}


Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponShare on RedditDigg this

Leave a Reply