M
Markus Svilans
Hi,
What is the difference between having a struct with constructors and
methods versus a class?
In many C++ examples online I have seen code similar to this:
struct Animal
{
Animal()
{
id = 0;
name[0] = 0;
}
int id;
char name[30];
};
Intuitively, I would understand structs with methods to be more data
oriented (i.e. no virtual methods), where the methods provide
initialization and maybe get/set methods. Classes would be more object
oriented, with more elaborate code, besides simple initialization and
get/set methods.
Can any one elaborate more on this? I'd like to understand better when
it's more correct to use a struct with methods, and when it's more
correct to use classes. Until now I have been using classes for any
kind of data structure that requires methods and automatic
initialization.
Thanks,
Markus.
What is the difference between having a struct with constructors and
methods versus a class?
In many C++ examples online I have seen code similar to this:
struct Animal
{
Animal()
{
id = 0;
name[0] = 0;
}
int id;
char name[30];
};
Intuitively, I would understand structs with methods to be more data
oriented (i.e. no virtual methods), where the methods provide
initialization and maybe get/set methods. Classes would be more object
oriented, with more elaborate code, besides simple initialization and
get/set methods.
Can any one elaborate more on this? I'd like to understand better when
it's more correct to use a struct with methods, and when it's more
correct to use classes. Until now I have been using classes for any
kind of data structure that requires methods and automatic
initialization.
Thanks,
Markus.