class ITEMS
{
string name;
public:
void putString(string n)
{
name = n;
}
void getString()
{
cout << " Name is " << name << endl;
}
};
int main()
{
ITEMS a;
ITEMS* b = new ITEMS; // Use * to indicate a pointer
a.putString("Michael");
a.getString();
b->putString("Angelo");
b->getString();
delete b; // remember to delete everything that you new
return 0;
}
Normally you should avoid using raw pointers, so a is generally preferred. However, sometimes you need to be able to create objects dynamically and so you have to use b. I think experience and practice will tell you when each method is most appropriate. Essentially it has to do with how long the object is expected to live.
Objects created like a, are destroyed automatically when they go out of scope, whereas objects created like b will live beyond the scope of their creation.
Always prefer method a. With method a, the object is created on the stack which is generally faster, its memory is automatically handled unlike the other more error-prone approach, and it is exception safe.
Use method b, only when you have to, and even then a smart pointer should be preferred over a raw one.