Difference between c++ structure and class

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Difference between c++ structure and class

  1. #1
    Join Date
    Dec 2006

    Difference between c++ structure and class

    Hi all, can anyone clear my confusion why we do not use structure in c++ in place of class? Structure also provide us inheritance and polymorphism and can also be used in place of class then why we prefer class in place of structure.
    There must be some benefit using it.What are those?

  2. #2
    Join Date
    Sep 2006
    The main difference between structs and classes is that the members of a struct have public visibility by default, and the members of a class have private visibility by default, so because of the private visibility, the class encourages the use of encapsulation (Information Hiding), which is important in OOP.

    there may be many others....... :)

  3. #3
    Join Date
    May 2006
    I know two differences between classes and structures:

    • The default protection is public for structures and private for classes:

    struct MyStruct
       int a; // a is public
    class MyClass
       int a; // a is private
    • The default inheritance is public for structures and private for classes:

    struct MyOtherStruct : MyStruct 
       // the same as "struct MyOtherStruct : public MyStruct"
    class MyOtherClass : MyClass 
       // the same as "class MyOtherClass : private MyClass"
    I think there are no other differences. But I cannot explain why they both coexist. Usually structures are used for old-fashioned data structures having no member functions.
    Last edited by Viorel; 12-13-2006 at 07:35 AM.

  4. #4
    Join Date
    Sep 2006
    As i said earlier, using classes we can easily achieves the encapsulation, so it's ideal for the object oriented programming compared to struct.

  5. #5
    Join Date
    Dec 2003
    Since you can override the public and private with the keywords, they are basically the same. They both exist because other OOP languages used "class", C already called them struct. Also, I think the authors wanted to tout the changes from C to C++ and did silly things like this to make it look more different to say "this is not C" boldly.

  6. #6
    Join Date
    Nov 2003
    There's no substantial difference betwee the two except for their default access. structs are public whereas classes are private by default, both with respect to members and inheritance. That said, the keyword class has a few other uses that struct doesn't support. For example, a template declaration uses the keyword class (or typename) to denote a template parameter:
    template <class T> T max(T t1 T t2);
    Danny Kalev

  7. #7
    Join Date
    Oct 2005
    What about protected ? if the "editor" allow u to use it under struct , it will send it to compiler like class ... same as when assinge 2.5 to float (not 2.5F) which is not a literal and the editor auto correct it without mention the correction .

    ***I'm talking about c++ , I havent a place in my mind to remember what I'm was doing in c .
    Programmer&Cracker CS

  8. #8
    Join Date
    Nov 2003
    protected is never the default access. You have to specify it explicitly in either case (struct or class), so it's the same as public and private.
    protected data members are always a bad idea. Protected member functions are useful when you have a deep hierarchy of inheritance, where only classes that belong to the same hierarchy are allowed to call/override a certain member function but other classes may not.
    Danny Kalev

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2006, 04:20 AM
  2. Fairly long question about class design
    By PeterS2 in forum C++
    Replies: 6
    Last Post: 02-25-2006, 09:53 AM
  3. Replies: 10
    Last Post: 09-26-2005, 08:43 PM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Abstract class
    By Manika in forum Java
    Replies: 1
    Last Post: 09-06-2000, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center