-
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++ ?
-
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.
-
There are no obvious between the these two things. By the way, class are mor epreffered method because it is easy to maintain
-
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?
-
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 10:34 AM.
Danny Kalev
-
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.
-
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.
-
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
-
By maximas2000 in forum VB Classic
Replies: 2
Last Post: 09-29-2007, 11:21 AM
-
By abhishek4soluti in forum C++
Replies: 7
Last Post: 12-14-2006, 12:40 PM
-
Replies: 3
Last Post: 04-26-2006, 04:20 AM
-
Replies: 10
Last Post: 09-26-2005, 08:43 PM
-
Replies: 5
Last Post: 10-17-2002, 01: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
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|