build a shape class Square

I have an assignment which I'm finding very difficult. Any help would be appreciated.

Build the hierarchy by creating the Shape classes Circle, Square, and Triangle. For these derived classes, create default constructors and constructors whose arguments can initialize the shapes appropriately using the correct number of Point objects (ie, Circle requires a Point center and a radius; Square requires four Point vertices, while Triangle requires three Point vertices).

In main(), create one instance each of the following: a Circle with a radius of 23, a Square with sides 25, and a Triangle with sides 10, 20, 30. Define all of them so that the origin (0,0) is somewhere within each object. Display the information from each object.

When I enter under main() Square s(25, Point(0,0));

class Square : public Shape
{
  double sides;
  Point cp;

  public:
  Square() : sides(0) {}
  Square(double side, const Point &center) : sides(side), cp(center){}

  void bbox()
  {
     Point bottomright = cp + Point(sides/2, -sides/2);
     Point topleft = cp + Point(-sides/2, sides/2);
     Point topright = cp + Point(sides/2, sides/2);
     Point bottomleft = cp + Point(-sides/2, -sides/2);

     std::cout << "Square::bounding " << bottomright  << topleft << topright << bottomleft;
  }

  double area() {std::cout << "Square::area "; return (sides * sides);}
  double circumference() {std::cout << "Square::perimeter "; return sides + sides + sides + sides;}

};

The class prints out

Square::area 625
Square::perimeter 100
Square::bounding (12.5,-12.5)(-12.5,12.5)(12.5,12.5)(-12.5,-12.5)

I'm wondering does this look right based on what the assignment is asking?


No it doesn't, from the requirement it appears you need the Square constructor to take 4 points as arguments:

Square(const Point& pt1,const Point& pt2,const Point& pt3,const Point& pt4)

Square requires four Point vertices

Right?

链接地址: http://www.djcxy.com/p/61686.html

上一篇: iOS上的核心图形性能

下一篇: 建立一个形状类广场