2012年2月22日 星期三

Exercise #11


This assignment will give you practice with inheritance and polymorphism.

Create the following classes:

GeometricObject:    This is the base class for the other four classes. It should have two protected data members, x and y (coordinates). The class should have the following member functions:
    GeometricObject()    // ctor
    getX()        // accessor function
    getY()        // accessor function
print()
    describeYourself()
    length()
    area()

    All member functions should be const member functions, except the constructor. describeYourself(), length(), and area() functions should be pure virtual functions. The print() function should display the line of output shown below, like
    GeometricObject: 0x????? - location = (x,y)

point    Is derived from GeometricObject and has no data members. Its length() and area() functions should return 0.0.

line    Is also derived from GeometricObject and has two point data members, p1 and p2.  Its constructor must initialize the x,y members of the GeometricObject class. The (x,y) coordinates of a line should be the midpoint of p1 and p2 (take the average of the x coordinates and the average of the y coordinates). Its area() function should return 0.0, but the length() function should return the distance between the two points. Use the formula:

 circle    Is also derived from GeometricObject and has one data member, a double, radius. The constructor should have a point argument and a double argument. The point argument is used to initialize the (x,y) coordinates of the GeometricObject base and the double to initialize the radius.  The length() function should return the circle's circumference and the area() function . r2.

triangle    Is also derived from GeometricObject and has 3 point data members, p1, p2, and p3. Its constructor should have three (point) arguments, to initialize the data members. It also needs to initialize the GeometricObject's (x,y) coordinates. The (x,y) coordinate for a triangle should be the average of its x coordinates and the average of its y coordinates. Add three private member functions, side1(), side2(), side3() to the class. They should each return a double length of a side. For example, side1() should return the length of the side that is opposite point p1. These functions will be useful for the length() and the area(). The length() should return the triangle's perimeter. Use the following formulas for the area of a triangle:


Use the main() below to test your program. The output should give you an indication of what is expected.

int main(void)
{
  int i;
  Point P(0,0), Q(3,0), R(3,4);
  P.DescribeYourself();
  Line L(Q,R);
  L. DescribeYourself ();
  Circle C(P,3);
  C. DescribeYourself();
  Triangle T(P,Q,R);
  T. DescribeYourself();

// polymorphism testing
  GeometricObject* Obj[6];
  Obj[0] = new Point(2,1);
  Obj[1] = new Point(8,1);
  Obj[2] = new Point(5,5);
  Obj[3] = new Line(P,Q);
  Obj[4] = new Circle(P,2.0);
  Obj[5] = new Triangle(P,Q,R);

  for (i = 0; i < 6; i++) Obj[i]-> DescribeYourself();

  for (i = 0; i < 6; i++) delete Obj[i];

  return 0;
}

******  Program Output  ******

GeometricObject: 0x6a0c4 - location = (0,0)
I am a point

GeometricObject: 0x6a060 - location = (3,2)
I am a line
Length=4

GeometricObject: 0x6a038 - location = (0,0)
I am a circle
Area=28.2743  circumference=18.8496

GeometricObject: 0x69fd4 - location = (2,1.33333)
I am a triangle
Area=6  permeter=12

GeometricObject: 0x6c160 - location = (2,1)
I am a point

GeometricObject: 0x6c180 - location = (8,1)
I am a point

GeometricObject: 0x6c1a0 - location = (5,5)
I am a point

GeometricObject: 0x6b100 - location = (?,?)
I am a line
Length=?

GeometricObject: 0x6c1c0 - location = (?,?)
I am a circle
Area=??????  circumference=??????

GeometricObject: 0x6e100 - location = (?,?)
I am a triangle
Area=?  permeter=??

Extra Credit (1 point0) Add a constructors to the circle class, so that you can create a circle:
using a point and a line - the first point is the center, the circle is tangent to the line.

沒有留言:

張貼留言