Мне нужно создать конструкторы с высотой + шириной или радиусом, а также с тремя новыми необязательными параметрами для координат x, y, z со значениями по умолчанию, равными нулю.
Все четыре конструктора (значение круга и ссылка, значение прямоугольника и ссылка) должны иметь список инициализаторов, который вызывает конструктор Shape с тремя параметрами.
Если x, y, z являются входящими аргументами конструктора, передайте их в Shape.
Для конструкторов копирования используйте x,y,z входящего аргумента прямоугольника или круга.
Я пробовал всевозможные вещи внутри ": 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; }
}
Прямоугольник.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; }
Круг.ч
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);
}
Коды ошибок
||=== 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)) ===|
Спасибо за помощь! Я действительно в рассол здесь.