I have the following code that runs just fine as long as I have the Rect default constructor included. However if I comment it out hope that it will just 'skip' to the Shape default constructor it fails to compile.
#include <cstdio>
class Shape
{
public:
Shape()
{
printf("Shape default called\n");
}
};
class Rect : public Shape
{
public:
int width;
int height;
Rect()
{
printf("Rect default called\n");
}
Rect(int width, int height)
{
this->width = width;
this->height = height;
}
int area()
{
return width*height;
}
};
class Square : public Rect
{
public:
Square(int width)
{
printf("Square constructor called.\n");
this->width = width;
this->height = width;
}
};
int main(int argv, char** argc)
{
printf("...\n");
Square r = Square(10);
printf("Area: %d\n", r.area());
return 0;
}
When the program runs and compiles I get the following output
...
Shape default called
Rect default called
Square constructor called.
Area: 100
I was hoping to simply remove the Rect
default constructor and get
...
Shape default called
Square constructor called.
Area: 100
because all the rect default constructor will do is sit there to satisfy clang's error. It isn't intended to do anything extra. Is there a way to accomplish this?