-
derived class
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:
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);
};
The assignment is to design a derived class called "Classic" that just adds an array for the primary work on the CD.
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.
-
Overload resolution does not apply to base class constructors. If you only define the constructor
Code:
Classic(char * perf, char * lab, int n, double x, char * w);
and someone tries to do
Code:
Classic myCD("Alfred Brendel", "Decca", 12, 57.326);
then they will get a compile error.
-
Here's my current solution:
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();
virtual ~Cd();
virtual void Report() const;
Cd & operator=(const Cd & d);
};
class Classic : public Cd
{
private:
char work[50];
public:
Classic(char * perf, char * lab, int n, double x, char * w = "unknown");
Classic(const Cd & d, char * w = "unknown");
Classic(const Classic & d); // copy constructor
Classic();
~Classic();
void Report() const;
Classic & operator=(const Cd & d);
};
My real question is whether I'm right in thinking that it's important to provide a default value (or use other means to do the same thing) in the lines of:
Classic(char * perf, char * lab, int n, double x, char * w = "unknown");
Classic(const Cd & d, char * w = "unknown");
-
Ok, then I'm more paranoid than necessary here.
Same apply to the statement:
Classic myCD(yourCD);
where yourCD is a Cd object (but not Classic)?
Actually, I think it's nice if we do get compiler errors on both of these.
I'm going to run some tests on this -- just to completely dispell my paranoia here... :)
-
Also, I'm now ok with an answer on which methods to make virtual. Are my picks of only the destructor and the Report() function correct?