SlideShare a Scribd company logo
1 of 18
Download to read offline
Object-Oriented Programming Language
                                            Chapter 8 : Inheritance


                          Atit Patumvan
          Faculty of Management and Information Sciences
                        Naresuan University
2



                                                               It All Begins at the Root



        @interface Fraction: NSObject
         :
        @end



                                                                          NSObject
                                                                                       root class or superclass




                                                                            Fraction
                                                                                       subclass




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



                                                    Subclasses and superclasses

                                                                                                  @interface ClassA: NSObject
                                                                                                  {
                                                                                                  !   int x;
                                          NSObject                                                }

                                                                                                  -(void) initVar;
                                                                                                  @end
    subclass                                                                         superclass   @implementation ClassA
                                                                                                  -(void) initVar
                                                                                                  {
                                                                                                  !    x = 100;
                                             ClassA                                               }
                                                                                                  @end


                                                                                                  @interface ClassB: ClassA
    subclass                                                                         superclass   -(void) printVar;
                                                                                                  @end

                                                                                                  @implementation ClassB
                                                                                                  -(void) printVar
                                             ClassB                                               {
                                                                                                  !    NSLog(@"x= %i", x);
                                                                                                  }
                                                                                                  @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                            Object-Oriented Programming Language
4



                                                    Subclasses and superclasses

    @interface ClassA: NSObject
    {
    !   int x;
                                                                                      Class     Instance Variables            Methods
    }

    -(void) initVar;                                                                 NSObject
    @end

    @implementation ClassA
    -(void) initVar
    {
    !    x = 100;
    }
    @end                                                                              ClassA           x             initVar

    @interface ClassB: ClassA
    -(void) printVar;
    @end

    @implementation ClassB
    -(void) printVar
    {                                                                                 ClassB           x             intVar            printVar
    !    NSLog(@"x= %i", x);
    }
    @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                       Object-Oriented Programming Language
5



                                                                        Simple Inheritance

Program 8.1 main.m
  :
28: int main(int argc, char *argv[]) {
29:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
30:!
31:! ClassB * b = [[ClassB alloc] init];
32:!
33:! [b initVar];
34:! [b printVar]; // x = 100
35:!
36:! [b release];
                                    @interface ClassA: NSObject @interface ClassB: ClassA
37:!
                                    {                             -(void) printVar;
38:! [pool drain];
                                    !    int x;                   @end
39:! return 0;!
                                    }
40:! }
                                                                  @implementation ClassB
41: }
                                    -(void) initVar;              -(void) printVar
  :
                                    @end                          {
                                                                  !    NSLog(@"x= %i", x);
                                    @implementation ClassA        }
                                    -(void) initVar               @end
                                    {
                                    !    x = 100;
                                    }
                                    @end

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University           Object-Oriented Programming Language
6



        Extension Through Inheritance: Adding New Methods

Program 8.2 Rectangle.h
01: #import <Foundation/Foundation.h>
02:
03: @interface Rectangle: NSObject
04: {
05:! int width;
06:! int height;
07: }
08:
09: @property int width, height;
10:
11: -(void) setWidth: (int) w andHeight: (int) h;                    Program 8.2 Rectangle.m
12: -(int) area;
13: -(int) perimeter;                      01: #import "Rectangle.h"
14:                                        02:
15: @end                                   03: @implementation Rectangle
                                           04:
                                           05: @synthesize width, height;
                                           06:
                                           07: -(void) setWidth: (int) w andHeight: (int) h
                                           08: {
                                           09:! width = w;
                                           10:! height = h;
                                           11: }
                                             :
                                           21: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
7



        Extension Through Inheritance: Adding New Methods

Program 8.2 Rectangle.h
01: #import <Foundation/Foundation.h>
02:#import "Rectangle.h"
03:
04: int main(int argc, char *argv[]) {
05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06:! Rectangle * myRect = [[Rectangle alloc] init ];
07:!
08:! [myRect setWidth: 5 andHeight: 8];
09:!
10:! NSLog(@"Rectangle: w = %i, h = %i", myRect.width, myRect.height);
11:! NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);
12:!
13:! [myRect release];
14:! [pool drain];
15:! return 0;
16: }




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
8



                         Extension Through Inheritance: Subclassing

Program 8.3 Square.h                                                                 Program 8.3 main.m
01:    #import <Foundation/Foundation.h>                                             01:   #import <Foundation/Foundation.h>
02:    #import "Rectangle.h"                                                         02:   #import "Square.h"
03:                                                                                  03:
04:    @interface Square : Rectangle                                                 04:   int main (int argc, char * argv[])
05:                                                                                  05:   {
06:    -(void) setSide: (int) s;                                                     06:      NSAutoreleasePool * pool =
07:    -(int) side;                                                                                  [[NSAutoreleasePool alloc] init];
08:    @end                                                                          07:
                                                                                     08:         Square * mySquare = [[Square alloc] init];
Program 8.3 Square.m                                                                 09:
                                                                                     10:         [mySquare setSide: 5];
01: #import "Square.h"                                                               11:
02:                                                                                  12:         NSLog(@"Square s = %i", [mySquare side]);
03: @implementation Square: Rectangle                                                13:         NSLog(@"Area = %i, Perimeter = %i",
04:                                                                                                  [mySquare area], [mySquare perimeter]);
05: -(void) setSide: (int) s                                                         14:
06: {                                                                                15:         [pool drain];
07:! [self setWidth: s andHeight: s];                                                16:      return 0;
08: }                                                                                17:
09:                                                                                  18: }
10: -(int) side
11: {
12:! return width;!
13: }
14: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                             Object-Oriented Programming Language
9



                                      A Point Class and Memory Allocation

                                                                                     (x,y)



      y                                                     myRect
                                              (x1,y1)


      (0,0)                                                     x
Program 8.4 XYPoint.h                                                                        Program 8.4 Rectangle.h
01:    #import <Foundation/Foundation.h>                                                       :
02:                                                                                          05: @interface Rectangle: NSObject
03:    @interface XYPoint: NSObject                                                          06: {
04:    {                                                                                     07:! int width;
05:       int x;                                                                             08:! int height;
06:       int y;                                                                             09: XYPoint * origin;
07:    }                                                                                     10: }
08:    @property int x, y;                                                                   11:
09:                                                                                            :
10:    -(void) setX: (int) xVal andY: (int) yVal;                                            13: -(XYPoint *) origin;
11:    @end                                                                                    :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                     Object-Oriented Programming Language
10



                                                                    The @class directive

Program 8.4 Rectangle.h
01: #import <Foundation/Foundation.h>
02:
03: @class XYPoint;
04:
                                                                                     @class directive
05: @interface Rectangle: NSObject
06: {
07:! int width;
08:! int height;
09: XYPoint * origin;
10: }
11:
12: @property int width, height;
13: -(XYPoint *) origin;
14: -(void) setOrigin: (XYPoint *) pt;
15: -(void) setWidth: (int) w andHeight: (int) h;
16: -(int) area;
17: -(int) perimeter;
18:
19: @end




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                      Object-Oriented Programming Language
11



                                                         Handling Object in Method

Program 8.4 Rectangle.h                                                              Program 8.4 Rectangle.m
01: #import <Foundation/Foundation.h>                                                01:   #import "Rectangle.h"
02:                                                                                  02:
03: @class XYPoint;                                                                  03:   @implementation Rectangle
04:                                                                                  04:
05: @interface Rectangle: NSObject                                                   05:   @synthesize width, height;
06: {                                                                                06:
07:! int width;                                                                      07:   -(XYPoint *) origin
08:! int height;                                                                     08:   {
09: XYPoint * origin;                                                                09:      return origin;
10: }                                                                                10:   }
11:                                                                                  11:   -(void) setOrigin: (XYPoint *) pt
12: @property int width, height;                                                     12:   {
13: -(XYPoint *) origin;                                                             13:     origin = pt;
14: -(void) setOrigin: (XYPoint *) pt;                                               14:   }
15: -(void) setWidth: (int) w andHeight: (int) h;                                      :
16: -(int) area;
17: -(int) perimeter;
18:
19: @end




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                 Object-Oriented Programming Language
12



                                                         Handling Object in Method

Program 8.4 main.m
01: #import <Foundation/Foundation.h>
02: #import "Rectangle.h"
03: #import "XYPoint.h"
  :
09:    Rectangle * myRect = [[Rectangle alloc] init];
10:    XYPoint * myPoint = [[XYPoint alloc] init];
11:
12:    [myPoint setX: 100 andY: 200];
13:
14:    [myRect setWidth: 5 andHeight: 8];
15:    myRect.origin = myPoint;
16:
17:    NSLog(@"Rectangle w = %i, h = %i", myRect.width, myRect.height);
18:
19:    NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);
20:
21:    NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);
22:
23:    [myRect release];
24:    [myPoint release];
  :




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
13



                      Can you explain the output from Program 7.5?

Program 8.5 main.m
  :
09:           Rectangle * myRect = [[Rectangle alloc] init];
10:           XYPoint * myPoint = [[XYPoint alloc] init];
11:
12:           [myPoint setX: 100 andY: 200];
13:
14:           [myRect setWidth: 5 andHeight: 8];
15:           myRect.origin = myPoint;
16:
17:           NSLog(@"Origin at                           (%i, %i)", myRect.origin.x, myRect.origin.y);
18:
19:           [myPoint setX: 50 andY: 50];
20:
21:           NSLog(@"Origin at                           (%i, %i)", myRect.origin.x, myRect.origin.y);
22:
23:           [myRect release];
24:           [myPoint release];                                                                   myPoint
25:
26:           [pool drain];
27:           return 0;                                                myRect         width = 5                          x = 100
  :                                                                                   height = 8                         y = 200
                                                                                     origin                              XYPoint@yyyy
                                                                                                        Rectangle@xxxx

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                 Object-Oriented Programming Language
14



                                                           Fixing Reference Problem

Program 8.5B Rectangle.m                                                                               Program 8.5B main.m
  :                                                                                                      :
12: -(void) setOrigin: (XYPoint *) pt                                                                  23:    [[myRect origin] release];
13: {                                                                                                  24:    [myRect release];
14:    if (! origin)                                                                                   25:    [myPoint release];
15:        origin = [[XYPoint alloc] init];                                                              :
16:
17:    origin.x = pt.x;
18:    origin.y = pt.y;
19: }
  :

                                                                                             myPoint


                           myRect                                               width = 5                       x = 100
                                                                                height = 8                      y = 200
                                                                               origin                           XYPoint@yyyy              pt

                                                                              Rectangle@xxxx
                                                                                                                x = 100
                                                                                                                y = 200            copy

                                                                                                                XYPoint@zzzz

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                              Object-Oriented Programming Language
15



                                                                      Overriding Methods

Program 8.6 main.m
  :                                                                                    :
07:    @interface ClassA : NSObject {                                                22:   @interface ClassB : ClassA
08:       int x;                                                                     23:   -(void) initVar;
09:    }                                                                             24:   -(void) printVar;
10:                                                                                  25:   @end
11:    -(void) initVar;                                                              26:
12:    @end                                                                          27:   @implementation ClassB
13:                                                                                  28:   -(void) initVar
14:    @implementation ClassA                                                        29:   {
15:    -(void) initVar                                                               30:      x = 200;
16:    {                                                                             31:   }
17:       x = 100;                                                                   32:   -(void) printVar
18:    }                                                                             33:   {
19:                                                                                  34:      NSLog(@"x = %i", x);
20:    @end                                                                          35:   }
  :                                                                                  36:   @end
                                                                                       :
  :
41:           ClassB * b = [[ClassB alloc] init];
42:
43:           [b initVar];                         // uses overrinding method in B
44:
45:           [b printVar];                        // reveal value of x;
46:           [b release];
  :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                      Object-Oriented Programming Language
16



                 Overriding Methods: Which method is Selected?

Program 8.7 main.m (old)                                                             Program 8.7 main.m (new)
  :
07:    @interface ClassA : NSObject {                                                  :
08:       int x;                                                                     12:   -(void) printVar;
09:    }                                                                             13:   @end
10:                                                                                  14:
11:    -(void) initVar;                                                              15:   @implementation ClassA
12:    @end                                                                          16:   -(void) initVar
13:                                                                                  17:   {
14:    @implementation ClassA                                                        18:      x = 100;
15:    -(void) initVar                                                               19:   }
16:    {                                                                             20:   -(void) printVar
17:       x = 100;                                                                   21:
18:    }                                                                             22:       NSLog(@"x = %i", x);
19:                                                                                  23:   }
20:    @end                                                                            :
  :
  :
42:          ClassA * a = [[ClassA alloc] init];
43:          ClassB * b = [[ClassB alloc] init];
44:         [a initVar];    // uses ClassA method
45:         [a printVar];   // reveal value of x
46:
47:         [b initVar];                         // uses overrinding ClassB method
48:         [b printVar];                        // reveal value of x;
  :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                    Object-Oriented Programming Language
17



       Overriding the dealloc Method and the Keyword super

Program 8.5B Rectangle.m                                                                    Program 8.5B main.m
  :                                                                                           :
12: -(void) setOrigin: (XYPoint *) pt                                                       23:    [[myRect origin] release];
13: {                                                                                       24:    [myRect release];
14:    if (! origin)                                                                        25:    [myPoint release];
15:        origin = [[XYPoint alloc] init];                                                   :
16:
17:    origin.x = pt.x;
18:    origin.y = pt.y;
19: }
  :
                                                                                     Text
Program 8.7B Rectangle.m
                                                                                            Program 8.7B main.m
  :
33: -(void) dealloc                                                                           :
34: {                                                                                       23:    [myRect release];
35:    if(origin)                                                                           24:    [myPoint release];
36:        [origin release];                                                                  :
37:    [super dealloc];
38: }
  :
                                                                            overriding dealloc method
                                             keyword super
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                      Object-Oriented Programming Language
18



          Extension Through Inheritance: Adding New Instance Variables

Program 8.8 main.m                                                                          Program 8.8 main.m
  :                                                                                           :
07: @interface ClassA : NSObject {                                                          22:   @interface ClassB : ClassA
08:     int x;                                                                              23:   {
09:}                                                                                        24:      int y;
10:                                                                                         25:   }
11:-(void) initVar;                                                                         26:
12:                                                                                         27:   -(void) initVar;
13:@end                                                                                     28:   -(void) printVar;
14:
15:@implementation ClassA
                                                                                     Text   29:
                                                                                            30:
                                                                                                  @end

16:-(void) initVar                                                                          31:   @implementation ClassB
17:{                                                                                        32:   -(void) initVar
18:     x = 100;                                                                            33:   {
19:}                                                                                        34:      x = 200;
20:@end                                                                                     35:      y = 300;
  :                                                                                         36:   }
                                                                                            37:   -(void) printVar
  :                                                                                         38:   {
48:             ClassB * b = [[ClassB alloc] init];                                         39:      NSLog (@"x = %i", x);
49:                                                                                         40:      NSLog (@"y = %i", y);
50:           [b initVar];                                                                  41:   }
51:           [b printVar];                                                                 42:   @end
  :                                                                                           :


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                      Object-Oriented Programming Language

More Related Content

Viewers also liked

Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPsEssay Corp
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritanceZubair CH
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts246paa
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 

Viewers also liked (20)

Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPs
 
Cv Thomas Faradta
Cv Thomas FaradtaCv Thomas Faradta
Cv Thomas Faradta
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 

More from Atit Patumvan

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

More from Atit Patumvan (20)

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

Recently uploaded

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 

OOP Chapter 8 : Inheritance

  • 1. Object-Oriented Programming Language Chapter 8 : Inheritance Atit Patumvan Faculty of Management and Information Sciences Naresuan University
  • 2. 2 It All Begins at the Root @interface Fraction: NSObject : @end NSObject root class or superclass Fraction subclass Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 3. 3 Subclasses and superclasses @interface ClassA: NSObject { ! int x; NSObject } -(void) initVar; @end subclass superclass @implementation ClassA -(void) initVar { ! x = 100; ClassA } @end @interface ClassB: ClassA subclass superclass -(void) printVar; @end @implementation ClassB -(void) printVar ClassB { ! NSLog(@"x= %i", x); } @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 4. 4 Subclasses and superclasses @interface ClassA: NSObject { ! int x; Class Instance Variables Methods } -(void) initVar; NSObject @end @implementation ClassA -(void) initVar { ! x = 100; } @end ClassA x initVar @interface ClassB: ClassA -(void) printVar; @end @implementation ClassB -(void) printVar { ClassB x intVar printVar ! NSLog(@"x= %i", x); } @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 5. 5 Simple Inheritance Program 8.1 main.m : 28: int main(int argc, char *argv[]) { 29:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 30:! 31:! ClassB * b = [[ClassB alloc] init]; 32:! 33:! [b initVar]; 34:! [b printVar]; // x = 100 35:! 36:! [b release]; @interface ClassA: NSObject @interface ClassB: ClassA 37:! { -(void) printVar; 38:! [pool drain]; ! int x; @end 39:! return 0;! } 40:! } @implementation ClassB 41: } -(void) initVar; -(void) printVar : @end { ! NSLog(@"x= %i", x); @implementation ClassA } -(void) initVar @end { ! x = 100; } @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 6. 6 Extension Through Inheritance: Adding New Methods Program 8.2 Rectangle.h 01: #import <Foundation/Foundation.h> 02: 03: @interface Rectangle: NSObject 04: { 05:! int width; 06:! int height; 07: } 08: 09: @property int width, height; 10: 11: -(void) setWidth: (int) w andHeight: (int) h; Program 8.2 Rectangle.m 12: -(int) area; 13: -(int) perimeter; 01: #import "Rectangle.h" 14: 02: 15: @end 03: @implementation Rectangle 04: 05: @synthesize width, height; 06: 07: -(void) setWidth: (int) w andHeight: (int) h 08: { 09:! width = w; 10:! height = h; 11: } : 21: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 7. 7 Extension Through Inheritance: Adding New Methods Program 8.2 Rectangle.h 01: #import <Foundation/Foundation.h> 02:#import "Rectangle.h" 03: 04: int main(int argc, char *argv[]) { 05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 06:! Rectangle * myRect = [[Rectangle alloc] init ]; 07:! 08:! [myRect setWidth: 5 andHeight: 8]; 09:! 10:! NSLog(@"Rectangle: w = %i, h = %i", myRect.width, myRect.height); 11:! NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]); 12:! 13:! [myRect release]; 14:! [pool drain]; 15:! return 0; 16: } Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 8. 8 Extension Through Inheritance: Subclassing Program 8.3 Square.h Program 8.3 main.m 01: #import <Foundation/Foundation.h> 01: #import <Foundation/Foundation.h> 02: #import "Rectangle.h" 02: #import "Square.h" 03: 03: 04: @interface Square : Rectangle 04: int main (int argc, char * argv[]) 05: 05: { 06: -(void) setSide: (int) s; 06: NSAutoreleasePool * pool = 07: -(int) side; [[NSAutoreleasePool alloc] init]; 08: @end 07: 08: Square * mySquare = [[Square alloc] init]; Program 8.3 Square.m 09: 10: [mySquare setSide: 5]; 01: #import "Square.h" 11: 02: 12: NSLog(@"Square s = %i", [mySquare side]); 03: @implementation Square: Rectangle 13: NSLog(@"Area = %i, Perimeter = %i", 04: [mySquare area], [mySquare perimeter]); 05: -(void) setSide: (int) s 14: 06: { 15: [pool drain]; 07:! [self setWidth: s andHeight: s]; 16: return 0; 08: } 17: 09: 18: } 10: -(int) side 11: { 12:! return width;! 13: } 14: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 9. 9 A Point Class and Memory Allocation (x,y) y myRect (x1,y1) (0,0) x Program 8.4 XYPoint.h Program 8.4 Rectangle.h 01: #import <Foundation/Foundation.h> : 02: 05: @interface Rectangle: NSObject 03: @interface XYPoint: NSObject 06: { 04: { 07:! int width; 05: int x; 08:! int height; 06: int y; 09: XYPoint * origin; 07: } 10: } 08: @property int x, y; 11: 09: : 10: -(void) setX: (int) xVal andY: (int) yVal; 13: -(XYPoint *) origin; 11: @end : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 10. 10 The @class directive Program 8.4 Rectangle.h 01: #import <Foundation/Foundation.h> 02: 03: @class XYPoint; 04: @class directive 05: @interface Rectangle: NSObject 06: { 07:! int width; 08:! int height; 09: XYPoint * origin; 10: } 11: 12: @property int width, height; 13: -(XYPoint *) origin; 14: -(void) setOrigin: (XYPoint *) pt; 15: -(void) setWidth: (int) w andHeight: (int) h; 16: -(int) area; 17: -(int) perimeter; 18: 19: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 11. 11 Handling Object in Method Program 8.4 Rectangle.h Program 8.4 Rectangle.m 01: #import <Foundation/Foundation.h> 01: #import "Rectangle.h" 02: 02: 03: @class XYPoint; 03: @implementation Rectangle 04: 04: 05: @interface Rectangle: NSObject 05: @synthesize width, height; 06: { 06: 07:! int width; 07: -(XYPoint *) origin 08:! int height; 08: { 09: XYPoint * origin; 09: return origin; 10: } 10: } 11: 11: -(void) setOrigin: (XYPoint *) pt 12: @property int width, height; 12: { 13: -(XYPoint *) origin; 13: origin = pt; 14: -(void) setOrigin: (XYPoint *) pt; 14: } 15: -(void) setWidth: (int) w andHeight: (int) h; : 16: -(int) area; 17: -(int) perimeter; 18: 19: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 12. 12 Handling Object in Method Program 8.4 main.m 01: #import <Foundation/Foundation.h> 02: #import "Rectangle.h" 03: #import "XYPoint.h" : 09: Rectangle * myRect = [[Rectangle alloc] init]; 10: XYPoint * myPoint = [[XYPoint alloc] init]; 11: 12: [myPoint setX: 100 andY: 200]; 13: 14: [myRect setWidth: 5 andHeight: 8]; 15: myRect.origin = myPoint; 16: 17: NSLog(@"Rectangle w = %i, h = %i", myRect.width, myRect.height); 18: 19: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y); 20: 21: NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]); 22: 23: [myRect release]; 24: [myPoint release]; : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 13. 13 Can you explain the output from Program 7.5? Program 8.5 main.m : 09: Rectangle * myRect = [[Rectangle alloc] init]; 10: XYPoint * myPoint = [[XYPoint alloc] init]; 11: 12: [myPoint setX: 100 andY: 200]; 13: 14: [myRect setWidth: 5 andHeight: 8]; 15: myRect.origin = myPoint; 16: 17: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y); 18: 19: [myPoint setX: 50 andY: 50]; 20: 21: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y); 22: 23: [myRect release]; 24: [myPoint release]; myPoint 25: 26: [pool drain]; 27: return 0; myRect width = 5 x = 100 : height = 8 y = 200 origin XYPoint@yyyy Rectangle@xxxx Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 14. 14 Fixing Reference Problem Program 8.5B Rectangle.m Program 8.5B main.m : : 12: -(void) setOrigin: (XYPoint *) pt 23: [[myRect origin] release]; 13: { 24: [myRect release]; 14: if (! origin) 25: [myPoint release]; 15: origin = [[XYPoint alloc] init]; : 16: 17: origin.x = pt.x; 18: origin.y = pt.y; 19: } : myPoint myRect width = 5 x = 100 height = 8 y = 200 origin XYPoint@yyyy pt Rectangle@xxxx x = 100 y = 200 copy XYPoint@zzzz Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 15. 15 Overriding Methods Program 8.6 main.m : : 07: @interface ClassA : NSObject { 22: @interface ClassB : ClassA 08: int x; 23: -(void) initVar; 09: } 24: -(void) printVar; 10: 25: @end 11: -(void) initVar; 26: 12: @end 27: @implementation ClassB 13: 28: -(void) initVar 14: @implementation ClassA 29: { 15: -(void) initVar 30: x = 200; 16: { 31: } 17: x = 100; 32: -(void) printVar 18: } 33: { 19: 34: NSLog(@"x = %i", x); 20: @end 35: } : 36: @end : : 41: ClassB * b = [[ClassB alloc] init]; 42: 43: [b initVar]; // uses overrinding method in B 44: 45: [b printVar]; // reveal value of x; 46: [b release]; : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 16. 16 Overriding Methods: Which method is Selected? Program 8.7 main.m (old) Program 8.7 main.m (new) : 07: @interface ClassA : NSObject { : 08: int x; 12: -(void) printVar; 09: } 13: @end 10: 14: 11: -(void) initVar; 15: @implementation ClassA 12: @end 16: -(void) initVar 13: 17: { 14: @implementation ClassA 18: x = 100; 15: -(void) initVar 19: } 16: { 20: -(void) printVar 17: x = 100; 21: 18: } 22: NSLog(@"x = %i", x); 19: 23: } 20: @end : : : 42: ClassA * a = [[ClassA alloc] init]; 43: ClassB * b = [[ClassB alloc] init]; 44: [a initVar]; // uses ClassA method 45: [a printVar]; // reveal value of x 46: 47: [b initVar]; // uses overrinding ClassB method 48: [b printVar]; // reveal value of x; : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 17. 17 Overriding the dealloc Method and the Keyword super Program 8.5B Rectangle.m Program 8.5B main.m : : 12: -(void) setOrigin: (XYPoint *) pt 23: [[myRect origin] release]; 13: { 24: [myRect release]; 14: if (! origin) 25: [myPoint release]; 15: origin = [[XYPoint alloc] init]; : 16: 17: origin.x = pt.x; 18: origin.y = pt.y; 19: } : Text Program 8.7B Rectangle.m Program 8.7B main.m : 33: -(void) dealloc : 34: { 23: [myRect release]; 35: if(origin) 24: [myPoint release]; 36: [origin release]; : 37: [super dealloc]; 38: } : overriding dealloc method keyword super Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 18. 18 Extension Through Inheritance: Adding New Instance Variables Program 8.8 main.m Program 8.8 main.m : : 07: @interface ClassA : NSObject { 22: @interface ClassB : ClassA 08: int x; 23: { 09:} 24: int y; 10: 25: } 11:-(void) initVar; 26: 12: 27: -(void) initVar; 13:@end 28: -(void) printVar; 14: 15:@implementation ClassA Text 29: 30: @end 16:-(void) initVar 31: @implementation ClassB 17:{ 32: -(void) initVar 18: x = 100; 33: { 19:} 34: x = 200; 20:@end 35: y = 300; : 36: } 37: -(void) printVar : 38: { 48: ClassB * b = [[ClassB alloc] init]; 39: NSLog (@"x = %i", x); 49: 40: NSLog (@"y = %i", y); 50: [b initVar]; 41: } 51: [b printVar]; 42: @end : : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language