I'm working on this Objective C programming assignment I found online. I'm not sure if I have met all the requirements, especially part C. Any help or suggestion will be appreciated.
Part 6
a) Implement class
A
with propertiesa1
,a2
, anda3
(int
,string
,int
).b) New objects are automatically initialized to
1
,"hello"
,1
.c) Also provide initializer to any data and constructor (called without alloc) to do the same.
d) Make sure
%@
ob object ofA
will print all data.e) Then implement
B
inheriting fromA
.B
adds propertyb
(string)
.f) Make sure
B
works asA
, that is new object is initialized to1
,"hello"
,1
, and3
(the new data). The rest also must work onB
.
//classA.h file
#import <Foundation/Foundation.h>
@interface ClassA : NSObject
// Part 6a
@property int a1;
@property NSString *a2;
@property int a3;
-(NSString *) description;
-(id) initWithA1: (int) x andA2: (NSString *) s andA3: (int) y;
-(id) init;
@end
//classA.m file
#import "ClassA.h"
@implementation ClassA
-(id) initWithA1:(int)x andA2:(NSString *)s andA3:(int)y {
self = [super init];
if (self) {
self.a1 = x;
self.a2 = s;
self.a3 = y;
}
return self;
}
// part 6b
- (id) init {
return [self initWithA1:1 andA2:@"hello" andA3:1];
}
// part 6d
-(NSString *) description {
return [NSString stringWithFormat:@"ClassA a1 = %d , a2 = %@ , a3 = %d", self.a1, self.a2, self.a3];
}
@end
//classB.h file
#import "ClassA.h"
@interface ClassB : ClassA
@property int a1;
@property NSString *a2;
@property int a3;
@property NSString * b;
-(NSString *) description;
-(id) initWithA1:(int)x andA2:(NSString *)s andA3:(int)y andB: (NSString *) z;
-(id) init;
@end
//classB.m file
#import "ClassB.h"
@implementation ClassB
-(id) initWithA1:(int)x andA2:(NSString *)s andA3:(int)y andB:(NSString *)z {
self = [super init];
if (self) {
self.a1 = x;
self.a2 = s;
self.a3 = y;
self.b = z;
}
return self;
}
-(id) init {
return [self initWithA1:1 andA2:@"hello" andA3:1 andB:@"3"];
}
-(NSString *) description {
return [NSString stringWithFormat:@"ClassB a1 = %d , a2 = %@ , a3 = %d , b = %@" , self.a1, self.a2, self.a3, self.b];
}
@end
//viewController.m file
#import "ViewController.h"
#import "ClassA.h"
#import "ClassB.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ClassA * a = [ClassA new];
NSLog(@"%@", a);
ClassB * j = [ClassB new];
NSLog (@"%@", j);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end