It is All About the Class

At the heart of any object oriented language are well, objects. Defining these objects are classes. It’s common to start learning from all the variable types and naming conventions but as I already know a little about these things from Java and C# I am taking the bold step of writing a class which defines and tests a resistor.

First there’s the interface presenting the class’s members.

The @interface thing is called a compiler directive. I find them really cool for some reason. They are used to declare and define a whole lot of things, as I understand. I will be learning more of them later.

//fileName: Resistor.h

@interface Resistor : NSObject
 double resistance, voltageAcross, currentThrough;

- (id) init:(double)r;
- (void) setVoltageAcross:(double)v;
- (void) setCurrentThrough:(double)i;
- (void) print;

Next the actual definition of the class and all the things it can do, in accordance with the interface defined above.

The highlighted line below prints out information about the resistor. NSLog calls the console in xCode and writes a string to it. Writes a NSString, to be exact, which is defined and initialized by the compiler directive beginning with @.

//fileName: Resistor.m

#import "Resistor.h"

@implementation Resistor
- (id) init:(double)r
 resistance = r;
 return self;

- (void) setVoltageAcross:(double)v
 voltageAcross = v;

- (void) setCurrentThrough:(double)i
 currentThrough = i;

- (void) print
 NSLog(@"V:%f I:%f R:%f", voltageAcross, currentThrough, resistance);

Now to test the class I wrote a main routine which goes pretty much like C but with some differences.

//fileName: main.m

#import <Foundation/Foundation.h>
#import "Resistor.h"

int main (int argc, char *argv[]) {
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 Resistor * aResistor = [[Resistor alloc] init:10];

 [aResistor setVoltageAcross:25];
 [aResistor setCurrentThrough:1];
 [aResistor print];

 [pool drain];
 return 0;

The highlighted lines are foreign to me, and I will seek our their reason for existence soon. But as far as I understand thus far, a class called NSAutoreleasePool is instansiated and one of its methods is called on in the second last line.

The #import statements in lines 3 and 4 respectively import the base library and the Resistor class to the routine.

Line 9 creates an object of Resistor type and set its resistance to 10.
Lines 11 to 13 ask the aResistor object to execute the methods I wrote in the Resistor class. They set the values of voltage and current of the resistor and call on print to give the output.

Now the output, as expected is:

[Switching to process 6845]
2009-12-06 15:35:58.708 Untitled[6845:a0f] V:25.000000 I:1.000000 R:10.000000

Debugger stopped.
Program exited with status value:0.

This is all too pretty, let’s see what comes next.


One Comment on “It is All About the Class”

  1. […] a comment » After a bit of thinking about the class I wrote in my previous post, I figured I should make the class have more features, and learn a few things while doing so. The […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s