Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Introduction to Objective-C Inheritance

Shape and Circle program compile results not what they should be

When I build, compile and run the example program we walk through in this lesson, my Circle area comes up as -1.000000 the same as the Shape area. All my code (Shape.h, Shape.m, Circle.h, Circle.m) is exactly the same as the instructors. I have made sure to go over it several times and yet I still don't get the results that show up for the instructor. Can someone please tell me what is going on?

Here is my main:

#import <Cocoa/Cocoa.h>
#import "Shape.h"
#import "Circle.h"


int main() {
    Shape *shape = [[Shape alloc]init];

    NSLog(@"Shape area is %f", [shape area]);

    Circle *round = [[Circle alloc]init];
    round.radius = 24;

    NSLog(@"Circle area is %f", [round area]);


    return 0;
}

Here is my Shape.m:

#import "Shape.h"

@implementation Shape
-(float)area {
    return -1;
}

@end

Here is my Shape.h:

#import <Foundation/Foundation.h>

@interface Shape : NSObject
-(float)area;
@end

Here is my Circle.m:

#import "Circle.h"

@implementation Circle

-(float)radius {
    return M_PI * (self.radius * self.radius);
}

@end

Here is my Circle.h:

#import "Shape.h"

@interface Circle : Shape

@property(nonatomic) float radius;

@end

Hope all of that helps. I'm running Xcode 6.1 as well. Thanks!

2 Answers

Tom Holland
Tom Holland
24,288 Points

The problem is on line 5 of Circle.m

Your method is named -(float)radius when it should be -(float)area.

Tom you're absolutely right! Thank you! I knew it was something simple that I was missing. Appreciate it!

Tom Holland
Tom Holland
24,288 Points

It's always something! Glad I could help.