One of the things that is helping me develop my skills with how oop works is the one-simple-concept-at-a-time, nature of the Quick Start book. Having taught math, I know that abundant practice is the key to problem solving and learning. Of course that's the reason that I do a second pass on each book exercise (I don't put them all out here). That's a good start, because even simply renaming things to match my own stories gives me more ownership, and makes me think through the code more.
Also for each piece of code that I get to run, I try to work back though each of the lines of code and make sure I can explain to myself the reason for that line being there and what it does.
In the process of re-thinking each line of code from my working example based on page 123, I remembered that the author had me instantiating an object differently a chapter or so back. I found the difference on page 97.
Why in one case, page 123 (when author happens to be talking about inheriting base-class data members) in the Quick Start book do we only instantiate an object with
Bridge *c2 = [Bridge new];
c2-> year = 1917;
But in another case we use ‘init’, and the author makes a big deal about using it on page 97, when he talks about “using constructors”
Container *object = [(Container new], init:2];
….
√ In the case of the page 97 ‘Container’ example, the author wants to make the point that a constructor allows me to ‘initialize the data in an object when I (you) create the object.”
Thanks Mr. Holzner, for focusing on one thing at a time.
......
Loading program into debugger…
Program loaded.
run
[Switching to process 44577]
Running…
This particular BRIDGE portal has been in use since 1917
Debugger stopped.
Program exited with status value:0.
...
#include <stdio.h>#import <Foundation/Foundation.h>
//inheriting base class data members QStart page 122
@interface Portal : NSObject
{
@public
int year;
}
@end
@implementation Portal
@end
@interface Bridge : Portal
@end
@implementation Bridge
@end
int main (void)
{
// *bridge is a pointer
// a pointer holds the address in memory of a data item
// "Bridge *bridge" means that this pointer is a variable of type 'Bridge'
// Instantiate the Bridge class
Bridge *bridge = [Bridge new];
// Next the pointer, bridge, picks up the address in memory for the ivar/class variable 'year'
// year is an ivar that the Bridge class inherits from the Portal class
// At the same time I set the value of 'year' to be 1917
bridge-> year = 1917;
//Am really looking forward to the day when I'm not hard-coding in information like the type of portal but am able to pick it up
// from a class variable!
printf("This particular BRIDGE portal has been in use since %i\n", bridge->year );
return 0;
}
No comments:
Post a Comment