[an error occurred while processing this directive]

Class and Method declaration and definitions

                         

Because of objective-C is the extension of ANSI-C and it follows an object oriented approach so provides classes and objects. The way to declare and define classes and creation of object is little bit different from C and C++.
To declare a new class objective-C uses @interface directive.

 

 

 



Declaration of a simple class: MyClass.h

#import"SuperClass.h"
#import<headerFile.h>
@interface ClassName:SuperClass {
     variable daclaration;
     variable daclaration;
   }
    method declaration;
    method declaration;
@end
#import<Foundation/NSObject.h>

@interface MyClass:NSObject{
    int a;
    int b;
    }
    -(void) setvara : (int) x;
    -(void) setvarb : (int) y;
    -(int) add;
@end

Definition of declared class: MyClass.m

#import<stdio.h>
#import"MyClass.h"

@implementation MyClass
   -(void) setvara :(int) x{
     a=x;
   }
   -(void) setvarb :(int) y{
     b=y;
   }
   -(int) add{
     return a+b;
   }
@end

Piecing it together
main.m

#import<stdio.h>
#import"MyClass.m"

int main(){
  MyClass *class = [[MyClass alloc]init];
  [class setvara : 5];
  [class setvarb : 6];
  printf("Sum is : %d",[class add]);
  [class release];
  return ;
}

 

                         

[an error occurred while processing this directive]