Can any one have any idea about Difference between structure and class in C++


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: Can any one have any idea about Difference between structure and class in C++

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    17

    Unhappy Can any one have any idea about Difference between structure and class in C++

    Hi All,

    Can any one have any idea about this issue

    what is Difference between structure and class in C++
    Other then by default structure have public scope and class have private scope For data member and member function .If there is no other difference between Class & structure then why we use 2 key world in C++ we can use either class or structure in C++ ?

  2. #2
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    There is no other difference. Everything that works in a C++ class will also work with a C++ struct. They are both class declarators. But using class will be more understandable, as people may confuse struct with C structs. And use struct when you do not want member functions (and there would not arise any need for introduction of member functions later).

    But apart from that issue of people getting confused (which is not a big problem, as anyone who knows C++ well enough will know it from a single glance that both are the same), I don't think there is any reason to prefer one over another.

    As for 2 different keywords... maybe they intended to make it clear that C++ classes are something different from the earlier structs. Or maybe for the meaning that the word 'class' implies, and didn't want to reduce compatibility with C programs.

  3. #3
    Join Date
    May 2007
    Posts
    843
    There are no obvious between the these two things. By the way, class are mor epreffered method because it is easy to maintain

  4. #4
    Join Date
    Dec 2003
    Posts
    3,366
    that makes no sense to me. If they are the same except for the arrangement of public / private keywords, why would one be easier to maintain?

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    There's one more difference: the default inheritance type of class is private and for struct it's public.

    A second differences between the keywords struct and class:
    you can only use class in a template declaration:

    Code:
    template <struct T> int func(T t);  //error, can't use struct here
    Last edited by Danny; 12-06-2007 at 11:34 AM.
    Danny Kalev

  6. #6
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    But here, class in not a class declarator. Here, instead of class, you could use the typename keyword.

    Code:
    template <typename T> int func( T t);
    In this context, typename and class keywords have exactly the same meaning, without any change in defaults. Note, they have the same meaning only within
    template<xxx T> ... .

    Is it another redundancy? Atleast in the class / struct for class declarators, there was a difference in the default scope of variabled declared in it and default inheritance.

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    I find the addition of the keyword class to be redundant, is that what you mean? C had struct, and it was very different, but when they made the c++ struct and then added a class keyword on top of it, that was just politics/hype -- the language would work just fine with only one of the two items.

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    jonnin's right. class was redundant right from the start at least as far as compilers are concerned. The problem is that C++ creators wanted to give the impression that in an OO environment, structs in the C sense are obsolete, so they came up with a new keyword. It was mostly a PR thing.There was another reason: all other OO languages have classes so C++ couldn't stay behind.
    typename was added to C++ much later not for replacing class in template declarations but mostly for disambiguating dependent names in templates. It was later decided that typename would also serve as a substitute for 'class' in a template-parameter list, hence the redundancy of template <class T> and template <typename T>
    Danny Kalev

Similar Threads

  1. How to make a hierarchical structure Class
    By maximas2000 in forum VB Classic
    Replies: 2
    Last Post: 09-29-2007, 12:21 PM
  2. Difference between c++ structure and class
    By abhishek4soluti in forum C++
    Replies: 7
    Last Post: 12-14-2006, 01:40 PM
  3. Replies: 3
    Last Post: 04-26-2006, 05:20 AM
  4. Replies: 10
    Last Post: 09-26-2005, 09:43 PM
  5. Replies: 5
    Last Post: 10-17-2002, 02:58 PM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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