Struct vs. Classes?

This is a discussion on Struct vs. Classes? within the C++ Programming forums, part of the General Programming Boards category; What exactly is the difference between a struct and a class? What are the advantages/disadvantages of one over the other?...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Struct vs. Classes?

    What exactly is the difference between a struct and a class?
    What are the advantages/disadvantages of one over the other?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One has private members, one has public members. Read your book.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    I am reading my book, and I found a code example with a struct with both private and public members...
    And I already know a class can have private, protected, and public members, so what is the difference? Can a struct not have protected members, or derived structs?

    Why would one use a struct instead of a class? Do they allocate different sizes of memory, maybe the class more than the struct?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You book should have already explained the difference. They are identical except for the default scope.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Right. I think it did say something about that, but I forgot it until you mentioned it.
    (Btw...do you know what book I'm reading?)

    So...the default scope for a class is public, and the default scope for a struct is private?

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Programmer_P View Post
    So...the default scope for a class is public, and the default scope for a struct is private?
    Opposite.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ahh, ok, thanks.

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Structs cannot have methods other than a constructor I believe....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by jeffcobb View Post
    Structs cannot have methods other than a constructor I believe....
    Um, no?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jeffcobb View Post
    Structs cannot have methods other than a constructor I believe....
    Fail!
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by cpjust View Post
    Fail!
    Sorry, that is all I have ever seen in a struct....and it was just initializing members. I have seen structs with function pointers before but that was a syntactic mess.....so is your FAIL that they cannot have ctors or that they cannot have anything or they can have everything?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    The ONLY difference is the default scope.

    A struct can have anything a class can have.

  13. #13
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I'll be, y'all are right. I have not seen structs used in such a fashion but GCC has no problem with it. Learn something new everyday...

    ^__^
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,458
    To elaborate: a struct is a class for which bases and members have public access by default; a class is a struct for which bases and members have private access by default. Often a convention is adopted such that the struct keyword is reserved for defining POD structs or a simple aggregation of objects.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

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