Home Iphone Objectivec Class and Method declaration and definitions
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Class and Method declaration and definitions

Advertisement
Because of objective-C is the extension of ANSI-C and it follows an object oriented approach so provides classes and objects.

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 ;
}

 

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 24, 2009

Ask Questions?    Discuss: Class and Method declaration and definitions   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
ghalib
June 14, 2011
error

when i compile it gives the error of that Nsobject or foundation directory not found
Balaji
November 16, 2011
Code doesn't work

The code that is mentioned here doesn't work at all. First of all, how to compile each of these classes. Where do we find NSObject.h?
DMCA.com