Microsoft Interview Question
Well few more differences.
Classes can be inherited.
The concept of constructor/destructor is not there in structures.
Incorrect. As raghava mentioned, structs default to public member access, whereas classes default to private member access. This is the ONLY difference in C++, so you can do anything with a struct that you can do with a class (constructor/destructor, inheritance, operator overloading, etc)
1)The members of a structure are public by default, the members of class are private by default.
- GirishNayak September 19, 20082)Default inheritance for Structure from another structure or class is public.Default inheritance for class from another structure or class is private.
class A{
public:
int i;
};
class A2:A{
};
struct A3:A{};
struct abc{
int i;
};
struct abc2:abc{
};
class abc3:abc{};
int _tmain(int argc, _TCHAR* argv[])
{
abc2 objabc;
objabc.i = 10;
A3 ob;
ob.i = 10;
//A2 obja; //privately inherited
//obja.i = 10;
//abc3 obss;
//obss.i = 10;
}
This is on VS2005.