+ 2
I can not understand difference between Class and Struct.
2 Réponses
+ 3
the major difference is in the instantiation.....a struct can not be initialized using the new keyword bt a class can.
e.g you have a class named 'man' and also a struct name 'dog'. to initialize we do this;
man x = new man ();
then,
dog y = "bark";
this means that struct doesn't have a default constructor just like classes (unless otherwise created).
Also struct is a value type while class is a reference type. This means that struct variables directly hold the values themself but class variables are more like reference to the value, they hold the address to the value
0
Structs are used for simple "classes" that do not require private/hidden data, subclasses or polymorphism.
struct ex: node, point, firstLastName
class ex: Bankaccount, enemy, world