【发布时间】:2015-03-13 21:51:35
【问题描述】:
我必须创建具有高度+宽度或半径的构造函数,以及 x、y、z 坐标的三个新可选参数,默认值为零。
所有四个构造函数(圆形值和引用、矩形值和引用)都必须有一个初始化列表,该列表调用 3 参数 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; }
圆.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);
}
错误代码
||=== 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)) ===|
感谢您的帮助!我真的在这里泡菜。
【问题讨论】:
-
那么,您对这些编译器消息有什么特别不了解的地方?
-
我不知道如何为 Child : Parent 构造函数提供正确数量的参数。
标签: c++ constructor parent initializer