Search code examples
c++constructorparentinitializer

Having difficulty passing initialization list for constructor for parent and child objects


I have to make constructors with height+width or radius as well as three new optional parameters for x,y,z coordinates with default values of zero.

All four constructors (circle value and reference, rectangle value and reference) must have an initializer list which calls the 3-parameter Shape constructor.

If x,y,z are incoming constructor arguments, pass them along to Shape.

For the copy constructors, use the x,y,z of the incoming rectangle or circle argument.

I've tried all sorts of things inside of ": Shape(.......)" and am having no luck. Can anyone point me in the right direction?

Shape.h

class Shape {
private:
// disallow copy constructor
Shape(const Shape &) {}
static int numinstances;
int myid;


protected:

const int getId() const { return myid; }
double posx, posy, posz;

public:

Shape(const double inx, const double iny, const double inz) {
    myid = numinstances;
    ++numinstances;
    posx = inx;
    posy = iny;
    posz = inz;
}
// destructor must be public to be used without overriding
~Shape() { --numinstances; }

const double getX() const { return posx; }
const double getY() const { return posy; }
const double getZ() const { return posz; }
}

Rectangle.h

class Rectangle : public Shape {
private:
double dimx, dimy;


public:
Rectangle(double indimx, double indimy) : Shape (0, 0, 0)
{
    setWidth(indimx);
    setHeight(indimy);
}

Rectangle(const Rectangle& inrect) : Shape (inrect.getX(), inrect.getY(),     inrect.getZ())
{
    setWidth(inrect.getWidth());
    setHeight(inrect.getHeight());
}

const double getWidth() const { return dimx; }
const double getHeight() const { return dimy; }

Circle.h

const double PI = 3.14159;

class Circle : public Shape {
private:
double radius;
double x, y, z;

public:
Circle(double inrad) : Shape (0, 0, 0)
{
    setRadius(inrad);
}

Circle(const Circle& incirc) : Shape (incirc.getX(), incirc.getY(), incirc.getZ())
{
    setRadius(incirc.getRadius());
}

const double getRadius() const { return radius; }
void setRadius(double inrad) {
    radius = (inrad < 0 ? (0 - inrad) : inrad);
}

Main.cpp

Rectangle r1(2,3);
Circle c1(4);

Shape * shapeset[NUMSHAPES];
Rectangle * rectset[NUMRECT];
Circle * circset[NUMCIRC];

// populate arrays with generic shapes
for(i=0;i<NUMSHAPES;++i) {
    shapeset[i] = new Shape(1,2,3);
}
for(i=0;i<NUMRECT;++i) {
    rectset[i] = new Rectangle(i, i+1);
}
for(i=0;i<NUMCIRC;++i) {
    circset[i] = new Circle(i);
}

Shape * shapeset[NUMSHAPES];
Rectangle * rectset[NUMRECT];
Circle * circset[NUMCIRC];

// populate arrays with generic shapes
for(i=0;i<NUMSHAPES;++i) {
    shapeset[i] = new Shape(1,2,3);
}
for(i=0;i<NUMRECT;++i) {
    rectset[i] = new Rectangle(i, i+1, 1,2,3);
}
for(i=0;i<NUMCIRC;++i) {
    circset[i] = new Circle(i, 1,2,3);
}

Error Codes

||=== Build: Debug in Project (compiler: GNU GCC Compiler) ===|
C:\X\main.cpp||In function 'int main()':|
C:\X\main.cpp|52|error: no matching function for call to 'Rectangle::Rectangle(int&, int, int, int, int)'|
C:\X\main.cpp|52|note: candidates are:|
C:\X\Rectangle.h|29|note: Rectangle::Rectangle(const Rectangle&)|
C:\X\Rectangle.h|29|note:   candidate expects 1 argument, 5 provided|
C:\X\Rectangle.h|23|note: Rectangle::Rectangle(double, double)|
C:\X\Rectangle.h|23|note:   candidate expects 2 arguments, 5 provided|
C:\X\main.cpp|55|error: no matching function for call to 'Circle::Circle(int&, int, int, int)'|
C:\X\main.cpp|55|note: candidates are:|
C:\X\Circle.h|30|note: Circle::Circle(const Circle&)|
C:\X\Circle.h|30|note:   candidate expects 1 argument, 4 provided|
C:\X\Circle.h|25|note: Circle::Circle(double)|
C:\X\Circle.h|25|note:   candidate expects 1 argument, 4 provided|
C:\X\main.cpp|13|warning: unused variable 'ARSIZE' [-Wunused-variable]|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 2 second(s)) ===|

Thanks for the help! I'm really in a pickle here.


Solution

  • You need to add the parameters to the child constructor and pass them to the parent constructor:

    Rectangle(double indimx, double indimy, double x=0, double y=0, double z=0) : Shape (x, y, z)
    {
        setWidth(indimx);
        setHeight(indimy);
    }
    

    The double x=0 sets the default value for x if it is not passed in the call.