400 likes | 528 Views
Object Oriented Programming. Similar concepts as in other programming languages Different syntax. Using the console for output. Console is at bottom , useful for debugging If it is not open, click on appropriate View button Can use printf function (from C)
E N D
Object Oriented Programming Similar concepts as in other programming languages Different syntax
Using the console for output • Console is at bottom, useful for debugging • If it is not open, click on appropriate View button • Can use printf function (from C) • printf uses the % notation for formatting • Can take a variable number of arguments
Using the console for output int age = 22; char sex = ‘M’; float gpa = 3.4; printf( “age is %i, sex is %c, gpa is %f\n”, age, sex, gpa ); • Test the above in the main function, the entry point of an app; or inside viewDidLoad method of ViewController.m
Class organization • Similar to C++ organization • Header/Declaration file: fileName.h • documents its API • Implementation file: fileName.m • implements functions declared in .h header file • Xcode provides skeleton files
Header file #import <Foundation/Foundation.h> // Other import (preprocessor) directives @interface ClassName:NSObject { // Instance variable declarations } // Method declarations @end
Import other classes you need #import <Foundation/Foundation.h> // Other import (preprocessor) directives @interface ClassName:NSObject { // Instance variable declarations } // Method declarations @end
Import (preprocessor) directive Contrary to C and C++, in Objective C, the import directive ensures that a file is imported only once we do not have to guard against importing 2 times the same file #import <Foundation/Foundation.h> #import <Foundation/Foundation.h> The above code works (not desirable though)
Class code goes between @interface and @end #import <Foundation/Foundation.h> @interface ClassName:NSObject { // Instance variable declarations } // Method declarations @end
Class code goes between @interface and @end @interface ClassName:NSObject Subclass goes left of : Superclass goes right of : ClassName is the subclass It inherits from NSObject, its superclass NSObject is the root class (“mother of all classes”)
Complex is the name of the class; it inherits from NSObject #import <Foundation/Foundation.h> @interface Complex:NSObject { // Instance variable declarations } // Method declarations @end
Instance variable declarations go between the curly braces #import <Foundation/Foundation.h> @interface Complex:NSObject { int real; int imaginary; } // Method declarations @end
Methods Instance methods Static (class) methods Instance methods are preceded with – Static (class) methods are preceded with + A method can be a void method or can return a value (or an object)
Method declaration syntax // 0 parameter ( returnType ) methodName; // Examples - ( void ) print; - ( int ) getReal; - ( Complex * ) multiplyBy2;
Method with 1 parameter ( returnType ) methodName: ( dataType ) parameterName; • Examples -( Complex * ) add: ( Complex * ) z; -( Complex * ) multiply: ( Complex * )z; • Note: Objective C does not support operator overloading
Method with 2 parameters or more ( returnType ) methodName: ( dataType ) parameterName1 :otherName: ( dataType) parameterName2….; • The name of the method is methodName:otherName: • Example with 3 parameters -( Complex * ) add: ( Complex * ) z1 and: ( Complex * ) z2 andAlso: (Complex * ) z3;
- Instance method #import <Foundation/Foundation.h> @interface Complex:NSObject { int real; int imaginary; } • (void) print; // instance method @end
Complex.h #import <Foundation/Foundation.h> @interface Complex:NSObject { int real; int imaginary; } • (void) print; // instance method • (void) setReal: (int) r; // another instance method @end
ClassName.m is the implementation file #import “ClassName.h” @implementation ClassName // implementation goes here @end • ClassName.h is between double quotes ( “ “ ) not angle brackets (< >) • Import project files double quotes • Import Cocoa framework classes angle brackets
Need to import the header file #import “Complex.h” @implementation Complex // implementation goes here @end
Code goes between @implementation and @end #import “Complex.h” @implementation Complex /* Note: superclass (NSObject) is not specified above */ // implementation goes here @end
Code goes between @implementation and @end #import “Complex.h” @implementation Complex • ( void ) print { printf( “%i + %i i”, real, imaginary ); } // setReal method implemented here @end
Code goes between @implementation and @end #import “Complex.h” @implementation Complex // print method implemented here • ( void ) setReal: (int ) r { real = r; } @end
Test our Complex class • Let’s practice using our class, using the console for now • Declare an object of type Complex • Allocate memory for the object • Use the print method • Use the setReal method
Declare an object of a class // Declare a Complex object // Syntax uses pointer notation (*) // objectName is a pointer of type ClassName ClassName *objectName;
Declare an object of the Complex class // Declare 2 Complex object references Complex *z1; Complex *z2; // z1 and z2 are pointers to Complex objects
Using a method • “Calling a method” = “Sending a message” • Smalltalk syntax • Static void method • [ClassName methodName]; • Instance void method • [objectName methodName];
Allocating memory for an object • Use alloc method inherited from NSObject • Google NSObject, look up alloc method • +(id) alloc; • + static method [Complex alloc] • (id) returns a new instance of the class, here Complex • Complex *z = [Complex alloc];
Allocating memory for an object // Declare a Complex object Complex *z1; // Allocate memory for it z1 = [Complex alloc]; // alloc returns a memory address // Combine declaration and allocation Complex *z2 = [Complex alloc];
Sending a message i.e. Calling a method Instead of an object calling a method, the terminology of objective-C is that we “send a message” to an object For a method that does not take any parameter, the syntax is: [ObjectName methodName]; // So to send a print message to a Complex object z [z print];
Sending a message i.e. Calling a method // Declare a Complex object reference Complex *z; // Allocate memory for it z = [Complex alloc]; // default values for real and imaginary are 0 and 0 // Send a print message to a Complex object z [z print]; // 0 + 0 i
Sending a message i.e. Calling a method • If the method takes 1 parameter, like setReal, the syntax is: [ObjectName methodName : argument]; • So, to send a setReal message with argument 5 [z setReal : 5];
Sending a message i.e. Calling a method Complex *z; z = [Complex alloc]; [z print]; // 0 + 0 i [z setReal: 5]; [z print]; // 5 + 0 I
Constructors • The default constructor, init, is inherited from NSObject • Can and should be called using [super init] • Can implement specialized init methods in our subclasses
Constructors • Inside .h file • -(Complex *) initWithReal: (int) newReal andImaginary: (int) newImaginary;
Inside .m file -(Complex *) initWithReal: (int) newReal andImaginary: (int) newImaginary { // self is equivalent to this in C++ and Java // call init from NSObject // -(id) init; self = [super init]; // initialize self here }
Inside .m file -(Complex *) initWithReal: (int) newReal andImaginary: (int) newImaginary { self = [super init]; if( self ) { // initialize self, i.e. its instance variables } return self; // could be nil if init failed/returned nil }
Inside .m file -(Complex *) initWithReal: (int) newReal andImaginary: (int) newImaginary { self = [super init]; if( self ) { [self setReal: newReal]; [self setImaginary: newImaginary]; } return self; }
Using our class Complex *z2 = [[Complex alloc] initWithReal: 5 andImaginary: 7]; // We can nest method calls - messages above [z2 print]; ….
Using NSLog • We can also use the NSLog function • NSLog behaves like printf • In addition, we can print objects with NSLog • Formatter is %@
Using NSLog • Constant strings have the @ in front of them and look like this • @”some string here” • They are instances of the class NSString • NSLog’s first argument is an NSString // if z is a Complex object NSLog( @”z is equal to %@”, z );