Here is an example I wrote to test how Objective-c uses the class's
TestClass.h
testclass.h// testclass//// Created by exchen on 6/15/15.// Copyright (c) exchen. All rights reserved.//#import <Foundation/Foundation.h> @interface testclass:nsobject{ //public member variable @ public int number1; int number2; NSString *nstr; Char strarray[20];} member function declaration-(void) print;-(void) calc;-(nsstring*) Strappend: (nsstring*) string1: (nsstring*) string2; @end
Testclass.m
testclass.m// testclass//// Created by exchen on 6/15/15.// Copyright (c) exchen. All rights reserved.//#import "TestClass.h" #import <stdio.h> @implementation testclass//member function implementation-(void) print{ printf ("%d\n", number1); NSLog (NSTR); printf ("%s\n", Strarray);} -(void) calc{ number1 + = number2; printf ("%d\n", Number1);} -(nsstring*) Strappend: (nsstring*) string1: (nsstring*) string2{nsstring *strret = [string1 STRINGBYAPPENDINGSTRING:STRING2]; return strret;} @end
Main.m
main.m// testclass//// Created by exchen on 6/15/15.// Copyright (c) exchen. All rights reserved.//#import <Foundation/Foundation.h> #import "TestClass.h" int main (int argc, const char * argv[] { @autoreleasepool { //Insert code here ... NSLog (@ "Hello, world!"); } TestClass *TC = [[TestClass alloc] init]; allocating memory tc->number1 = 1; Assigning a value to a class member variable tc->number2 = 2; [TC Calc]; Call the class member function strcpy (Tc->strarray, "Strarray"); Assigning a value to a class member string variable [TC print]; Call the class member function nsstring *strret = [TC strappend:@ "string1": @ "string2"]; Call a function with parameters NSLog (strret);//print return value returned 0;}
Engineering
Objective-c the use of creating classes