O'Reilly logo

C++ Primer Plus, Fourth Edition by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required