Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
OOP: Chapter 2: Programming in Objective-C
1. Object-Oriented Programming Language
Chapter 2 : Programming in Objective-C
Atit Patumvan
Faculty of Management and Information Sciences
Naresuna University
วันอังคารที่ 7 กุมภาพันธ์ 12
2. 2
Program 2.1 (ex02-01.m)
// First program example
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
! NSLog (@"Programming is fun");
!
! [pool drain];
! return 0;
}
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
3. 3
Compile and Running Program
• OSX
gcc -framework Foundation hello.m -o hello
• Windows: GNUStep: gcc
gcc `gnustep-config --objc-flags` -L /GNUstep/System/
Library/Libraries hello.m -o hello -lgnustep-base -lobjc
• Linux
gcc `gnustep-config --objc-flags` hello.m -o hello -I /
usr/include/GNUstep/ -L /usr/lib/GNUstep/ -lgnustep-
base
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
4. 4
Common Filename Extension
Extension Meaning
.c C language source file
.cc, .cpp C++ language source file
.h Header file
.m Objective-C source file
.mm Objective-C++ source file
.pl Perl source file
.o Object (compiled) file
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
5. 5
Explanation of Your First Program
// First program example
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
! NSLog (@"Programming is fun");
!
! [pool drain];
! return 0;
}
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
6. 6
Program 2.2 (ex02-02.m)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
!
! NSLog (@"Programming is fun");
! NSLog (@"Programming in Objective-C is event more fun!");
!
! [pool drain];
! return 0;
}
Programming is fun
Programming in Objective-C is event more fun!
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
7. 7
Program 2.3 (ex02-03.m)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
!
! NSLog (@"Testing...n..1n...2n....3");
!
! [pool drain];
! return 0;
}
Testing...
..1
...2
....3
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
8. 8
Program 2.4 (ex02-04.m)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
!
! int sum;
!
! sum = 50 + 25;
! NSLog (@"The sum of 50 and 25 is %i", sum);
!
! [pool drain];
!
! return 0;
}
The sum of 50 and 25 is 75
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12
9. 9
Program 2.5 (ex02-05.m)
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
!
! int value1, value2, sum;
!
! value1 = 50;
! value2 = 25;
! sum = value1 + value2;
! NSLog (@"The sum of %i and %i is %i", value1, value2, sum);
!
! [pool drain];
!
! return 0;
}
The sum of 50 and 25 is 75
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
วันอังคารที่ 7 กุมภาพันธ์ 12