【问题标题】:setting a later value after constructing an object in object inheritance在对象继承中构造对象后设置稍后的值
【发布时间】:2013-05-02 19:54:38
【问题描述】:
Shape *shape[100];
Square sqr;

void inputdata() {
int len,width;
cout << "enter length";
cin >> len;
cout << "enter width";
cin >> width;

sqr = Square(len,width,0); //---> i have not compute area for this, i just put a 0 for it first     
shape[0] = &sqr;
}

void computeArea() {
int area;
area = shape[0]->computeArea();
//----> need to set my area here after getting it
}

shape 是父类,square 是子类

创建方形对象并将其插入形状数组后。我无法在我的 square 类中使用 setArea() 方法来设置区域。

我已经找到了两种解决方案,但是感觉它不适合对象继承多态。

一种方法是在 shape 类中实现 setArea()(我已经在 square 类上设置了 setArea())并通过多态调用 setArea 方法并将其设置到我的正方形区域属性中。

另一种方法是在 shape 类中创建一个 get 对象方法,即 getSquare(),这样我就可以通过 Shape 数组访问 getArea() 方法

我的两种方法有效吗?还是有更好的方法?

class Square: public Shape{

private:
int len;
int width;
int area;

public:
Square(string,int,int,int);
int getArea();
void setArea(int);
};

int Square::computeArea() {
int sqrArea = len*width;
area = setArea(sqrArea);
return sqrArea;
}

int Square::setArea(int _area) {
area = _area;
}

【问题讨论】:

  • 显示 Square 和 Shape 类!
  • 你在inputdata()方法上有一个错误,你应该使用sqr = new Square(len, width, 0),看看你的编译器产生的警告,它们在这里不仅是为了惹恼你。言归正传,给我们解释一下,为什么需要将Squares 保留在Shape 类型的数组中。如果你想对这个数组使用Square-specific 方法,你可以使用Square *square[100]; 数组。如果您想在其中保留其他对象,为什么要使用仅对Squares 可用的方法?看起来像是设计问题,请为我们提供 SquareShape 类,以便我们为您提供帮助。

标签: c++ object inheritance polymorphism


【解决方案1】:

如果您的Square 真的依赖于通过将其自己的computeArea() 的结果传递给它的人来设置其区域,那么您的设计看起来是错误的。

为什么不实现computeArea() 以便它在对象上设置区域并返回它?

编辑

根据您更新的问题,为什么SetArea() 会返回任何内容?实际上,根据声明,它没有,但它的定义确实如此。只需完全删除SetArea()(无论如何,从外面设置一个正方形的区域有什么意义?)然后这样做:

class Square: public Shape {

private:
  int len;
  int width;
  int area;

public:
  Square(string,int,int,int);
  int getArea();
};

int Square::computeArea() {
  area = len*width;
  return area;
}

【讨论】:

  • 可以说我的 shapeArray 中有 3 个方形对象,shape[0],shape[1],shape[3],我计算了其中的 3 个。在我的 square.cpp 中调用 computeArea() 后立即将 area 设置为我的 area 属性。程序能够验证区域属于哪个形状数组吗?我虽然我需要 shape[0].setArea 之类的东西才能正确输入每个数组的正确区域。
  • @user2351750 我假设该区域存储在Square 的数据成员中。这意味着Square 的每个实例都有自己的数据成员副本,并且它的成员函数computeArea() 将在该副本上运行。这些是 C++ 中 OOP 的基础知识;您可能想阅读good book 以了解它们。
  • 好的,我在返回区域之前将其设置在我的 computeArea() 中的区域属性上。我收到一个错误“无效值没有被忽略,因为它应该是。因为运行时还没有开始,所以还没有完成计算,所以当我设置它时,我有这个错误。有什么方法可以忽略或避免这个?
  • @user2351750 您正在询问有关您未显示的代码的问题。将ShapeSquarecomputeArea()setArea() 的定义添加到您的问题中。
  • @user2351750 不进入 cmets。编辑您的问题以将代码放入其中。
【解决方案2】:

如果你真的只想在Square类中实现setArea,你可以dynamic_cast

if ( Square *s = dynamic_cast<Square *>shape[0] ) {
  s -> setArea();
}

通常使用dynamic_cast 是糟糕设计的标志,在这种情况下,为什么Shape 不实现setArea(),因为面积对所有形状都是通用的。

【讨论】:

    【解决方案3】:

    计算区域应该是所有形状共有的东西,因此将computeArea 提升到基类中(并可能使其抽象)似乎是一个有效的解决方案。

    【讨论】:

    • 是的,这就是我的建议。 OTOH,你会考虑LineShape - 如果是,它的面积是多少?
    • A Shape 是 2D 对象 - 它有长度和宽度(因此也有面积)。一条线是一维对象 - 它只有长度。
    • 对不起,我不知道你在基类中托管 computeArea 是什么意思。我在类和形状类中都有 computeArea,并且虚函数可以正常工作
    猜你喜欢
    • 2012-09-28
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-03
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多