Presentation is loading. Please wait.

Presentation is loading. Please wait.

Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY.

Similar presentations


Presentation on theme: "Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY."— Presentation transcript:

1 Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY

2 F ILE E XTENSIONS  Header files  “.h file”  Implementation files  “.m file”

3 C REATING C LASSES T HE INTERFACE (F RACTION. H ) #import @interface Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; @end Inheritance Class:Parent Instance variables Instance methods -= instance + = class (static)

4 C REATING C LASSES T HE IMPLEMENTATION (F RACTION. M ) #import "Fraction.h“ #import @implementation Fraction -(void) print { printf( "%i/%i", numerator, denominator ); } -(void) setNumerator: (int) n { numerator = n; } -(void) setDenominator: (int) d { denominator = d; } -(int) denominator { return denominator; } -(int) numerator { return numerator; } @end

5 T HE C LIENT (M AIN. M ) #import #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; // create a new instance // alloc gets the memory and init is the constructor // set the values [frac setNumerator: 1]; // calling setNumerator on frac and passing 1 [frac setDenominator: 3]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); // free memory [frac release];// if we alloc it, we need to release it return 0; } Output: The fraction is: 1/3

6 M ULTIPLE P ARAMETERS F UNKY S YNTAX In fraction.h... -(void) setNumerator: (int) n andDenominator: (int) d;...

7 M ULTIPLE P ARAMETERS F UNKY S YNTAX In fraction.m... -(void) setNumerator: (int) n andDenominator: (int) d { numerator = n; denominator = d; }...

8 M ULTIPLE P ARAMETERS F UNKY S YNTAX – I N M AIN. M #import #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; // create new instances Fraction *frac2 = [[Fraction alloc] init]; [frac setNumerator: 1]; // set the values [frac setDenominator: 3]; [frac2 setNumerator: 1 andDenominator: 5]; // combined set printf( "The fraction is: " ); [frac print]; printf( "\n" ); printf( "Fraction 2 is: " ); [frac2 print]; printf( "\n" ); [frac release]; // free memory [frac2 release]; return 0; } Output: The fraction is: 1/3 Fraction 2 is: 1/5

9 C ONSTRUCTORS  They are just a method named init  Not a special construct like in Java or C++  Default constructor  -(id) init;  Id is a generic type for an arbitrary object

10 C ONSTRUCTORS In Fraction.h... -(Fraction*) initWithNumerator: (int) n denominator: (int) d;... In Fraction.m... -(Fraction*) initWithNumerator: (int) n denominator: (int) d { self = [super init]; // call the base class constructor if ( self ) { // if (self != nil) – making sure we didn’t run out of memory on last call [self setNumerator: n andDenominator: d]; } return self; // I’m a constructor }... In main.m Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];

11 A CCESS  @public, @private and @protected  Default is @protected

12 A CCESS. H #import @interface Access: NSObject { @public int publicVar; @private int privateVar; // private doesn’t go before each variable, unlike Java int privateVar2; @protected int protectedVar; } @end

13 A CCESS E XAMPLE – M AIN. M #import "Access.h" #import int main( int argc, const char *argv[] ) { Access *a = [[Access alloc] init]; // works a->publicVar = 5; // notice the -> notation here. printf( "public var: %i\n", a->publicVar ); // doesn't compile //a->privateVar = 10; //printf( "private var: %i\n", a->privateVar ); [a release]; return 0; }

14 O BJECT -O RIENTED F EATURES  Inheritance (not multiple)  Override parent class methods by putting implementation in the child class  In a Square.h file… #import "Rectangle.h" @interface Square: Rectangle // square inherits from rectangle -(Square*) initWithSize: (int) s; // constructor -(void) setSize: (int) s; -(int) size; @end

15 B RACKETS VS. D OTS IS C ONFUSING  Bracket Notation  [object method]  Or dot (.) notation  object.method  [frac setNumerator: 1] becomes frac.Numerator = 1

16 MISC  The ‘@’ symbol is used to introduce Objective-C keywords so they won’t conflict with the C or C++ stuff  You will see a lot of “NS” – “NextStep” – code from back in the day.  YES and NO instead of TRUE and FALSE


Download ppt "Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY."

Similar presentations


Ads by Google