Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are of course many ways to set this up. I can remember being confused when starting out, below is an alternative example to give you something else to look at. </p> <p>It is not intended to be fancy but just bare-bones so that you can see a minimal setup of the class with an initializer. </p> <p>Note that the only value initialized is the const pi, of course, the radius can be initialized there as well, as nhgrif's example shows quite nicely.</p> <p>Hope this helps!</p> <pre><code>// Circle.h #import &lt;Foundation/Foundation.h&gt; @interface Circle : NSObject { double radius; double pi; } @property double radius, pi; -(double) getArea; -(double) getDiameter; -(double) getCircumference; @end </code></pre> <p>And then the implementation:</p> <pre><code>// Circle.m #import "Circle.h" @implementation Circle @synthesize radius, pi; // Initialize with const pi: - (id)init { self = [super init]; if (self) { pi = 3.14159; NSLog(@"Circle created."); } return self; } -(double) getArea { return pi*radius*radius; } -(double) getDiameter { return 2*radius; } -(double) getCircumference { return 2*pi*radius; } @end </code></pre> <p>And then for main:</p> <pre><code>// main.m #import &lt;Foundation/Foundation.h&gt; #import "Circle.h" int main(int argc, const char * argv[]) { @autoreleasepool { Circle *aCircle = [[Circle alloc] init]; // Use an arbitrary value: [aCircle setRadius:2]; NSLog(@"Area = %f",[aCircle getArea]); NSLog(@"Circumference = %f",[aCircle getCircumference]); NSLog(@"Diameter = %f",[aCircle getDiameter]); NSLog(@"Check pi = %f",[aCircle pi]); } return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload