// Ex9_05.cpp
// Using a derived class copy constructor
#include <iostream> // For stream I/O
#include "CandyBox.h" // For CBox and CCandyBox
using std::cout;
using std::endl;
int main()
{
CCandyBox chocBox(2.0, 3.0, 4.0, "Chockies"); // Declare and initialize
CCandyBox chocolateBox(chocBox); // Use copy constructor
cout << endl
<< "Volume of chocBox is " << chocBox.Volume()
<< endl
<< "Volume of chocolateBox is " << chocolateBox.Volume()
<< endl;
return 0;
}
This example produces the following output:
CBox constructor called
CCandyBox constructor2 called
CBox constructor called
CCandyBox copy constructor called
Volume of chocBox is 24
Volume of chocolateBox is 1
CCandyBox destructor called
CBox destructor called
CCandyBox destructor called
CBox destructor called
Although this looks okay at first sight, there is something wrong. The third line of output shows that the default constructor for the CBox part of the object chocolateBox is called, rather than the copy constructor. As a consequence, the object has the default dimensions rather than the dimensions of the initializing object, so the volume is incorrect. The reason for this is that when you write a constructor for an object of a derived class, you are responsible for ensuring that the members of the derived class object are properly initialized. This includes the inherited members."
Why must we call the base class constructor explicitly for it to work? Why does the default automatically call it then, why doesn't the copy constructor?
It will work right? Cause when I do "CBox(initCB)" it only sends the BASE part of the derived object to the base class copy constructor, is it copy or default?
Why must we call the base class constructor explicitly for it to work?
We don't always want the base class copy constructor to be called. If you are supplying the definition of a copy constructor, you're specifying the behavior of the constructor. So, specify the behavior.
Why does the default automatically call it then, why doesn't the copy constructor?
Is it really that surprising that the default constructor is invoked when you don't explicitly invoke a different one?
But then why does my book tell me do to that? It says make sure each constructor explicitly calls base constructor otherwise the argument the derived constructor wont be called.
The book is saying that if you need to use a base class constructor that isn't the default one, you must specify which constructor to use, with which arguments, in the initialization list.
If you don't specify it, it will use the default base class constructor, which doesn't take any arguments.
Its telling me that I need a base class constructor in the copy constructor that I call. Otherwise my copy constructor wont be able to be called. Try running the first code, the copy constructor isnt called.
But why?
Is it in default the compiler automatically calls both defaults?
The base class constructor was being called, but the from the new derived class constructor, I was confused thinking the derived constructor initializes the heights/widths/heights so I thought it's default was called instead. Sorry for my fail