Creating a Calculator Class
Creating a calculator class in Objective C is not an easy task! Oh, c’mon sure it
. I’ve been reading about operators lately. Arithmetic operators, bit operators, assignment operators etc. The fact that many of these things were known helps me gap the lack of time to proceed with reading as I wanted too. Here is my last code:
#import <objc/Object.h> #import <stdio.h> @interface Calculator: Object { double accumulator; } // accumulator methods -(void) setAccumulator: (double) value; -(void) clear; -(double) accumulator; // arithmetic methods -(void) add: (double) value; -(void) subtract: (double) value; -(void) multiply: (double) value; -(void) divide: (double) value; @end @implementation Calculator -(void) setAccumulator: (double) value { accumulator = value; } -(void) clear { accumulator = 0; } -(double) accumulator { return accumulator; } -(void) add: (double) value { accumulator += value; } -(void) subtract: (double) value { accumulator -= value; } -(void) multiply: (double) value { accumulator *= value; } -(void) divide: (double) value { accumulator /= value; } @end int main (int argc, char *argv[]) { Calculator *calc; calc = [[Calculator alloc] init]; [calc clear]; [calc setAccumulator: 100.00]; [calc add: 200.00]; [calc divide: 15.00]; [calc subtract: 10.00]; [calc multiply: 5.00]; printf("The result is %g\n", [calc accumulator]); [calc free]; return 0; }
Okay, pretty simple you’ll say! But it’s not! Actually for the first time some things came natural! The allocation and initialisation of memory, I didn’t even think about the right syntax after the first line [calc clear]; which helped write the program quickly. However I made some mistakes the first I wrote the program. The first one was not paying attention that the accumulator was of type “double” and not “void” which caused some warning but not fatal errors
Related posts:
- Programming issues. Writing a tax calculator: part 2. After playing around with the tax calculator I managed to...
- Programming issues. Writing a tax calculator. My Objective C reading doesn’t go as I would like...
Related posts brought to you by Yet Another Related Posts Plugin.


Hmmm. I don’t think what you have there actually compiles, let alone runs without errors, at least on Apple’s implementation of Objective-C. What kind of compiler/libraries did you use for this? And for which version of Objective-C?
For starters while you’re extending
Object. NextStep/Cocoa definesNSObject. Then you do[calc free]. The correct way to release an object is by[object release], or — this is not recommended — [object dealloc]. Perhaps you were thinking of C?Good to see more people coding in Objective-C. Good luck.
Hello there Cosmix!
Actually the #import was naked because completelly missing html tags, I didn’t notice that the < and > characters needed special html tags to be displayed.
Now compiles at least via command line:
As for [calc free], I’ve read in the manual that it’s the a way to release the allocated & initialized memory. I didn’t knew that there is another way to do it and I don’t have a clue about the differences. I’ll try out the [object release]; method asap!
Thnx for the tips
Ah, you were using ‘pure’ Objective-C. Apologies, I was referring to Cocoa (or at least Foundation). You see, even with simple command line tools, it’s a good idea to leverage the features provided by the Foundation classes.
You won’t find release and dealloc in the ‘pure’ objc runtime, as they are part of NSObject. At some point you will essentially be forced to import at least Foundation to do any sort of meaningful work. I’d suggest you spend some time familiarising yourself with NSObject, as opposed to the pure ObjC ‘Object’ as you probably won’t be using the latter too much.
I didn’t knew the differences thank you for the enlightment
I’ll keep that in mind. For now I do mostly exercises that the book shows.
I’m dealing with general purpose concepts. However the main idea is to be able to write cocoa applications sooner of later.. so your tips are very useful.
beautiful online information center. greatest work thanks