Defining a Class in Objective C, XCode
- by Brett
Hello;
I am new to Objective C, and am trying to write a class that defines a complex number. The code seems fine but when I print to the console, my values for instance variables are 0.
Here is the code:
//
// ComplexNumber.h
// Mandelbrot Set
//
// Created by Brett on 10-06-02.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <stdio.h>
@interface ComplexNumber : NSObject {
double real;
double imaginary;
}
// Getters
-(double) real;
-(double) imaginary;
// Setters
-(void)setReal: (double) a andImaginary: (double) b;
//Function
-(ComplexNumber *)squared;
@end
//
// ComplexNumber.m
// Mandelbrot Set
//
// Created by Brett on 10-06-02.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ComplexNumber.h"
#import <math.h>
#import <stdio.h>
@implementation ComplexNumber
-(double)real{
return self->real;
}
-(double)imaginary{
return self->imaginary;
}
-(void)setReal: (double) a andImaginary: (double) b{
self->real=a;
self->imaginary=b;
}
-(ComplexNumber *)squared{
double a = pow(real,2);
double b = pow(imaginary, 2);
double c = 2*real*imaginary;
ComplexNumber *d;
[d setReal:(a-b) andImaginary: c];
return d;
}
@end
In the App Delegate for debugging purposes I added:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ComplexNumber *testNumber = [[ComplexNumber alloc] init];
[testNumber setReal:55.0 andImaginary:30.0];
NSLog(@"%d", testNumber.real);
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
But the console returns 0 everytime. Help?