I started to learn oC from today, and I am very happy to write a hello world for my reference.
# Import <Foundation/Foundation. h> @ interface rectangle: nsobject {int width; int height;}-(void) setwidth :( INT) W setgeight :( INT) h;-(INT) geperimeter;-(INT) getarea; @ end
# Import "rectangle. H "@ implementation rectangle-(void) setwidth :( INT) W setgeight :( INT) h {width = W; Height = H;}-(INT) geperimeter {return (width + height) * 2;}-(INT) getarea {return width * height;} @ end
The square class inherits the rectangle class.
# Import "rectangle. H" @ interface square: rectangle-(void) setside :( INT) W;-(INT) side; @ end
# Import "Square. H "@ implementation square: rectangle-(void) setside :( INT) W {[self setwidth: W setgeight: W] ;}- (INT) side {return width ;} -(INT) getarea {return [self side] * [self side];} @ end
Main file:
//// Main. M // ocstart /// created by wildcat on 13-3-25. // copyright (c) 2013 wildcat. all rights reserved. // # import <Foundation/Foundation. h> # import "person. H "# import" rectangle. H "# import" Square. H "int main (INT argc, const char * argv []) {@ autoreleasepool {// insert code here... nslog (@ "Hello, world! "); Nslog (@" Hello, Lee !! "); // Define a person class person * P = [[person alloc] init]; [p setstr: @" Li xingle "]; [p myprint]; // define a rectangle Class Object rectangle * r = [[rectangle alloc] init]; [R setwidth: 3 setgeight: 4]; int area = [R getarea]; int perimeter = [R geperimeter]; nslog (@ "the area is: % d, the parimeter is % d", area, perimeter ); // define a square Instance Object square * s = [[Square alloc] init]; // enter the positive side length nslog on the keyboard (@ "Enter the side length of the square "); int number; scanf ("% I", & number); // set the side length [s setside: number]; int side = [s side]; int areas = [s getarea]; int perimeters = [s geperimeter]; nslog (@ "the square's side is: % d, the area is: % d, the primeter is: % d ", side, areas, perimeters); nsstring * Hello = @" sdfgdsf "; // convert it to uppercase Hello = [Hello uppercasestring]; nslog (Hello); // call the nslog (@ "the string length is: % lD", [Hello length]);} return 0 ;}