Hi all, I am not sure with C about one thing, I know that C is not an Object-oriented language.
so it means we cannot implement any class by using C?
Hi all, I am not sure with C about one thing, I know that C is not an Object-oriented language.
so it means we cannot implement any class by using C?
Classes aren't supported by C. If you want them, you'll have to use C++.
(But structs are supported.)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
C doesn't have classes. It has the simple precursor to classes -- structs.
In C, a struct can only have data (no functions), although you can have pointers to functions. It therefore has no constructors & destructors... Also, the data in a struct is all public. It's mostly just a container to hold multiple data variables in one package.
You CAN use objects in C with Objective-C.
Objective-C (or objc as it is sometimes written) is a superset of C. This means that it IS C, but with objects added. You don't even have to use objects in objc, you a totally free to choose when to use a procedural C approach to a problem or when to change to an object orientate approach.
In objective-C you always refer to an object by a pointer. For example, if I had a 'Person' object, you would send methods to it like this,
In objc the square brackets are used only when the contents are objects (pointer to objects) and methods, i.e.Code:Person *myPersonObject = [Person new]; [myPersonObject setName:"twonk"]; const char* personName = [myPersonObject name];
<result> = [<object> <method>].
If the method takes arguments, then they come after an colon, i.e.
<result> = [<object> <method with parameters>:<parameter>].
In the above example, I made a new Person object by sending the class 'Person' the 'new' method. I then set the name of the object to "twonk" by using a C string as a parameter, I then asked person object for its instance variables 'name' (which I just set).
The code to define objects in objc looks like this,
This is the 'class interface'. The instance variables are defined separately from the methods. The instance variables are defined in the curly brackets, the methods defined afterwards. Instance methods follow the pattern,Code:// all for this goes in a file called Person.h #import <object.h> @interface Person : Object { const char *name; } - (const char*) name; @end
- (<return type>) <method name>;
The hyphen indicates the start of the method, followed by the return type (e.g. float, double, or even another object) in parentheses, finally we have the method name and end with a semicolon.
Next we define the 'class implementation',
The implementation is very similar to the interface apart from it a bit simpler. We get rid of the semicolon and add curly brackets to the method name.Code://all of this goes in a file called Person.m the .m indicated an ObjC file. #import "Person.h" @implementation Person - (const char*) name { return name; } @end
A good introduction to the topic is here, http://www.otierney.net/objective-c.html
And don't forget, the native MacOS programming language is objc. You use iTunes, it's built with (at least in part) Objective-C. Objective-C lies at the heart of the iPhone and iPod Touch's interfaces. The first web browser was built using objective-c. Doom was coded in Objective-C ... it's a good language ;o)
But since this is a C/C++ board that doesn't mean much.
I'm probably going to get flamed for saying this but a C struct is nearly identical to a C++ class. The ONLY difference between a C++ class and a C struct is that by default all members of a C struct are public and all members of a C++ class are private. I won't further this explanation for fear of seeing some hideous abuse of structures on the board.
The traditional use of structs is for data blocks such as file headers, etc.
I failed that question in my interview by the way.
Last edited by VirtualAce; 12-01-2007 at 12:46 PM.
Err, struct is almost a class. I can't see how. Classes are (can be) indefinitely much more complex than structs with virtual functions, public, protected, private, etc.
C++ classes can be used for so much more than a C struct. Polymorphism, for example.
I don't think I agree with that statement at all.
True, but "C struct" in this case actually means "C++ struct". As I understand it, a C struct is identical to a POD C++ struct.I'm probably going to get flamed for saying this but a C struct is nearly identical to a C++ class. The ONLY difference between a C++ class and a C struct is that by default all members of a C struct are public and all members of a C++ class are private.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yes I'm referring to a C++ struct. In pure C using any C++ reserved keywords will cause the compiler to barf.
C++ classes and specifics are done using under-the-hood C techniques. Function pointers and such. Nothing too fancy. Nothing brand new. Just more work underneath so we can do more for less on top.
I don't know anything about C++ so I can't answer that question, have a look here, C++ and Objective-C Compared.
But as far I'm aware it is an object orientated language in the style of smalltalk and it adds a very small runtime onto of C which which makes structs behave as objects (inheritance, polymorphism etc..). Objects have a different syntax (as I described above) to the rest of the language. It is known for it very dynamic nature (it is weakly typed). The only think I would like to see is operator overloading, but that is because I do a lot of science stuff.
Last edited by boyfarrell; 12-02-2007 at 07:54 PM. Reason: typo
I've never used or seen Smalltalk, but I found this quote pretty funny:
C++ has tons of features not found in Objective-C. Objective-C is a simpler language. Objective-C tries to blend the purity of Smalltalk with the simplicity of C. However, some people have said that Objective-C blends the lack of readability of C with the limitations of Smalltalk.