• Save
OOP Chapter 3: Classes, Objects and Methods
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

OOP Chapter 3: Classes, Objects and Methods

on

  • 1,431 views

http://www.youtube.com/watch?v=jCJrnSGzhOc

http://www.youtube.com/watch?v=jCJrnSGzhOc

Statistics

Views

Total Views
1,431
Views on SlideShare
1,400
Embed Views
31

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 31

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

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 3: Classes, Objects and Methods Presentation Transcript

  • 1. Object-Oriented Programming Language Chapter 3 : Classes, Objects, and Methods Atit Patumvan Faculty of Management and Information Sciences Naresuna UniversityMonday, November 21, 2011
  • 2. 2 Contents • What Is an Object, Anyway? • Instances and Methods • An Objective-C Class for Working with Fractions • The @interface Section • The @implementation Section • The program Section • Accessing Instance Variable and Data EncapsulationAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 3. 3 What Is an Object, Anyway? Class Instance/Object create from Car myCar:Car create from yourCar:CarAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 4. 4 Instances and Methods [ ClassOrInstance method ]; yourCar = [Car new]; message yourCar:Car receiver Sender [yourCar drive]; [yourCar getGas: 30]; currentMileage = [yourCar odometer];Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 5. 5 An Objective-C Class for Working with FractionsProgram 3.1 01: #import <Foundation/Foundation.h> 02: 03: int main (int argc, const char * argv[]) { 04: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 05: 06: int numerator = 1; 07: int denominator = 3; 08: 09: NSLog(@"The fraction is %i/%i", numerator, denominator); 10: [pool drain]; 11: return 0; 12: } :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 6. 6 The @interface SectionProgram 3.2 : 03: //---- @interface section ---- Fraction 04: 05: @interface Fraction: NSObject numerator:int 06: { 07: int numerator; denominator:int 08: int denominator; 09: } print:void 10: setNumerator(int):void 11: -(void) print; setDenominator(int):void 12: -(void) setNumerator: (int) n; 13: -(void) setDenominator: (int) d; 14: 15: @end :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 7. 7 The @implementation SectionProgram 3.2 : 18: Fraction 19: @implementation Fraction 20: 21: -(void) print numerator:int 22: { denominator:int 23: NSLog (@"%i/%i", numerator, denominator); 24: } print:void 25: setNumerator(int):void 26: -(void) setNumerator: (int) n; 27: { setDenominator(int):void 28: numerator = n; 29: } 30: 31: -(void) setDenominator: (int) d; 32: { 33: denominator = d; 34: } 35: 36: @end :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 8. 8 The program SectionProgram 3.2 : 40: int main (int argc, const char * argv[]) { 41: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 42: 43: // insert code here... 44: Fraction * myFraction; Fraction 45: 46: myFraction = [Fraction alloc]; numerator:int 47: myFraction = [myFraction init]; 48: denominator:int 49: Set fraction to 1/3; // 50: print:void 51: [myFraction setNumerator: 1]; setNumerator(int):void 52: [myFraction setDenominator: 3]; setDenominator(int):void 53: 54: Display the fraction using the print method // 55: 56: NSLog(@"The value of myFraction is:"); 57: [myFraction print]; myFraction:Fraction 58: 59: [myFraction release]; 60: 61: [pool drain]; 62: return 0; 63: }Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 9. 9 Accessing Instance Variables and Data EncapsulationProgram 3.4 01: @interface Fraction: NSObject Fraction : 10: -(int) numerator; 11: -(int) denominator; numerator:int 12: denominator:int 13: @end : print():void 17: @implementation Fraction setNumerator(int):void 18: : setDenominator(int):void 34: -(int) numerator numerator():int 35: { denominator():int 36: return numerator; 37: } 38: 39: -(int) denominator 40: { 41: return denominator; 42: } 43: 44: @end :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011
  • 10. 10 Accessing Instance Variables and Data EncapsulationProgram 3.4 : 52: int main (int argc, const char * argv[]) { 53: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 54: 55: // insert code here... 56: Fraction * myFraction = [[Fraction alloc] init]; Fraction 57: 58: Set fraction to 1/3; // 59: numerator:int 60: [myFraction setNumerator: 1]; denominator:int 61: [myFraction setDenominator: 3]; 62: print():void 63: Display the fraction using our two new methods // setNumerator(int):void 64: setDenominator(int):void 65: NSLog(@"The value of myFraction is:%i/%i", numerator():int 66: [myFraction numerator], 67: [myFraction denominator]); denominator():int 68: 69: [myFraction release]; 70: 71: [pool drain]; 72: return 0; 73: }Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, November 21, 2011