Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Objective-C
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Mon Feb 09, 2004 12:35 pm   Post subject: Objective-C

Objective-C is an elegant programming language which combines the power and flexibility of C with the object model of Smalltalk. As such it has all of the things you'd expect in C, plus the ability to create classes and objects, and use (single) inheritance. Perhaps an example would show it off best.

code:
// The standard include file when using GCC
#include <objc/Object.h>
#include <string.h>
#include <stdio.h>

// First we create the root class for our purposes:
// NamedThing, which itself inherits from the Object class
@interface NamedThing : Object
{
   @protected
   char * name;
}
- initWithName: (const char *) initName;
@end

// Next we create the Dog class which is a NamedThing
@interface Dog : NamedThing
- (void) bark;
@end

// But we have little dogs that have weird barks
@interface LittleDog : Dog
- (void) bark
@end

// Our Main function
int main()
{
   Dog * fido = [[Dog alloc] initWithName: "Fido"];
   LittleDog * fluffy = [[LittleDog alloc] initWithName: "Fluffy"];

   [fido bark];
   [fluffy bark];

   return 0;
}

// And actually implement those methods.
@implementation NamedThing
- initWithName: (const char *) initName
{
   [super init];
   strcpy(name, initName);
   return self;
}
@end

@implementation Dog
- (void) bark
{
   puts("Woof!");
}
@end

@implementation LittleDog
- (void) bark
{
   puts("Yip!");
}
@end
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Mon Feb 09, 2004 3:20 pm   Post subject: (No subject)

yes objective-c is an excellent language. i've got a little experience with it. not much (consider me a newbie). i use for osx development.
rizzix




PostPosted: Mon Feb 09, 2004 3:22 pm   Post subject: (No subject)

can u explain in more detail the method headers, method calls etc. and the +/- modifiers
wtd




PostPosted: Mon Feb 09, 2004 3:59 pm   Post subject: (No subject)

rizzix wrote:
can u explain in more detail the method headers, method calls etc. and the +/- modifiers


Certainly. Just interested in seeing if there was any interest first. Smile

code:
@interface NamedThing : Object


Here I've begun declaring the interface for the NamedThing class. Much like C++, the colon indicates that I'm inheriting from the Object class. Every class in Objective-C inherits from a base class. In the sparse GCC libraries that class is Object. As another example, Apple's Cocoa API has the class NSObject at its root.

code:
@end


Skipping ahead a bit, this simply ends the interface specification.

code:
{
   @protected
   char * name;
}


In between we see this. Between the braces I've declared the instance variables my class contains. In this case I'm only using a single C string to hold a name. I've declared this protected (private and public labels are also available) partly because I wanted to demonstrate that it's possible, and also because classes in Objective-C can be treated much like structs in regular C.

code:
NamedThing * foo;
strcpy(foo->name, "hello world!");


Letting instance variables be public means the compiler doesn't complain about a hack like this. But it does break the whole object-oriented theme, so, by labeling it protected, I get the compiler to issue an error if it sees me using something like the above code.

code:
- initWithName: (const char *) initName;


The "-" indicates that this method is an instance method, just as you would declare in C++. However, Objective-C also uses "+" in this position. A "+" sign indicates that the method in fact acts on the class itself.

code:
[NamedThing alloc]


An example of just such a method.

The return type of the method follows the + or - sign in parentheses. This defaults to a type not seen outside Obj-C called "id", which is sort of a generic type. If no return type is specified Objective-C assumes you meant to include "(id)". Next is the method name.

Objective-C features method names that might seem odd to many programmers not familiar with SmallTalk. Instead of a method name, then a list of arguments, the arguments are placed in the name, always after a colon. This allows the method call to read more naturally, as arguments are associated directly with the part of the method name which refers to them. The type of each argument is specified in a manner identical to that used for the method's return type.

code:
LittleDog * fluffy = [[LittleDog alloc] initWithName: "Fluffy"];


Here I'm declaring a pointer to a LittleDog object. In Objective-C objects are not statically declared as they can be in C++. To the right of the "=" we see a nested method call. The brackets may seem odd, but they're a straightforward way of enclosing the SmallTalk-inspired method call syntax without conflicting with existing C syntax. This example also shows both a class method ("alloc") and the "initWithName:" instance method calls.

The "@implementation" is relatively straightforward. The method is repeated here, then its implementation follows in curly brackets, just as in regular C or C++.

Normally though, the interface and implementation files would be in separate files, both having the name of the class followed by a ".h" for the interface", and ".m" for the implementation.
rizzix




PostPosted: Mon Feb 09, 2004 4:33 pm   Post subject: (No subject)

hmm ok.. u know in ur example :
Quote:

NamedThing * foo;
strcpy(foo->name, "hello world!");


looking at this and with my knowldege of c++, here are my questions..

is foo a pointer to an object of NamedThing?
if so why isin't foo assigned and Instance of NamedThing, like we do in c++..
code:

NamedThing *foo = new NamedThing();



now looking at the method header..
Quote:

- initWithName: (const char *) initName;


so - indicates an instance method (i prefer java terminology, makes things clear) and + indicates a static method?

initWithName is the name of the method?

: seperates the method name from the parameters right? well what if my method has no parameters do i write the ':'?

initName is a parameter of type (const char*)? what if i want to write a method that accepts more than one argument?

... these are all the questions i have so far.. more to come later.. (probably)
wtd




PostPosted: Mon Feb 09, 2004 8:39 pm   Post subject: (No subject)

rizzix wrote:
hmm ok.. u know in ur example :
Quote:

NamedThing * foo;
strcpy(foo->name, "hello world!");


looking at this and with my knowldege of c++, here are my questions..

is foo a pointer to an object of NamedThing?


Yes.

Quote:
if so why isin't foo assigned and Instance of NamedThing, like we do in c++..


Normally it would be. I got lazy in this case. Smile

Quote:

code:

NamedThing *foo = new NamedThing();



now looking at the method header..
Quote:

- initWithName: (const char *) initName;


so - indicates an instance method (i prefer java terminology, makes things clear) and + indicates a static method?


Yes, though you shouldn't be thinking of them as "static methods". The SmallTalk object model, which Obj-C aims to incorporate, is very pervasive. Everything is an object, including classes. Due to the limitations of C, this isn't possible as much as in other SmallTalk-ish languages (such as the excellent Ruby).

Still, think of the class as an object, and "static methods" as instance methods for classes.

Quote:
initWithName is the name of the method?


Yes and no. Since it does have the one parameter, the name is -initWithName:. The - or + usually gets added on the front to make it clear which type of method it is.

Quote:
: seperates the method name from the parameters right? well what if my method has no parameters do i write the ':'?


No, you wouldn't. You can see this kind of style in things like:

code:
[NamedThing alloc]


Quote:
initName is a parameter of type (const char*)?


Exactly. See, it's really quite straightforward. Smile

Quote:
what if i want to write a method that accepts more than one argument?


Let's say we have a fictitious String class because we're in a reinventing the wheel kind of mood. Now we want to give it a method for finding some weird substring.

code:
- (MyString *) substringFrom: (int) startPos to: (int) endPos whereFirstCharIs: (char) firstChar andLastCharIs: (char) lastChar;


And the whole method would be documented as:

-substringFrom:to:whereFirstCharIs:andLastCharIs:

Certainly more clear what each variable does than something like:

code:
MyString * substringFromToWhereFirstCharIsAndLastCharIs(int startPos, int endPos, char firstChar, char lastChar);


Where you have to remember which parameters are in which order.

Quote:
... these are all the questions i have so far.. more to come later.. (probably)


Keep 'em coming.
rizzix




PostPosted: Sat Feb 14, 2004 12:49 pm   Post subject: (No subject)

With reference to your first example...

Quote:

...
@interface NamedThing : Object
{
@protected
char * name;
}
- initWithName: (const char *) initName;
@end
...

and
Quote:

...
Dog * fido = [[Dog alloc] initWithName: "Fido"];


the following questions:

alloc creates an instance of the class Dog right?

If so, here's how I interpret this chain method call: a new instance of Dog is returned of which the -initWithName: method is called, which has a return type of id? Hmm.. what exactly does fido now point to?

Also how would I write a constructor/destructor to this class?
wtd




PostPosted: Sat Feb 14, 2004 2:39 pm   Post subject: (No subject)

rizzix wrote:
With reference to your first example...

Quote:

...
@interface NamedThing : Object
{
@protected
char * name;
}
- initWithName: (const char *) initName;
@end
...

and
Quote:

...
Dog * fido = [[Dog alloc] initWithName: "Fido"];


the following questions:

alloc creates an instance of the class Dog right?

If so, here's how I interpret this chain method call: a new instance of Dog is returned of which the -initWithName: method is called, which has a return type of id? Hmm.. what exactly does fido now point to?


Fido is now a pointer to an object of class Dog.

Quote:
Also how would I write a constructor/destructor to this class?


Ah, but you're thinking in C++. Smile

Objective-C is a relatively simple addition to C (C++ is not). As such, it doesn't use operators like "new" and "delete". Instead memory allocation and deallocation is handled through methods like "+alloc" and "-free". The beauty of Objective-C is that these methods are implemented in Object, and since everything subclasses Object, everything automatically has them.

Yes, you can override them, but you don't have to write them yourself is you don't want to.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: