1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Define "class"

Discussion in 'Software Development' started by kenneth2k1, Aug 3, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. kenneth2k1

    kenneth2k1 Thread Starter

    Joined:
    Oct 9, 2003
    Messages:
    252
    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.
     
  2. AGCurry

    AGCurry

    Joined:
    Jun 15, 2005
    Messages:
    431
    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.
     
  3. kenneth2k1

    kenneth2k1 Thread Starter

    Joined:
    Oct 9, 2003
    Messages:
    252
    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.
     
  4. kenneth2k1

    kenneth2k1 Thread Starter

    Joined:
    Oct 9, 2003
    Messages:
    252
    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?
     
  5. blaqDeaph

    blaqDeaph

    Joined:
    Nov 22, 2005
    Messages:
    869
    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".
     
  6. kenneth2k1

    kenneth2k1 Thread Starter

    Joined:
    Oct 9, 2003
    Messages:
    252
    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.
     
  7. blaqDeaph

    blaqDeaph

    Joined:
    Nov 22, 2005
    Messages:
    869
    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.
     
  8. AGCurry

    AGCurry

    Joined:
    Jun 15, 2005
    Messages:
    431
    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
     
  9. blaqDeaph

    blaqDeaph

    Joined:
    Nov 22, 2005
    Messages:
    869
    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.
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/489130

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice