• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
OOP Chapter 7 : More on Classes
 

OOP Chapter 7 : More on Classes

on

  • 670 views

objective-c,oop

objective-c,oop

Statistics

Views

Total Views
670
Views on SlideShare
537
Embed Views
133

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 133

http://www.atit.patumvan.com 133

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    OOP Chapter 7 : More on Classes OOP Chapter 7 : More on Classes Presentation Transcript

    • Object-Oriented Programming Language Chapter 7 : More on Classes Atit Patumvan Faculty of Management and Information Sciences Naresuan Universityวันจันทร์ท่ี 26 มีนาคม 12
    • 2 Separate Interface and Implementation files Single Source File Separate Source File SourceCode_A.m SourceCode_A.h Interface Code Interface Code SourceCode_A.m Implementation Code Implementation Code Program Code SourceCode_B.m Program Code Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 3 Separate Interface and Implementation files Fraction.h #import <Foundation/Foundation.h> @interface Fraction : NSObject : @end Fraction.m #import "Fraction.h" @implementation Fraction : @end main.m #import <Foundation/Foundation.h> #import "Fraction.h" int main (int argc, const char * argv[]){ ... Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 4 Separate Interface and Implementation FilesProgram 7.1 main.m01: #import <Foundation/Foundation.h>02: #import "Fraction.h"03:04: int main (int argc, char * argv[])05: {06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07:! Fraction * myFraction = [[Fraction alloc] init];08:09:! [myFraction setNumerator: 1];10:! [myFraction setDenominator: 3];11:12:! // display the fraction13:14:! NSLog (@"The value of myFraction is:");15:! [myFraction print];16:! [myFraction release];17:18:! [pool drain];19:! return 0;20: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 5 Separate Interface and Implementation FilesProgram 7.1 Fraction.h01: #import <Foundation/Foundation.h>02:03: // The Fraction class04:05: @interface Fraction : NSObject06: {07:! int ! numerator;08:! int ! denominator;09: }10:11: -(void) print;12: -(void) setNumerator: (int) n;13: -(void) setDenominator: (int) d;14: -(int)!numerator;15: -(int)!denominator;16: -(double) convertToNum;17:18: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 6 Separate Interface and Implementation FilesProgram 7.1 Fraction.m01: #import "Fraction.h" 25: -(double) convertToNum02: 26: {03: @implementation Fraction 27:! if(denominator != 0)04: 28:! ! return (double) numerator / denominator;05: -(void) print 29:! else06: { 30:! ! return NAN;07:! NSLog(@"%i/%i", numerator, denominator); 31: }08: } 32: @end09: -(void) setNumerator: (int) n10: {11:! numerator = n;12: }13: -(void) setDenominator: (int) d14: {15:! denominator = d;16: }17: -(int)! numerator18: {19:! return numerator;20: }21: -(int)! denominator22: {23:! return denominator;24: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 7 Compiling Program from the Command line gcc -framwork Foundation Fraction.m main.m -o main Using GNUMakefile 01: include $(GNUSTEP_MAKEFILES)/common.make 02: 03: TOOL_NAME = main 04: main_HEADERS = Fraction.h 05: main_OBJC_FILES = main.m Fraction.m 06: main_RESOURCE_FILES = 07: 08: include $(GNUSTEP_MAKEFILES)/tool.make Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 8 Synthesize Accessor MethodsProgram 7.1b Fraction.h Program 7.1b Fraction.m01: #import <Foundation/Foundation.h> 01: #import "Fraction.h"02: 02:03: // The Fraction class 03: @implementation Fraction04: 04:05: @interface Fraction : NSObject 05: @synthesize numerator, denominator;06: { 06:07:! int ! numerator; 07: -(void) print08:! int ! denominator; 08: {09: } 09:! NSLog(@"%i/%i", numerator, denominator);10: 10: }11: @property int numerator, denominator; 11: -(double) convertToNum12: 12: {13: -(void) print; 13:! if(denominator != 0)14: -(void) convertToNum; 14:! ! return (double) numerator / denominator;15: 15:! else16: @end 16:! ! return NAN; 17: } 18: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 9 Accessing Properties Using Dot Operator Dot Operator [myFraction numerator] myFraction.numerator instance property instance property [myFraction setNumerator: 3] myFraction.numerator = 3 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 10 Accessing Properties Using Dot OperatorProgram 7.1c main.m01: #import <Foundation/Foundation.h>02: #import "Fraction.h"03:04: int main (int argc, char * argv[])05: {06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07:! Fraction * myFraction = [[Fraction alloc] init];08:09:! //[myFraction setNumerator: 1];10:! myFraction.numerator = 1;11:! //[myFraction setDenominator: 3];12:! myFraction.denominator = 3;13:! // display the fraction14:15:! NSLog (@"The value of myFraction is:");16:! [myFraction print];17:! [myFraction release];18:19:! [pool drain];20:! return 0;21: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 11 Multiple Argument to Methods [myFraction setNumerator: 1]; [myFraction setDenominator: 3]; [myFraction setNumerator: 1 andDenominator: 3]; [myFraction setTo: 1 over: 3]; Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 12 Multiple Argument to MethodsProgram 7.2 Fraction.h01: #import <Foundation/Foundation.h>02:03: // The Fraction class04:05: @interface Fraction : NSObject06: {07:! int ! numerator;08:! int ! denominator;09: }10:11: @property int numerator, denominator;12:13: -(void) print;14: -(void) setTo: (int) n over: (int) d;15: -(double) convertToNum;16:17: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 13 Multiple Argument to MethodsProgram 7.2 Fraction.m Program 7.2 main.m01: #import "Fraction.h" 01: import <Foundation/Foundation.h>02: 02: #import "Fraction.h"03: @implementation Fraction 03:04: 04: int main (int argc, char * argv[])05: @synthesize numerator, denominator; 05: {06: 06:! NSAutoreleasePool * pool =07: -(void) print [[NSAutoreleasePool alloc] init];08: { 07:! Fraction * aFraction =09:! NSLog(@"%i/%i", numerator, denominator); [[Fraction alloc] init];10: } 08:11: -(double) convertToNum 09:! [aFraction setTo: 100 over: 200];12: { 10:! [aFraction print];13:! if(denominator != 0) 11:14:! ! return (double) numerator / denominator; 12:! [aFraction setTo: 1 over: 3];15:! else 13:! [aFraction print];16:! ! return NAN; 14:17: } 15:! aFraction.setNumerator= 1;18: -(void) setTo: (int) n over: (int) d 16:! aFraction.setDenominator= 7;19: { 17:! [aFraction print];20:! numerator = n; 18:21:! denominator = d;! 19:! [aFraction release];22: } 20:23: @end 21:! [pool drain]; 22:! return 0; 23: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 14 Operations on Fractions _ 4 6 1 _ _ + = 1 4 8 Program 7.3 Fraction.h : 16: -(void) add: (Fraction * ) f; : Program 7.3 Fraction.h : 21: -(void) add: (Fraction * ) f 22: { 23:! numerator = numerator * f.denominator + denominator * f.numerator; 24:! denominator = denominator * f.denominator; 25: 26: } : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 15 Operations on FractionsProgram 7.3 main.m01: #import <Foundation/Foundation.h>02: #import "Fraction.h"03:04: int main (int argc, char * argv[])05: {06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07:! Fraction * aFraction = [[Fraction alloc] init];08:! Fraction * bFraction = [[Fraction alloc] init];09:10:! [aFraction setTo: 1 over: 4];11:! [bFraction setTo: 1 over: 2];12:13:! [aFraction print];14:! NSLog(@"+");15:! [bFraction print];16:! NSLog(@"=");17:18:! [aFraction add: bFraction];19:! [aFraction print];20:! [aFraction release];21:! [bFraction release];22:23: ! return 0;24: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 16 Local Variables Program 7.4 Fraction.m : 21: -(void) reduce 22: { 23:! int u = numerator; 24:! int v = denominator; 25:! int temp; 26:! 27:! while ( v != 0 ){ 28:! ! temp = u % v; 29:! ! u = v; 30:! ! v = temp; 31:! } 32:! numerator /= u; 33:! denominator /= u; 34: } : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 17 Local VariablesProgram 7.4 main.m01: #import <Foundation/Foundation.h>02: #import "Fraction.h"03:04: int main (int argc, char * argv[])05: {06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07:! Fraction * aFraction = [[Fraction alloc] init];08:! Fraction * bFraction = [[Fraction alloc] init];09:10:! [aFraction setTo: 1 over: 4];11:! [bFraction setTo: 1 over: 2];12:13:! [aFraction print];14:! NSLog(@"+");15:! [bFraction print];16:! NSLog(@"=");17:18:! [aFraction add: bFraction];19:! [aFraction reduce];20:21:! [aFraction print];22:! [aFraction release];23:! [bFraction release];24: ! return 0;25: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 18 The self Keyword [self reduce;] Program 7.4a Fraction.m : 21: -(void) add: (Fraction * ) f 22: { 23:! numerator = numerator * f.denominator + denominator * f.numerator; 24:! denominator = denominator * f.denominator; 25: 26: [self reduce]; 26: } : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 19 The static Keyword -(int) showPage { static int pageCount = 0; : ++pageCount; : return pageCount; } #import “Printer.h” static int pageCount; @implementation Printer : @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 20 Allocating and Return Objects from Methods Program 7.5 Fraction.h : 16: -(Fraction) add: (Fraction * ) f; : Program 7.5 Fraction.h : 21: -(void) add: (Fraction * ) f 22: { 23:! numerator = numerator * f.denominator + denominator * f.numerator; 24:! denominator = denominator * f.denominator; 25: 26: [self reduce]; 26: } : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 21 Allocating and Return Objects from MethodsProgram 7.5 main.m01: #import <Foundation/Foundation.h>02: #import "Fraction.h"03:04: int main (int argc, char * argv[])05: {06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07:! Fraction * aFraction = [[Fraction alloc] init];08:! Fraction * bFraction = [[Fraction alloc] init];09:10:! [aFraction setTo: 1 over: 4];11:! [bFraction setTo: 1 over: 2];12:13:! [aFraction print];14:! NSLog(@"+");15:! [bFraction print];16:! NSLog(@"=");17:18:! [[aFraction add: bFraction] print];19:20:! [aFraction release];21:! [bFraction release];22:! [pool drain];23:! return 0;24: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 22 Allocating and Return Objects from MethodsProgram 7.6 main.m01: #import <Foundation/Foundation.h>02: #import "Fraction.h"03:04: int main (int argc, char * argv[])05: {06:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];07:! Fraction * aFraction = [[Fraction alloc] init];08:! Fraction * sum = [[Fraction alloc] init], * sum2;09:10:! int i, n, pow2;11:12:! [sum setTo: 0 over: 1];13:14:! NSLog(@"Enter you value for n:" );15:! scanf("%i", &n);16:!17:! pow2 = 2;18:! for(i = 1; i<=n; ++i){19:! ! [aFraction setTo: 1 over: pow2];20:! ! sum2 = [sum add: aFraction];21:! ! [sum release];22:! ! sum = sum2;23:! ! pow2 *= 2;24:! }25:! Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12
    • 23 Allocating and Return Objects from MethodsProgram 7.6 main.m26:! NSLog(@"After %i iterations, the sum is %g", n , [sum convertToNum]);27:28:! [aFraction release];29:! [sum release];30:!31:! [pool drain];32:! return 0;33: } 2012-03-26 11:07:12.238 main[3496] Enter you value for n: 5 2012-03-26 11:07:14.261 main[3496] After 5 iterations, the sum is 0.96875 2012-03-26 11:14:24.890 main[3672] Enter you value for n: 10 2012-03-26 11:14:35.645 main[3672] After 10 iterations, the sum is 0.999023 2012-03-26 11:14:39.991 main[2184] Enter you value for n: 15 2012-03-26 11:14:41.604 main[2184] After 15 iterations, the sum is 0.999969 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Languageวันจันทร์ท่ี 26 มีนาคม 12