#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 |
|
Recommend the tutorial |


Ask Questions? Discuss: Objective C Constructors View All Comments
Post your Comment