Thread: struct vs class

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    struct vs class

    ok i know struct is from c, but struct are so similar to classes...you can declare:

    Code:
    struct person
    {
      // can have constructors
      int age;
      int height;
      // can have member functions
    };
    
    person me;
    me.age = 18;
    well doesnt that create an object from the struct person...and if struct was from the old C language...isnt that object oriented programming.....? dont mind my stupidity.....but please answer.

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    Here's the bottom line:

    In C++ classes and structs are essentially the same, structs just default to public access and were included for compatibility with old C code.

  3. #3
    Classes allow for inheritance, polymorphism, privacy.

    Structs are meant to only hold data values while Classes were designed to hold both data and functions - you can put functions in structs but I don't think it is very effecient.

  4. #4
    I usually use structs if I'm organizing some variables together and classes for things like units in a game or something.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    in C++ structs and classes are EXACTLY the same thing, EXACTLY, but structs default to public access and in classes you can define public and private. other than that they are the same damn thing.

  6. #6
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Classes allow for inheritance, polymorphism, privacy.
    So do C++ structs

    Structs are meant to only hold data values while Classes were designed to hold both data and functions
    They're not "meant" for anything... you can use them for whatever you please; but most just use them for raw data... probably a tribute to what they were in C. Personally I like to include a constructor and destructor in structs for the terse syntax.
    you can put functions in structs but I don't think it is very effecient.
    Efficiency has nothing do do with it (but I doubt that's what you meant) Most expect raw data when they see struct, so i'd just go along with tradition in an effort not to confuse anyone.

  7. #7
    Originally posted by Captain Penguin
    in C++ structs and classes are EXACTLY the same thing, EXACTLY, but structs default to public access and in classes you can define public and private. other than that they are the same damn thing.
    It just seems like you skimmed a great point in their differents. You said that structs default to public but you didn't really express that classes default to private.

  8. #8
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    _

    If you use typedef in C you can avoid having to add that struct specifier.

    Code:
    typdef struct {
        int x, y;
    } point2d_t;
    
    /* ... */
    point2d_t p;

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Originally posted by OneStiffRod
    Classes allow for inheritance, polymorphism, privacy.

    Structs are meant to only hold data values while Classes were designed to hold both data and functions - you can put functions in structs but I don't think it is very effecient.
    In C++ structs and classes are identical except for the default access mentioned above.
    classes by default are private. structs by default are public.

    Also see this thread

    -Futura
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05:05 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM