Ditto on what Luis and rmaddy said. Some of this is best illustrated with an example. Lets consider a Circle class object:
Here is the .h file:
// Circle.h
#import <Foundation/Foundation.h>
@interface Circle : NSObject
{
double radius;
double pi;
}
@property double radius, pi;
-(double) area;
-(double) diameter;
-(double) circumference;
@end
And then the implementation file (note that I have defined instance methods as Luis points out):
// Circle.m
#import "Circle.h"
@implementation Circle
@synthesize radius, pi;
// Initialize with default radius:
- (instancetype)init {
self = [super init];
if (self) {
pi = 3.14159;
NSLog(@"Circle created.");
}
return self;
}
-(double) area {
return pi*radius*radius;
}
-(double) diameter {
return 2*radius;
}
-(double) circumference {
return 2*pi*radius;
}
@end
Now create a circle object in main and send messages to return several relevant quantities:
// main.m
#import <Foundation/Foundation.h>
#import "Circle.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Circle *aCircle = [[Circle alloc] init];
[aCircle setRadius:2];
NSLog(@"Area = %f",[aCircle area]);
NSLog(@"Circumference = %f",[aCircle circumference]);
NSLog(@"Diameter = %f",[aCircle diameter]);
NSLog(@"Check pi = %f",[aCircle pi]);
}
return 0;
}
You can see from this example the value of pi is set when the object is created and then is stored as part of the object, available for other calculations.
Note: you wouldn't want to define pi this way in practice, this is just a simple example to illustrate the point.
Note also that I could have set other internal values of the object after it was created, then this data is also available from that point forward, subject to the qualifications pointed out in the post.
Does this make better sense? You should run this code and experiment with the idea until you feel more comfortable with it.
EDIT: At Andrew Madsen's suggestion, updated the accessor methods for area, diameter, and circumference; these should not be prefixed with 'get'