Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 17, 2008, 12:11 AM   #1
larswik
macrumors 65816
 
Join Date: Sep 2006
Ojects Question

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
larswik is offline   0 Reply With Quote
Old Apr 17, 2008, 12:23 AM   #2
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
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.
Catfish_Man is offline   0 Reply With Quote
Old Apr 17, 2008, 01:00 AM   #3
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
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
larswik is offline   0 Reply With Quote
Old Apr 17, 2008, 01:04 AM   #4
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Apr 17, 2008, 01:48 AM   #5
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
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
larswik is offline   0 Reply With Quote
Old Apr 17, 2008, 04:03 AM   #6
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: 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.
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Apr 17, 2008, 10:10 AM   #7
Spike099
macrumors regular
 
Join Date: Feb 2007
Location: 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.
__________________
MacBook 4GBRam 320GBHDD
iMac 20" 4GBRam 1TBHDD
iPhone 16GB
Cocoa Developer
Spike099 is offline   0 Reply With Quote
Old Apr 17, 2008, 10:29 AM   #8
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Mophie Juice Pack Plus (iPhone 5) Question - Mute Button Question JulesK iPhone Accessories 3 Mar 30, 2014 03:58 PM
Snow Leopard File Vault Question and Basic System Password Question & Time Machine? GordonGekko999 Mac Basics and Help 0 Oct 25, 2013 07:06 AM
Question for anyone who has seen or owns the pink ipod nano? Color deciding question. Pinkstiletto66 iPod 4 Apr 4, 2013 01:57 PM
Family Share Question, Upgrade Question, Argh head hurting.. Jazwire iPhone 1 Sep 14, 2012 01:19 AM
Time Capsule - Newbie Question Wifi Question Alper1234 Mac Peripherals 1 Jun 23, 2012 10:17 AM

Forum Jump

All times are GMT -5. The time now is 03:44 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC