What are the differences between struct and class?
There is not much difference between the keywords
struct and
class in C++.
The members of a struct are public by default, while in class, they are private. It is thus very important to specify the access specifiers instead of relying on the defaults. Default values often end up confusing programmers who end up managing code that you wrote.
As a simple example:
class point1
{
int x; //private by default.
public:
int y;
};
struct point2
{
int x; //public by default.
public:
int y;
};
int main()
{
point1 a;
point2 b;
a.x = 0; //error because x is private
b.x = 0; //Valid, as the member x is a public member by default in struct.
}