SlideShare a Scribd company logo
Object-Oriented Programming Language
                            Chapter 3 : Classes, Objects, and Methods


                                             Atit Patumvan
                             Faculty of Management and Information Sciences
                                           Naresuna University




Monday, November 21, 2011
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 Encapsulation




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University              Object-Oriented Programming Language
Monday, November 21, 2011
3



                                                       What Is an Object, Anyway?




                  Class                                                                             Instance/Object
                                                                                      create from
                                       Car                                                            myCar:Car


                                                                                     create from
                                                                                                     yourCar:Car



Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                              Object-Oriented Programming Language
Monday, November 21, 2011
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 Language
Monday, November 21, 2011
5



                  An Objective-C Class for Working with Fractions

Program 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 Language
Monday, November 21, 2011
6



                                                                The @interface Section

Program 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 Language
Monday, November 21, 2011
7



                                                   The @implementation Section

Program 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 Language
Monday, November 21, 2011
8



                                                                    The program Section

Program 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 Language
Monday, November 21, 2011
9



        Accessing Instance Variables and Data Encapsulation

Program 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 Language
Monday, November 21, 2011
10



        Accessing Instance Variables and Data Encapsulation

Program 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 Language
Monday, November 21, 2011

More Related Content

PDF
OOP Chapter 4: Data Type and Expressions
PDF
OOP: Chapter 2: Programming in Objective-C
PDF
OOP Chapter 7 : More on Classes
PDF
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
PDF
OOP Chapter 6: Making Decisions
PDF
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
PDF
Python Application: Visual Approach of Hopfield Discrete Method for Hiragana ...
ODT
Object Oriented Approach Within Siebel Boundaries
OOP Chapter 4: Data Type and Expressions
OOP: Chapter 2: Programming in Objective-C
OOP Chapter 7 : More on Classes
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
OOP Chapter 6: Making Decisions
Multi-dimensional exploration of API usage - ICPC13 - 21-05-13
Python Application: Visual Approach of Hopfield Discrete Method for Hiragana ...
Object Oriented Approach Within Siebel Boundaries

What's hot (14)

PDF
Object-oriented Programming in Python
PDF
Writing Usable APIs in Practice by Giovanni Asproni
PDF
Producing simulation sequences by use of a Java-based Framework
PDF
Design and implementation of a java based virtual laboratory for data communi...
PDF
turecko-150426_pse_01
DOCX
Case study how pointer plays very important role in data structure
PPTX
Object oriented programming in python
PDF
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
DOC
Unit 8
PPTX
Event-driven Model Transformations in Domain-specific Modeling Languages
PDF
Animations On PDF Using Lua and LaTeX
PDF
Extractive Summarization with Very Deep Pretrained Language Model
PPT
pointer, structure ,union and intro to file handling
PDF
Data Structures and Algorithms
Object-oriented Programming in Python
Writing Usable APIs in Practice by Giovanni Asproni
Producing simulation sequences by use of a Java-based Framework
Design and implementation of a java based virtual laboratory for data communi...
turecko-150426_pse_01
Case study how pointer plays very important role in data structure
Object oriented programming in python
A Logic Meta-Programming Foundation for Example-Driven Pattern Detection in O...
Unit 8
Event-driven Model Transformations in Domain-specific Modeling Languages
Animations On PDF Using Lua and LaTeX
Extractive Summarization with Very Deep Pretrained Language Model
pointer, structure ,union and intro to file handling
Data Structures and Algorithms
Ad

Similar to OOP Chapter 3: Classes, Objects and Methods (20)

PDF
오브젝트C(pdf)
PDF
OOP Chapter 8 : Inheritance
PDF
Lecture 03
PPTX
OOPS features using Objective C
PDF
iOS Hackathon 2012 Objective-C talk
PPTX
Presentation 1st
PDF
201005 accelerometer and core Location
PDF
The messy lecture
PDF
Objective-C A Beginner's Dive
ZIP
PDF
Beginningi os part1-bobmccune
PDF
iOS Programming Intro
PPT
PDF
Louis Loizides iOS Programming Introduction
PPTX
Classes, objects in JAVA
PDF
Irving iOS Jumpstart Meetup - Objective-C Session 1b
PDF
PDF
Objective-C
PDF
Iphone course 1
오브젝트C(pdf)
OOP Chapter 8 : Inheritance
Lecture 03
OOPS features using Objective C
iOS Hackathon 2012 Objective-C talk
Presentation 1st
201005 accelerometer and core Location
The messy lecture
Objective-C A Beginner's Dive
Beginningi os part1-bobmccune
iOS Programming Intro
Louis Loizides iOS Programming Introduction
Classes, objects in JAVA
Irving iOS Jumpstart Meetup - Objective-C Session 1b
Objective-C
Iphone course 1
Ad

More from Atit Patumvan (20)

PDF
Iot for smart agriculture
PDF
An Overview of eZee Burrp! (Philus Limited)
PDF
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
PDF
Chapter 1 mathmatics tools
PDF
Chapter 1 mathmatics tools
PDF
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
PDF
Chapter 0 introduction to theory of computation
PPTX
Media literacy
PDF
Chapter 01 mathmatics tools (slide)
PDF
การบริหารเชิงคุณภาพ ชุดที่ 8
PDF
การบริหารเชิงคุณภาพ ชุดที่ 7
PDF
การบริหารเชิงคุณภาพ ชุดที่ 6
PDF
Computer Programming Chapter 5 : Methods
PDF
Computer Programming Chapter 4 : Loops
PDF
Introduction to Java EE (J2EE)
PDF
การบริหารเชิงคุณภาพ ชุดที่ 5
PDF
การบริหารเชิงคุณภาพ ชุดที่ 4
PDF
การบริหารเชิงคุณภาพ ชุดที่ 3
KEY
การบริหารเชิงคุณภาพ ชุดที่ 2
PDF
Computer Programming: Chapter 1
Iot for smart agriculture
An Overview of eZee Burrp! (Philus Limited)
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
Chapter 1 mathmatics tools
Chapter 1 mathmatics tools
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
Chapter 0 introduction to theory of computation
Media literacy
Chapter 01 mathmatics tools (slide)
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 6
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 4 : Loops
Introduction to Java EE (J2EE)
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 2
Computer Programming: Chapter 1

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
Cell Structure & Organelles in detailed.
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
Pharma ospi slides which help in ospi learning
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Anesthesia in Laparoscopic Surgery in India
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Open Quiz Monsoon Mind Game Prelims.pptx
Renaissance Architecture: A Journey from Faith to Humanism
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
How to Manage Starshipit in Odoo 18 - Odoo Slides
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Cell Structure & Organelles in detailed.
UPPER GASTRO INTESTINAL DISORDER.docx
Open Quiz Monsoon Mind Game Final Set.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
Pharma ospi slides which help in ospi learning

OOP Chapter 3: Classes, Objects and Methods

  • 1. Object-Oriented Programming Language Chapter 3 : Classes, Objects, and Methods Atit Patumvan Faculty of Management and Information Sciences Naresuna University Monday, 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 Encapsulation Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, November 21, 2011
  • 3. 3 What Is an Object, Anyway? Class Instance/Object create from Car myCar:Car create from yourCar:Car Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, 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 Language Monday, November 21, 2011
  • 5. 5 An Objective-C Class for Working with Fractions Program 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 Language Monday, November 21, 2011
  • 6. 6 The @interface Section Program 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 Language Monday, November 21, 2011
  • 7. 7 The @implementation Section Program 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 Language Monday, November 21, 2011
  • 8. 8 The program Section Program 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 Language Monday, November 21, 2011
  • 9. 9 Accessing Instance Variables and Data Encapsulation Program 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 Language Monday, November 21, 2011
  • 10. 10 Accessing Instance Variables and Data Encapsulation Program 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 Language Monday, November 21, 2011