Class Constructors and Destructors

Meanwhile, there's more to be done with the Stock class. There are certain standard functions, called constructors and destructors, that you should normally provide for a class. Let's see why they are needed and how to write them.

One of C++'s aims is to make using class objects similar to using standard types. However, you can't yet initialize a Stock object the way you can an ordinary int or struct:

int year = 2001;                                   // okay
struct thing
{
    char * pn;
    int m;
};
thing amabob = {"wodget", -23};                    //okay
Stock hot = {"Sukie's Autos, Inc.", 200, 50.25};   // NO!

The reason you can't initialize a Stock object this way is because the data parts have private access status, which means a program cannot access the data ...

Get C++ Primer Plus, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.