Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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, 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

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
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

macrumors regular
Feb 18, 2007
143
0
Canada
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.