Classes and Structures.

This is a discussion on Classes and Structures. within the C++ Programming forums, part of the General Programming Boards category; I read that classes and structures are completely the same, after learning that you should use structures for data, and ...

  1. #1
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256

    Question Classes and Structures.

    I read that classes and structures are completely the same, after learning that you should use structures for data, and classes for both data and functions. The only difference from what i read between classes and structures is that structures are public by default and classes are private by default. If this is the only difference does it matter which one you use?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If this is the only difference does it matter which one you use?
    Aside from default permissions, the only difference is in your mind. Use whatever makes more sense. I usually use classes for full data types and structures for little things, like linked list nodes.
    My best code is written with the delete key.

  3. #3
    Set Apart -- jrahhali's Avatar
    Join Date
    Nov 2002
    Posts
    256
    Ok, thanks. What are full data types?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What are full data types?
    I see I used potentially confusing terminology. A class would be used for all but the simplest objects. So while a struct would be used for a linked list node, a class would be used for the linked list.
    My best code is written with the delete key.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Also with structures, I do not think that they come with a default constructor, so there is no way to initalise your variables declared within your structure when you declare an instance of the structure. Therefore, classes are safer to use as you can always make sure the variables are initalised before you start doing any work on the data.
    Be a leader and not a follower.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I do not think that they come with a default constructor
    Aside from the default permissions, structures are exactly the same as classes. That includes the default constructor.
    My best code is written with the delete key.

  7. #7
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Ok, I stand corrected. I wonder where I read that anyways? Ah well.
    Be a leader and not a follower.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about classes and structures.
    By RealityFusion in forum C++ Programming
    Replies: 19
    Last Post: 08-30-2005, 04:54 PM
  2. Replies: 1
    Last Post: 06-17-2005, 08:31 AM
  3. Pointers to Classes || pointers to structures
    By C++Child in forum C++ Programming
    Replies: 24
    Last Post: 07-30-2004, 07:14 PM
  4. Structures, Unions and Classes
    By Makoy in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2004, 02:57 PM
  5. structures fooloing to look like classes
    By samsam1 in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2003, 11:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21