derived class

This is a discussion on derived class within the C++ Programming forums, part of the General Programming Boards category; This is problem 1 from chap. 13 of Prata, C++ Primer Plus, which deals with inheritance. I feel like I'm ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    242
    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");

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    242
    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...

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    242
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call base class function or derived class function
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-18-2008, 06:23 AM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 12:36 PM
  3. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 07:32 PM
  4. Replies: 4
    Last Post: 12-29-2002, 12:29 AM
  5. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21