1 / 40

Object Oriented Programming

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)

clio
Download Presentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming Similar concepts as in other programming languages Different syntax

  2. 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

  3. 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

  4. 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

  5. Header file #import <Foundation/Foundation.h> // Other import (preprocessor) directives @interface ClassName:NSObject { // Instance variable declarations } // Method declarations @end

  6. Import other classes you need #import <Foundation/Foundation.h> // Other import (preprocessor) directives @interface ClassName:NSObject { // Instance variable declarations } // Method declarations @end

  7. 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)

  8. Class code goes between @interface and @end #import <Foundation/Foundation.h> @interface ClassName:NSObject { // Instance variable declarations } // Method declarations @end

  9. 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”)

  10. Complex is the name of the class; it inherits from NSObject #import <Foundation/Foundation.h> @interface Complex:NSObject { // Instance variable declarations } // Method declarations @end

  11. Instance variable declarations go between the curly braces #import <Foundation/Foundation.h> @interface Complex:NSObject { int real; int imaginary; } // Method declarations @end

  12. 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)

  13. Method declaration syntax // 0 parameter ( returnType ) methodName; // Examples - ( void ) print; - ( int ) getReal; - ( Complex * ) multiplyBy2;

  14. 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

  15. 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;

  16. - Instance method #import <Foundation/Foundation.h> @interface Complex:NSObject { int real; int imaginary; } • (void) print; // instance method @end

  17. 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

  18. 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

  19. Need to import the header file #import “Complex.h” @implementation Complex // implementation goes here @end

  20. Code goes between @implementation and @end #import “Complex.h” @implementation Complex /* Note: superclass (NSObject) is not specified above */ // implementation goes here @end

  21. Code goes between @implementation and @end #import “Complex.h” @implementation Complex • ( void ) print { printf( “%i + %i i”, real, imaginary ); } // setReal method implemented here @end

  22. Code goes between @implementation and @end #import “Complex.h” @implementation Complex // print method implemented here • ( void ) setReal: (int ) r { real = r; } @end

  23. 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

  24. Declare an object of a class // Declare a Complex object // Syntax uses pointer notation (*) // objectName is a pointer of type ClassName ClassName *objectName;

  25. Declare an object of the Complex class // Declare 2 Complex object references Complex *z1; Complex *z2; // z1 and z2 are pointers to Complex objects

  26. Using a method • “Calling a method” = “Sending a message” • Smalltalk syntax • Static void method • [ClassName methodName]; • Instance void method • [objectName methodName];

  27. 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];

  28. 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];

  29. 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];

  30. 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

  31. 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];

  32. 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

  33. 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

  34. Constructors • Inside .h file • -(Complex *) initWithReal: (int) newReal andImaginary: (int) newImaginary;

  35. 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 }

  36. 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 }

  37. Inside .m file -(Complex *) initWithReal: (int) newReal andImaginary: (int) newImaginary { self = [super init]; if( self ) { [self setReal: newReal]; [self setImaginary: newImaginary]; } return self; }

  38. Using our class Complex *z2 = [[Complex alloc] initWithReal: 5 andImaginary: 7]; // We can nest method calls - messages above [z2 print]; ….

  39. Using NSLog • We can also use the NSLog function • NSLog behaves like printf • In addition, we can print objects with NSLog • Formatter is %@

  40. 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 );

More Related