Thread: structure vs. class

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    structure vs. class

    I have read that the structure and classes have the same capability.

    e.g. it can have data members and member functions, and you can also create an object of the class and structure.

    I don't know if the information I read is complete or not but here I am formulated a question between structure and classes.

    Since structure and classes have the same capability, why C++ is provided such feature that has the same capability? which one is better to use to create an object the class or the structure? This causes confusion somehow. Or is there any difference between?

    Thanks,

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by $l4xklynx
    I have read that the structure and classes have the same capability.
    A structure is a class for which access and inheritance is public by default.

    Quote Originally Posted by $l4xklynx
    which one is better to use to create an object the class or the structure?
    They are both equally good. That said, sometimes (often?) a convention is used where struct is used to define a POD structure or a simple aggregate whereas class is used otherwise (i.e., for "normal" object oriented programming as opposed to more C-style flavour).
    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

  3. #3
    a newbie :p
    Join Date
    Aug 2008
    Location
    Zurich, Switzerland, Switzerland
    Posts
    91
    they are the same except for the default public vs default private issue...
    i think it is only convention problem..
    i always make my struct contain no function...

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    I see, so I should not confuse myself between classes and structure in C++ I could use both. In my discretion I will use class instead of struct since I'm using C++, would that be alright?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I, myself, would make a distinction between the two.
    Structs are the legacy-old C-types, and classes are the new C++ OOP-style types.
    Some would disagree, but this is how I see it, and this is how I program with them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I, myself, would make a distinction between the two.
    But then... you would make a choice between two different 'string' classes based solely on which one has the most methods. Who cares what you think?

    Soma

    (I'm just poking you in the ribs... so to speak.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hehe, well, the truth may be that some do the distinction and some do not. I believe I have witnessed both. The majority of this board seem to use classes instead of structs.
    So I thought I'd just give my opinion in the whole and let everyone select what they think...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I use structs when the earth, moon, and sun line up.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Structure vs. Class
    By bobbelPoP in forum C++ Programming
    Replies: 4
    Last Post: 07-09-2008, 09:44 AM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  5. Converting a C structure into a C++ class
    By deadpoet in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 02:06 PM