CS177/477 C++ Programming
Spring 2000

Moving Squares

One of the major uses of inheritance is to the add features to an existing class structure. Typically, a set of classes will be delivered as a pre-compiled library, and thus the behavior of the classes is fixed, since the source code is not available. However, through inheritance, the behavior of the classes in the library can be extended. In this assignment, you will be provided with a pre-compiled set of classes and a header file (with the .h file name extension). The hierarchy of the library is shown below in sketch form.

Text Box: class GObject
  virtual void move(int dx, int dy)=0;
  virtual void insert(ostream& str);

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

The header file gobject.h contains the declarations of each class and their members. It is printed at the end of this assignment. You should examine the header file to get an accurate picture of the capabilities of each class. The definitions of the member functions have been compiled into a .o file that you will also be provided with. The purpose of each function is as follows:

·        move: the parameters dx and dy are distances along x and y axes. The object will be moved accordingly.

·        insert: a member function that puts a text version of the object into the stream str. You can use this to print the objects on the screen. There is also an overloaded insertion operator for each class that calls the member function insert.

·        length (in Line only): this simply returns the length of the line.

·        addPoint (in MultiPoint only): this adds a point to the set of points stored in the vector (using push_back).

Procedure

1.      First, copy the two files cobjects.h and cobjectl.o from my public directory with:

% cp ~rth/public/gobjects.* .

Note the period at the end of the command that means "to this directory".

2.      Your task is to implement two classes, both of which represent a square. Square1 will use the line class to implement a square as four lines of equal length. This will use inheritance from GObject, and will look as follows:

class Square1 : public GObject {
private:
  vector<Line> lines;
public:
  Square1(int sideLength, int topLeftX, int topLeftY);
  void move(int dx, int dy);
  void insert(ostream& str);
friend ostream& operator<<(ostream& sout, Square1& sq);
};

Square2 will also use inheritance, but from MultiPoint. In fact it is a special kind of MultiPoint object—one with four points such that the distances between them are the same, and each pair of adjacent points is either horizontal or vertical. Its class will look as follows:

class Square2 : public Multipoint {
public:
  Square2(int sideLength, int topLeftX, int topLeftY);
};

The reason it has no other members is that it will inherit both representation and behavior (move and insert) from MultiPoint. Constructor functions are not inherited, so its additonal behavior will need a new constructor.

3.      Use the main function given below to guide your implementation of both classes Square1 and Square2. Your program must work with this main function, unaltered.

 

 

int main() {
  Square1 s1(100, 200, 300);
  Square2 s2(100, 200, 300);

  GObject* A[2];

  A[0] = &s1;
  A[1] = &s2;

  cout << "Square 1 is " << s1 << endl;
  cout << "Square 2 is " << s2 << endl;

  for (int i = 0; i < 2; i++)
    A[i]->move(50, 100);

  cout << "Square 1 is " << s1 << endl;
  cout << "Square 2 is " << s2 << endl;

  return 0;

}

Hints

·        You may want to write a small test program to investigate the behavior of Line, Point and MultiPoint before you write the Square classes.

·        There is no checking of any kind in the implementation of the basic classes, so it is possible that they can be made to produce peculiar results.

·        The main program given above uses the sub-type rule to store pointers to objects of different types in an array. The call to move then uses the virtual function mechanism to choose the appropriate function at run-time.

·        To compile your program and link with gobjects.o, use the command: g++ mysquares.cc gobjects.o

Deliverables

1.    A printout of your entire program source code.

2.     A printout of the results of your program running on the given main function.

Due Date

Hand in your solution by 5pm on Monday, May 1st. Submit your program as "program4" through the assignment submitter.

 


The header file for the classes GObject, Point, Line and MultiPoint

 

#include <iostream>

#include <vector>

 

using namespace std;

 

class GObject 

{

public:

      virtual void move(int dx, int dy) = 0;

private:

      virtual void insert(ostream& str) const = 0;

};

 

class Point : public GObject 

{

public:

      Point();

      Point(int xx, int yy);

private:

      void insert(ostream& str) const;

      int y;

      int x;

     

friend ostream& operator<<(ostream& str, const Point& p);

friend class Line;

};

 

class Line : public GObject 

{

public:

      Line();

      Line(Point pp1, Point pp2);

      int length();

      void move(int dx, int dy);

private:

      void insert(ostream& str) const;

      Point p2;

      Point p1;

friend ostream& operator<<(ostream& str, const Line& l);

};

 

class MultiPoint : public GObject 

{

public:

      MultiPoint();

      void addPoint(Point p);

      void move(int dx, int dy);

private:

      void insert(ostream& str) const;

      vector<Point> points;

friend ostream& operator<<(ostream& str, const MultiPoint& mp);

};