KEMBAR78
OOP Chapter 8 : Inheritance | PDF
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

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