C programming Objective Tutorial - Syntax and Classes


Khaoz.Tk
 
PortalHomeSearchMemberlistCalendarFAQLog inRegisterUsergroups
Increase font size   Decrease font size

Share | 
 

 C programming Objective Tutorial - Syntax and Classes

View previous topic View next topic Go down 
AuthorMessage
_-_EVIL-LOCO_-_
Programmer
Programmer



Posts : 7
Join date : 2010-06-02

PostSubject: C programming Objective Tutorial - Syntax and Classes   Thu Jul 01, 2010 11:12 pm

Introduction/Summary

Hey what's up, I am Aresborne and today I will show guys a simple tutorial of C programming. What will we learn today is Syntax and classes. Objective C has the same syntax as C with the addition of smalltalk like syntax for classes. Objective C classes are usually written in two files. There's a header file which contains the definition and a file which contains the implementation of the class. You can put everything in one file but not all the time or not the convention. Let's start an example, first we will start making a "person" class.

Note #1:To compile this code or another codes, you will need gcc and GNUStep installed.
If you're running a Mac, you will need XCode. if you're running Windows, install MinGW, and then install GNUStep.

Note #2: Every example of classes/syntax functions contains examples with this symbol right next to it
Symbol: /* Aresborne */

Example #1:

Code:
 // Person.h

/*
 * Objective C uses #import instead of #include
*/
#import
#import

/*
 * Inheritance in Objective C is shown by a classname
 * followed by a colon followed by another classname
 * NSObject is the superclass for every Objective C class, either directly or indirectly
*/
@interface Person: NSObject
{
        //Instance data are declared here:
        @private
                NSString* name;
                int age;
}

//Methods are declared here:

/*
 * Methods are defined like this
 * -() : ()
*/
-(Person*) initWithName: (NSString*) n andAge: (int) a;

-(NSString*) name;

-(int) age;

-(NSString*) speak;

@end
[code]// Person.m

#import "Person.h"

@implementation Person

-(Person*) initWithName: (NSString*) n andAge: (int) a
{
/*
* Methods are called with [ ];
* init is a method provided by NSObject that provides a default constructor
*/
self = [super init];


/*
* if the super constructor returned a success, continue initialization
*/

if (self)
{
/*
* release and retain are memory management methods
* retain increments the count of pointers to a location in memory
* release does the opposite
* Once the count is zero the memory is released
*/
[n retain];
[name release];
name = n;

/*
* age is simply an int, and not a pointer
* so it's pass-by-value
*/
age = a;
}

/*
* self is analagous to this in C++/Java
*/
return self;
}

-(NSString*) name
{
return name;
}

-(int) age
{
return age;
}

-(NSString*) speak
{
/*
* stringWithFormat creates a new string using printf format
* %@ inserts an NSObject
* and you don' need to call the init method here, because the
@ preceding the string constant initializes the string for you
*/
NSString* result = [ [NSString alloc] initWithFormat:@"Hi. My name is %@ and my age is %d.",
name, age];

return result;

}

@end


Section 2:

Now for the driver file

[code][pre]// main.m

#import
#import
#import
#import "Person.h"

int main(int argc, const char* argv[])
{
/*
* alloc reserves memory space for an object
* NSAutoReleasePool is an automated Garbage collection mechanism
*/
NSAutoreleasePool* pool = [ [NSAutoreleasePool alloc] init];
NSString* name = [NSString stringWithString: @"GWatt"];
Person* gwatt = [ [Person alloc] initWithName: name andAge: 500];

/*
* printf can't handle NSStrings so we use UTF8String to return a char array
*/
printf("Name:\t%s\n", [ [gwatt name] UTF8String] );
printf("Age:\t%d\n", [gwatt age] );
printf("\n%s\n", [ [gwatt speak] UTF8String] );

[gwatt release];

return 0;
}
[/pre][/code]

We compile this code by opening a terminal and typing:
"gcc Person.m main.m -o person -framework Foundation -fobjc-exception"
and run it by:
"./person"
or
"person.exe"<---If your running windows

Did you know....

Objective C is not as syntactically close to C as C++ is, but is not a completely new language to learn either. The most practical application of objective c is as an Apple developer since Apple writes their applications using Objective C. It's definitely not as widespread as the .net languages, but if you want to program on a Mac and want an OOPL that's not Java, Objective C is worth knowing.

Thanks for reading and I hope you learn something in this tutorial

~Aresborne
Back to top Go down
View user profile
 

C programming Objective Tutorial - Syntax and Classes

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
KhaoticGamerz.Tk :: PC Tutorials and Sources :: C/C#/C+/C++-
Jump to:  
Copyright 2010 © KhaoticGamerz
Free forum | Games | Action | © phpBB | Free forum support | Contact | Report an abuse | Create a forum on Forumotion