1) creates a local object a
of type Money
, which is allocated on the stack. a
will be destroyed when it goes out of scope, e. g. when the function that created it exits.
2) dynamically allocates an object of type Money
on the heap. That object will persist even after the function that created it exits, until delete
is called on it.
If you only need an object to use within a function, it is generally ok to use 1). If you need the object to persist after your function exits, use 2). You should also use 2) if the object is very large, i. e. if it takes up a lot of memory. If you use 2), remember to call delete
when you no longer need the object.
There is more to it, such as the use of polymorphism, but this should get you started.