structure vs. class

This is a discussion on structure vs. class within the C++ Programming forums, part of the General Programming Boards category; I have read that the structure and classes have the same capability. e.g. it can have data members and member ...

  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
    21,660
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    90
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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
    4,231
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,554
    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,183
    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, 10: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, 08:54 AM
  5. Converting a C structure into a C++ class
    By deadpoet in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 01:06 PM

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