Hi everyone, I'm just getting my feet wet with Obj-C here. I'm going through Stephen Kochan's Programming in Objective-C 2.0, and have gotten stuck on this small problem.
Basically, I'm trying to add two numbers. I could easily do this the C way, by declaring 2 variables (either predefined or using scanf to get user input) but in doing it the Obj-C way, I am here:
However, I'm having two issues. One, im getting "error, 'total' undeclared (first use in this function)" in my "- (void) print; " method implementation, and secondly, the total readout says "5+7=0". What am I doing wrong here? Thanks in advance!
Basically, I'm trying to add two numbers. I could easily do this the C way, by declaring 2 variables (either predefined or using scanf to get user input) but in doing it the Obj-C way, I am here:
#import <Foundation/Foundation.h>
//interface section
@interface Addition : NSObject
{
int numOne;
int numTwo;
}
- (void) setNumOne: (int) a;
- (void) setNumTwo: (int) b;
- (void) print;
- (int) total;
@end
//implementation section
@implementation Addition
- (void) setNumOne: (int) a
{
numOne = a;
}
- (void) setNumTwo: (int) b
{
numTwo = b;
}
- (int) total
{
return (numOne + numTwo);
}
- (void) print
{
NSLog(@"%i + %i = %i", numOne, numTwo, total);
}
@end
//program section
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Addition *myAddition = [[Addition alloc] init];
[myAddition setNumOne: 5];
[myAddition setNumTwo: 7];
[myAddition print];
[myAddition release];
[pool drain];
return 0;
}
However, I'm having two issues. One, im getting "error, 'total' undeclared (first use in this function)" in my "- (void) print; " method implementation, and secondly, the total readout says "5+7=0". What am I doing wrong here? Thanks in advance!