I have a set of code, which mimics a basic library cataloging system. There is a base class named items, in which the the general id,title and year variables are defined and 3 other derived classes (DVD,Book and CD).
Base [Items]
Derived [DVD,Book,CD].
The programs runs, however I get the following warnings, I’m not sure how to fix these.
>"C:\Program Files\gcc\bin/g++" -Os -mconsole -g -Wall -Wshadow -fno-common mainA4.cpp -o mainA4.exe In file included from mainA4.cpp:5: a4.h: In constructor `DVD::DVD(int, std::string, int, std::string)': a4.h:28: warning: `DVD::director' will be initialized after a4.h:32: warning: base `Items' a4.h:32: warning: when initialized here a4.h: In constructor `Book::Book(int, std::string, int, std::string, int)': a4.h:48: warning: `Book::numPages' will be initialized after a4.h:52: warning: base `Items' a4.h:52: warning: when initialized here a4.h: In constructor `CD::CD(int, std::string, int, std::string, int)': a4.h:66: warning: `CD::numSongs' will be initialized after a4.h:70: warning: base `Items' a4.h:70: warning: when initialized here >Exit code: 0
When you declare member variables in a class, they’re initialized in the order you declare them. But you may write them in any order in your constructor’s initializer list. For example,
will construct
aand thenb, even though it appears that you’re initializing them in the other order.The compiler issues a warning because you can trick yourself into writing incorrect code. For example,
the value of
awill be undefined.