Skip to Main Content
C++ Primer Plus, Fourth Edition
book

C++ Primer Plus, Fourth Edition

by Stephen Prata
November 2001
Beginner content levelBeginner
1128 pages
29h 12m
English
Sams
Content preview from C++ Primer Plus, Fourth Edition

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++ Primer Plus, Fifth Edition

C++ Primer Plus, Fifth Edition

Stephen Prata
C++ All-In-One For Dummies®, 2nd Edition

C++ All-In-One For Dummies®, 2nd Edition

John Paul Mueller, Jeff Cogswell
C++ Primer Plus

C++ Primer Plus

Stephen Prata

Publisher Resources

ISBN: 0672322234Purchase book