Thread: Structs in C vs classes in C++

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    83

    Structs in C vs classes in C++

    Hi,

    What's the difference between a class in C++ and a struct in C?
    They seem to be very similar to each other. If they are so similar why can't OOP be used in C? Then what's the point of C++?

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by jjohan View Post
    Hi,

    What's the difference between a class in C++ and a struct in C?
    They seem to be very similar to each other. If they are so similar why can't OOP be used in C? Then what's the point of C++?
    Sounds like a flame bait , if not, I think your question is *tooo* general for any good answers.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    It's not a flame bait. I'm new as a c programmer and I started to wonder when I started to read about structs yesterday. Some fast googling seems to show that there's no big difference (except some default thing about private and public).

  4. #4
    Registered User
    Join Date
    Dec 2011
    Location
    Namib desert
    Posts
    94
    Just continue with some "fast googling"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A C++ class is a C++ struct, the only functional difference being that default access of members is public for a struct and private for a class.

    Access control is not the only difference between C++ struct and a C struct. Others include the option for the programmer of deriving one from another, having member functions, better control of how an instance is initialised (constructors), assigned (assignment operators), and destroyed (destructor). More advanced differences include each C++ struct type being associated with a distinct scope, and that C structs cannot have members which are C++ structs (such code cannot be compiled as C).

    All of those features (I'm sure I've missed some) make various programming styles/approaches/techniques significantly easier in C++ and C. Including OOP.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    83
    Thank you.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    When talking about the differences between a struct and a class in C++, the only real difference is the access mode. However since we are in the C forum there are many other differences that need to be taken into account. First and foremost, there is no "class" in C, this is a C++ construct. C++ allows, and encourages member functions in both classes and structures, C doesn't allow member functions.

    Remember C and C++ are quite different languages they share the same base language, but what is considered a good practice in C is not necessarily a good practice in C++, and the reverse is also true.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Classes vs Structs
    By afesheir in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2011, 01:52 PM
  2. Structs or classes?
    By legit in forum C++ Programming
    Replies: 4
    Last Post: 06-28-2009, 10:16 AM
  3. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  4. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 03:58 AM
  5. Structs in classes
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2002, 11:54 AM