PDA

View Full Version : Ojects Question




larswik
Apr 16, 2008, 11:11 PM
I'm wrapping my head around Objects before I move forward in the book. I understand the Car concept and instance of the objects.

But are Objects empty containers that are created for storing types of variables, or are they objects that already have all the code embedded in them?

I think I know that the printf function can be used because I import that object/class in the beginning that already has all the code that I in that class. So, all I have to type is printf and it refers to all the code in the class which saves me thousands of lines of code that I would normally have to write if that class did not exist, right?

Also Classes and Objects are the same right?

Thanks,

-Lars



Catfish_Man
Apr 16, 2008, 11:23 PM
printf is a function, not an object. It's from C, which doesn't have objects at all.

An object is a collection of related properties and behaviors and (usually) access/visibility restrictions on some or all of those. A class is a template for instantiating new objects from. They're not the same*.


*this is not strictly true in two cases, but can be treated as true even in those without causing problems.

larswik
Apr 17, 2008, 12:00 AM
Still a little lost. printf is a function of C like you said. Some where all that code must be stored to tell my computer that it should do when it sees the printf function. Is that code located in C?

So in the section that I am reading it starts off by saying...

#import <stdio.h>
#import <objc/Object.h>

So none of that includes the info for the printf function.

I started to learn JAVA years ago and I remember that you had to import Objects or Classes (I forget) to use them. To use graphics I had to import javax.swing and then I could create windows, buttons and so on. Isn't the philosophy the same?

-Lars

HiRez
Apr 17, 2008, 12:04 AM
printf is a precompiled function that is built-in to the C language. You gain access to it by importing stdio.h. You aren't importing objects (because C doesn't know about objects), but you are importing code that your code can call into. If you look at the stdio.h header file (http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html), you'll see the printf function listed. You can't see the C code itself because it's already compiled into machine code, but the interface (.h) shows you what you can access.

larswik
Apr 17, 2008, 12:48 AM
HiREZ, thanks, that I understand and thanks for the link. I knew the code was some where and now I understand that the stdio.h it the doorway I guess you would say that allows me to access that code for printf. If I did not import the stdio.h then the printf function would have failed.

Thank you, that was exactly the clarification that I needed to better understand it.

-Lars

HiRez
Apr 17, 2008, 03:03 AM
If it helps, you can just think of C as one giant Utility class that is a collection of unrelated static methods (functions) that you can use, but are not associated with any object.

Spike099
Apr 17, 2008, 09:10 AM
Related to the classes vs objects.

Whenever I try to explain what the difference is, I say this.

Think about a memory block, like a stick of ram. In this memory block you must have a definition of how a object will look like, which is a Class. Now pretend you want 100 objects of this Class. Every single one of those 100 objects, is an Object. So... In memory, you'd have one Class and many 'instances' of it, called objects.

So classes define what variables and functions an object will have.

And you must remember that the C programming language does not implement the concept of objects, C is a procedural programming language. C++ and Objective-C are supersets of the C language, and include the concept of Objects.

lee1210
Apr 17, 2008, 09:29 AM
I'll toss my 2 cents in as well...

In C you can have a struct. It's a collection of data elements that are related. You have your structure definition, then instances of your struct. You can have functions that act on your struct. Every element of the struct can be accessed and modified. You can have pointers to functions in a struct if you needed to, and they could do things to the struct as long as it was passed in.

A class extends the usefulness of a struct with things like access levels on variables they contain. They also have functions that are internal to the object and can act on an objects variables without having the object passed to them. Classes also open up the possibility of inheritance and polymorphism which structs can't really do, though you can get pretty creative with unions and pointer casts.

-Lee