This is problem 1 from chap. 13 of Prata, C++ Primer Plus, which deals with inheritance. I feel like I'm a little fuzzy on some of the issues here.
Here's the problem. He gives you a Cd class defined like this:
The assignment is to design a derived class called "Classic" that just adds an array for the primary work on the CD.Code:class Cd // represents a CD disk { private: char performers[50]; char label[20]; int selections; double playtime; public: Cd(char *s1, char * s2, int n, double x); Cd(const Cd & d); Cd(); ~Cd(); void Report() const; Cd & operator=(const Cd & d); };
So, the derived class has an additional (private) member that could look like this:
char work[50];
What you're supposed to do is now create appropriate methods in the derived class and declare as virtual what needs to be virtual in the base class (obvious is the destructor, but please don't comment on that yet, as I'd like to figure it out myself--intuitively, I want Report() to be virtual as well, but nothing else).
My current problem is more on what's important here on the constructor end.
I'm inclined to make one like this:
Classic(char * perf, char * lab, int n, double x, char * w = "unknown");
Where I'm not completely solid is whether it's really necessary to define a default value for w (which will of course be the value of the new array). If we just had
Classic(char * perf, char * lab, int n, double x, char * w);
wouldn't we end up with potential problems just in case someone should do something like this in main:
Classic myCD("Alfred Brendel", "Decca", 12, 57.326);
basically forgetting to put in something for primary work. Without giving w a default value, wouldn't that one just fall back on the Cd constructor with those parameters? Then the issue is what happens with the work[] array. If the compiler definitely assigns it just as an empty array (beginning with '\0'), then we're good, but if it just assigns a pointer out into space with potential junk values, then it seems to me like we need something along the lines of my proposed constructor... ?
Anyhow, to begin with, if at all possible, don't answer any other aspects of this exercise for the moment. I think I understand the rest of it, but I may have some other issues that I need to ask about, too, before it's all over.