#import<Foundation/NSObject.h> @interface MyClass:NSObject{
int a;
int b;
}
// declare constructor
-(MyClass*) set:(int) a andb:(int) b;
-(void) sum;
@end
|
MyClass.m
#import<stdio.h> #import"MyClass.h" @implementation MyClass // define constructor
-(MyClass*) set:(int) x andb:(int) y {
self = [super init];
if(self) {
a=x;
b=y;
return self;
}
}
-(void) sum {
printf("Sum is : %d",a+b);
}
@end
|
MyClassMain.m
#import<stdio.h>
#import"MyClass.m"
int main(){
// use constructor
MyClass *class = [[MyClass alloc] set : 10 andb : 12];
[class sum]; [class release]; return ; } |
Output:
| Sum is : 22 |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Objective C Constructors View All Comments
Post your Comment