Reputation: 3095
I'm confused as to why my instance variables aren't working in my subclass even though the instance variables are declared in the interface file of the parent class. My subclass inherits a method from the parent class to define the instance variables. Then the subclass uses one of its own methods to display the values of instance variables but the values are zero? Why is that?
/interface file of parent class **/
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
int w;
int h;
int j;
int k;
int a;
int b;
}
@property int width, height;
-(void) define;
@end
/**implementation file of parent class **/
#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(void)define{
a = width;
b = height;
}
@end
/**interface file of subclass **/
#import "Rectangle.h"
@interface Rectangle2 : Rectangle
@property int width, height;
-(void) show;
@end
/**implementation file of subclass **/
#import "Rectangle2.h"
@implementation Rectangle2
@synthesize width, height;
-(void) show{
NSLog(@"the width and height are %i and %i", width, height);
NSLog(@" a and b are %i and %i", a, b);
}
@end
/**Main**/
#import "Rectangle.h"
#import "Rectangle2.h"
int main (int argc, const char * argv[])
{
@autoreleasepool {
Rectangle * shape1;
shape1 = [[Rectangle alloc] init];
Rectangle2 * shape2;
shape2 = [[Rectangle2 alloc] init];
shape1.width =10;
shape1.height = 5;
shape2.width =2;
shape2.height = 3;
[shape2 define];
[shape2 show];
}
return(0);
}
My program displays the following:
Rectangle6[900:303] the width and height are 2 and 3
2013-07-15 20:09:35.625 Rectangle6[900:303] a and b are 0 and 0
Why are the a and b 0? Since these instance variables were declared in the inheritance file of the parent class, shouldn't I be able to use them in the subclass? I'm not getting any errors so I know that we are accessing the instance variables, but why am I not displaying the right values when running?
Upvotes: 1
Views: 101
Reputation: 12641
You should call your base class methods and variables using derived class object
#import <Foundation/Foundation.h>
@interface Rectangle : NSObject
{
int w;
int h;
int j;
int k;
int a;
int b;
}
@property int width, height;
-(void) define;
@end
#import "Rectangle.h"
@implementation Rectangle
@synthesize width, height;
-(id)init
{
if (self=[super init]) {
}
return self;
}
-(void)define{
a = width;
b = height;
}
@end
Rectangle 2
#import <Foundation/Foundation.h>
#import "Rectangle.h"
@interface Rectangle2 : Rectangle
@property int width, height;
-(void) show;
@end
#import "Rectangle2.h"
@implementation Rectangle2
@synthesize width, height;
-(id)init
{
if (self=[super init]) {
}
return self;
}
-(void) show
{
[super setHeight:10];
[super setWidth:5];
[super define];
NSLog(@"the width and height are %i and %i", width, height);
NSLog(@" a and b are %i and %i",a, b);
}
@end
Main Class
int main(int argc, char *argv[])
{
@autoreleasepool {
Rectangle2 * shape2;
shape2 = [[Rectangle2 alloc] init];
shape2.width =2;
shape2.height = 3;
[shape2 show];
return(0);
}
}
output is :-------
2013-07-16 09:26:49.275 rec[894:11303] the width and height are 2 and 3 2013-07-16 09:26:49.277 rec[894:11303] a and b are 5 and 10
Upvotes: 1