There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Define "class"


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
kenneth2k1's Avatar
Senior Member with 249 posts.
 
Join Date: Oct 2003
Location: San Diego
03-Aug-2006, 03:04 PM #1
Define "class"
I'm new to C, and I'm on Apple's development site going through their tutorial thing. I'm reading about "object models" and I can understand some of these terms, but I am not wrapping my head around the idea of a class. And it's probably because of these stupid types of definitions. I don't get it, so perhaps someone could explain it in dumber terms.

class In the Objective-C language, a prototype for a particular kind of object. A class definition declares instance variables and defines methods for all members of the class. Objects that have the same types of instance variables and have access to the same methods belong to the same class.


Thanks for any help, and any other info you want to give a beginning C developer.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
04-Aug-2006, 10:12 AM #2
Classes are really not an integral part of C programming, but they are a part of C++ and Java and any other object-oriented language.

Do you know what a "struct" is in C? A struct is a structure, which in even older languages we call a record. It may contain scalar variables, arrays, other structs, and pointers.

In C we can use pointers to functions. It's kind of an advanced concept and may be a little hard to wrap your head around, but if you think about how a program looks when it's in memory, a function resides at a certain address, just like a variable. So, we can create a variable which is a pointer to that address, and we call it a pointer to a function.

So, it wasn't long before programmers began to include these pointers to functions in structs. In that way, you can "wrap" functions related to the data in the struct inside the struct itself.

This works perfectly well, but some programmers wanted to implement this capability in a more programmer-friendly way, so the concept of the class was developed as a wrapper, to kind of hide the ugly nuts and bolts of implementation.

As a dyed-in-the-wool plain old C programmer, it helps me to understand classes in this way - as an implementation of pointers to functions. In the object-oriented world, these functions are called methods.

The final piece of the puzzle involves the difference between "definition" and "instantiation."
In a C program, when I code:

struct something
{
int c ;
char d ;
} ;

This defines what struct something looks like, but it does not result in an INSTANCE of a struct something. This is analogous to coding a class, or to putting a new word in the dictionary before it has actually ever been used.

To make an INSTANCE of the struct that, I must code:

struct something something_instance ;

This is a little bit of an oversimplification, but not by much. Hope it helps.

Last edited by AGCurry : 04-Aug-2006 10:21 AM.
kenneth2k1's Avatar
Senior Member with 249 posts.
 
Join Date: Oct 2003
Location: San Diego
04-Aug-2006, 01:01 PM #3
Well that is a better explanation. I am starting to see how it fits. I think once I start programming I will see a lot better, which should be today sometime. Thanks.
kenneth2k1's Avatar
Senior Member with 249 posts.
 
Join Date: Oct 2003
Location: San Diego
04-Aug-2006, 03:06 PM #4
Ok, so I'm writing my first program, and the compiler keeps returning an error. Since I just kinda jumped into this thing, perhaps you can help me. I wrote a simple program the way it was described to me here's the lines of code:

#include <stdio.h>
void main()
{
printf("Hello World");
}

When I compile it, I get an error that says:

'main' must return 'int'
so I delete void and put int in the parenthesis. No errors are returned, but then the program doesn't execute anything. My guess is that if it's supposed to return an integer, it's not going to do anything because I'm not outing any integers, right?
blaqDeaph's Avatar
Senior Member with 860 posts.
 
Join Date: Nov 2005
Location: Down Under!
Experience: Enough
08-Aug-2006, 08:56 AM #5
your best bet is to make it "int main()", and add a "return 1;" as the second line of the function.

And BTW, the only real difference between a struct and class in C & C++ is that with a class you can control access to variables and functions by using "public", "private", and "protected".
__________________
Windows XP: 64-bit wanna-be OS with a 32-bit graphical shell for 16-bit extensions of a 8-bit patch to an 4-bit operating system originally coded for a 2-bit microprocessor, written by a company that can't stand 1 bit of competition.
kenneth2k1's Avatar
Senior Member with 249 posts.
 
Join Date: Oct 2003
Location: San Diego
08-Aug-2006, 06:57 PM #6
Thanks for the info on class. I am going through the tutorial that came with the compiler program I got, it's a little easier to follow. I changed the code to read like this:

#include<stdio.h>
main()
{
printf("Hello World\n");
}

That's a hell of a lot easier to understand to me.

I'm on to loops now, and things are going nicely.
blaqDeaph's Avatar
Senior Member with 860 posts.
 
Join Date: Nov 2005
Location: Down Under!
Experience: Enough
10-Aug-2006, 09:17 AM #7
This int and return 1 thing is another left over from the old DOS days, where programs returned whether or not they were completed successfully. The number used to (not sure if it's still) be stored under ERRORLEVEL 255 which the OS (or other subsequent programs) would then access.
__________________
Windows XP: 64-bit wanna-be OS with a 32-bit graphical shell for 16-bit extensions of a 8-bit patch to an 4-bit operating system originally coded for a 2-bit microprocessor, written by a company that can't stand 1 bit of competition.
AGCurry's Avatar
Senior Member with 431 posts.
 
Join Date: Jun 2005
Location: Kansas City area
Experience: advanced but learning
10-Aug-2006, 09:49 AM #8
Quote:
Originally Posted by blaqDeaph
This int and return 1 thing is another left over from the old DOS days, where programs returned whether or not they were completed successfully. The number used to (not sure if it's still) be stored under ERRORLEVEL 255 which the OS (or other subsequent programs) would then access.
This is still the preferred approach. All programs should return an exit status. However, the convention is to return 0 for success and nonzero for errors. This is a UNIX convention, which allows shell coding like:

if myprogram
then
do_something
else
echo "An error occurred in myprogram"
fi
blaqDeaph's Avatar
Senior Member with 860 posts.
 
Join Date: Nov 2005
Location: Down Under!
Experience: Enough
12-Aug-2006, 04:47 AM #9
Yea, DOS would be a little more complicated, if my memory serves me right, you have to call the program, then test for the errorlevel, instead of directly testing the program.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 05:21 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.