difference between a c++ structure and class
what is the difference between a C++ structure and a class?
What comes to my mind are
1) structures in c++ doesnot provide datahiding where as a class provides datahiding
2)classes support polymorphism, whereas structures donot
Is there anything other than this?
Re: difference between a c++ structure and class
Quote:
Originally posted by rajalekshmy+r
what is the difference between a C++ structure and a class?
What comes to my mind are
1) structures in c++ doesnot provide datahiding where as a class provides datahiding
2)classes support polymorphism, whereas structures donot
Is there anything other than this?
No, you are wrong on both 1 and 2.
There is no difference between a class and a struct except that by default, the members of a struct are public, and the members of a class are private. Also structs are by default inherited publicly and classes by default are inherited privately. Other than that, whatever you can do in a class, you can do in a struct.
Hopefully you weren't asked this question on a job interview.
Regards,
Paul McKenzie
Re: difference between a c++ structure and class
Accoring to Zeeshan "C++ structure and C++ class remain same after introducing virtual function or virtual inheritance ." then why we do introduce a keyword class.
I mean if both are doing the same then whu unneceesary ly we have introduce a keyword class. There might be some reason behind the same
Re: difference between a c++ structure and class
structs by default inherit publicly and classes by default inherit privately.
Code:
struct MyStruct {};
class MyClass {};
struct MyDerivedStruct : MyClass //Public inheritance.
{}
class MyDerivedClass : MyStruct //Private inheritance
{}
Quote:
Originally Posted by ramacet
Accoring to Zeeshan "C++ structure and C++ class remain same after introducing virtual function or virtual inheritance ." then why we do introduce a keyword class.
I mean if both are doing the same then whu unneceesary ly we have introduce a keyword class. There might be some reason behind the same
One word - legacy reasons. Initially C had structs which could hold only data members and did not have access specifiers and all that good stuff. Then, when C++ was being designed, the creators wanted much more from a simple struct. They couldn't very well just break existing code by changing structs both syntactically or semantically. Thus, the class was born. Later on, more power was given to structures while maintaining them almost compatible with legacy code.