Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ Inheritance help needed?

Hi, I am having problems with a base constructor and a derived constructor for a Car class

http://pastebin.com/xkqWLV4s

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    A few things:

    I got an error on this line:

    sponsor = s;

    s is a const char *, but sponsor is just a char *. You can't discard constness like that. You want:

    const char *sponsor;

    The output is wrong. Look at this bit:

    car::car( const char *name, const char *color )

    {

    name = name;

    color = color;

    What does name refer to on the left side of the = sign? On the right side?

    Well, they certainly refer to the same thing. Which means you are assigning something to itself, which is a NOP. Change the parameter names.

    In your destructor, you are calling:

    delete [] name;

    But you didn't allocate name, you shouldn't be deleting it.

    Since this is C++, you really should be using strings rather than char *'s. But you can make it work with char * if you must.

Still have questions? Get your answers by asking now.