Thread: Class in C

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    23

    Class in C

    Hi all, I am not sure with C about one thing, I know that C is not an Object-oriented language.
    so it means we cannot implement any class by using C?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Classes aren't supported by C. If you want them, you'll have to use C++.
    (But structs are supported.)

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    C doesn't have classes. It has the simple precursor to classes -- structs.
    In C, a struct can only have data (no functions), although you can have pointers to functions. It therefore has no constructors & destructors... Also, the data in a struct is all public. It's mostly just a container to hold multiple data variables in one package.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    31

    You can use Objective-C

    You CAN use objects in C with Objective-C.

    Objective-C (or objc as it is sometimes written) is a superset of C. This means that it IS C, but with objects added. You don't even have to use objects in objc, you a totally free to choose when to use a procedural C approach to a problem or when to change to an object orientate approach.

    In objective-C you always refer to an object by a pointer. For example, if I had a 'Person' object, you would send methods to it like this,

    Code:
    Person *myPersonObject = [Person new];
    [myPersonObject setName:"twonk"];
    const char* personName = [myPersonObject name];
    In objc the square brackets are used only when the contents are objects (pointer to objects) and methods, i.e.

    <result> = [<object> <method>].

    If the method takes arguments, then they come after an colon, i.e.

    <result> = [<object> <method with parameters>:<parameter>].

    In the above example, I made a new Person object by sending the class 'Person' the 'new' method. I then set the name of the object to "twonk" by using a C string as a parameter, I then asked person object for its instance variables 'name' (which I just set).

    The code to define objects in objc looks like this,

    Code:
    // all for this goes in a file called Person.h
    #import <object.h>
    
    @interface Person : Object
    {
        const char *name;
    }
    
    - (const char*) name;
    
    
    @end
    This is the 'class interface'. The instance variables are defined separately from the methods. The instance variables are defined in the curly brackets, the methods defined afterwards. Instance methods follow the pattern,

    - (<return type>) <method name>;

    The hyphen indicates the start of the method, followed by the return type (e.g. float, double, or even another object) in parentheses, finally we have the method name and end with a semicolon.

    Next we define the 'class implementation',

    Code:
    //all of this goes in a file called Person.m  the .m indicated an ObjC file.
    #import "Person.h"
    
    @implementation Person
    
    - (const char*) name 
    {
        return name;
    }
    
    @end
    The implementation is very similar to the interface apart from it a bit simpler. We get rid of the semicolon and add curly brackets to the method name.

    A good introduction to the topic is here, http://www.otierney.net/objective-c.html

    And don't forget, the native MacOS programming language is objc. You use iTunes, it's built with (at least in part) Objective-C. Objective-C lies at the heart of the iPhone and iPod Touch's interfaces. The first web browser was built using objective-c. Doom was coded in Objective-C ... it's a good language ;o)

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    But since this is a C/C++ board that doesn't mean much.

    I'm probably going to get flamed for saying this but a C struct is nearly identical to a C++ class. The ONLY difference between a C++ class and a C struct is that by default all members of a C struct are public and all members of a C++ class are private. I won't further this explanation for fear of seeing some hideous abuse of structures on the board.

    The traditional use of structs is for data blocks such as file headers, etc.


    I failed that question in my interview by the way.
    Last edited by VirtualAce; 12-01-2007 at 12:46 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Err, struct is almost a class. I can't see how. Classes are (can be) indefinitely much more complex than structs with virtual functions, public, protected, private, etc.
    C++ classes can be used for so much more than a C struct. Polymorphism, for example.
    I don't think I agree with that statement at all.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm probably going to get flamed for saying this but a C struct is nearly identical to a C++ class. The ONLY difference between a C++ class and a C struct is that by default all members of a C struct are public and all members of a C++ class are private.
    True, but "C struct" in this case actually means "C++ struct". As I understand it, a C struct is identical to a POD C++ struct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yes I'm referring to a C++ struct. In pure C using any C++ reserved keywords will cause the compiler to barf.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by boyfarrell View Post
    You CAN use objects in C with Objective-C.
    ...
    Well I know absolutely nothing about Objective C, but is there anything in Objective C that can't be done as well or better in C++?

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    C++ classes and specifics are done using under-the-hood C techniques. Function pointers and such. Nothing too fancy. Nothing brand new. Just more work underneath so we can do more for less on top.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by cpjust View Post
    Well I know absolutely nothing about Objective C, but is there anything in Objective C that can't be done as well or better in C++?
    I doubt it. It seems just to lack functionality that C++ has (like virtual functions), instead of adding to it. It's kinda like C+ it seems.

  13. #13
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MacGyver View Post
    C++ classes and specifics are done using under-the-hood C techniques. Function pointers and such. Nothing too fancy. Nothing brand new. Just more work underneath so we can do more for less on top.
    The only class functionality I haven't found a way to duplicate in C are constructors & destructors. You can add function pointers to them, but they won't get called unless you explicitly call them.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    I don't know anything about C++ so I can't answer that question, have a look here, C++ and Objective-C Compared.

    But as far I'm aware it is an object orientated language in the style of smalltalk and it adds a very small runtime onto of C which which makes structs behave as objects (inheritance, polymorphism etc..). Objects have a different syntax (as I described above) to the rest of the language. It is known for it very dynamic nature (it is weakly typed). The only think I would like to see is operator overloading, but that is because I do a lot of science stuff.
    Last edited by boyfarrell; 12-02-2007 at 07:54 PM. Reason: typo

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by boyfarrell View Post
    I don't know anything about C++ so I can't answer that question, have a look here, C++ and Objective-C Compared.
    I've never used or seen Smalltalk, but I found this quote pretty funny:
    C++ has tons of features not found in Objective-C. Objective-C is a simpler language. Objective-C tries to blend the purity of Smalltalk with the simplicity of C. However, some people have said that Objective-C blends the lack of readability of C with the limitations of Smalltalk.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM