My Blog List

Showing posts with label Review. Show all posts
Showing posts with label Review. Show all posts

Tuesday, October 26, 2010

Reviewing Concepts:Portal Used Since - Back and Forth in the Book



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.”

He also says, “the constructor returns a pointer to the object, and you get that pointer by calling the super class’s init method.” The pointer returned for the page 97 example would be ‘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;
}
 

 

Tuesday, October 19, 2010

Reviewing Concepts: Sussin' Out Time Portals



Program loaded.
run
[Switching to process 1280]
Running…
Portal Pilot: Engineering Log for Common Era 99.921 
TimePortal count is 1
TimePortal count is 2
....
 Portal Pilots has identified 2 new Time Portals in the Common Era

Debugger stopped.
Program exited with status value:0.



------------------
#import <stdio.h>
#import <Foundation/NSObject.h>


// Based on Page 110 ClassVariables2 and use of 'self'keyword

//  
// Portal Pilots are always searching for new Time Portals
//
// 
@interface TimePortal:NSObject
static int count;

+(int) getCount;
@end

//METHODS
@implementation TimePortal
-(TimePortal*) init

{
// (p 136) The 'super' keyword alows me to access the base class
// super = superclass = base class .... the class from which my
//     class TimePortal inherits or derives functionality
//     (p 119) This inheritance can include methods from the superclass
//  QUESTION I thought that a class could make use of ivars in the
//     super class too. But am not finding that this minute. CHECK BACK
// Here I'm using a constructor from the superclass to initialize the
//      data in an object when I create it (p 97)
//    Contructors are very typically called 'init'
//    The superclass for 'TimePortal' is 'NSObject'
self = [super init];
count++;
// A constructor returns a pointer to the object. 
// I get the pointer by calling the super class's init method. (p 97)
return self;
}

+(int) getCount
{
return count;
}
@end



int main (void) {
// Would LOVe to know how to display the SYSTEM DATE here
// It must be a keyword
// Look in the C++ Book
printf("Portal Pilot: Engineering Log for Common Era 99.921 \n");
    TimePortal *tp1 = [TimePortal new];
printf("TimePortal count is %i\n", [TimePortal getCount]);
TimePortal *tp2 = [TimePortal new];
printf("TimePortal count is %i\n", [TimePortal getCount]);
printf("\t....\n Portal Pilots has identified %i new Time Portals in the Common Era\n ", [TimePortal getCount]);

return 0;
}