THIS IS IN C++ The goal of assignment is to implement classes representing shape
ID: 3790489 • Letter: T
Question
THIS IS IN C++
The goal of assignment is to implement classes representing shapes. A given program will use this class to create shapes at arbitrary locations in the x-y plane and move them. The program uses a function to move shapes and to detect if shapes overlap. The board is assumed to be the entire xy plane. The class Shape is an abstract base class from which six classes are derived, named: O, I, L, S, X, U. Your task is to write the implementation file Shape.cpp containing the implementation of all classes. You are not allowed to modify the file Shape.h Your class implementation file Shape.cpp should compile without warning using the command $ g++ -Wall –c Shape.cpp You are also given a program testShape.cpp and a Makefile (same web site as above). These two files should not be modified. The testShape.cpp program tests the functionality of the Shape class. You should be able to build the executable testShape using the command $ make You will use the testShape executable and the input and output test files provided on the web site to check the functionality of your classes and verify that your program reproduces the test output exactly. Use the diff command to compare your output file with the output file provided. Note that these test files do not check all the functionality and you are responsible for verifying your implementation. Other test files will also be used when grading your implementation. The testShape program reads input from stdin and writes to stdout. It can be used e.g. as $ ./testShape < test1.in It can be assumed that input will consist of two lines. Each line consists of one character followed by two integers. The character and integers are separated by white space as in e.g. X 2 -3 Specification of the Shape class We describe here the detailed specification of the Shape class. The positions of the cells occupied by a shape are stored in two integer arrays whose size depends on the shape, i.e. a shape occupies the cells located at (x[0],y[0]), (x[1],y[1]), etc... Study carefully the file Shape.h. Some members are public and some protected. Some members are virtual. Make sure you understand why. Study the file testShape.cpp in order to understand how the classes will be used. Public members virtual char name(void) = 0; This pure virtual function returns the name of the class instance as a character, i.e. O, I, L, S, X, U. void print(void) This function prints information about the location of the shape by showing the position of the cells that the shape occupies: name at (x[0],y[0]) (x[1],y[1]) ... For example, from Figure 2: X at (5,4) (4,5) (5,5) (6,5) (5,6) bool overlap(const Shape &s) This function should return true if the shape calling the function overlaps with the shape s. This can be checked by comparing the arrays x and y of both shapes. If there is no overlap, i.e. no cells are shared by the two shapes, the function should return false. void move(int dx, int dy) This function moves the shape by a displacement (dx, dy). This function modifies the values of the arrays x and y. Note that this function is the same for all shapes and need not be implemented as a virtual function. static Shape *makeShape(char ch, int posx, int posy) This function is a "shape factory". It is used to generate instances of shapes. It creates an instance of the appropriate kind of shape (specified by the character ch, with values 'O', 'I', 'L', 'S','X', 'U') at the given position (posx,posy) using the operator new, and returns a pointer to the instance. If the character ch is not one of the allowed letters, the function should throw an invalid_argument exception. The error printed by the program should correspond to the example given in one of the test files. protected members int *x,*y The arrays holding the coordinates of the cells occupied by the shape
So inputing
will output
and the input :
will output:
Thanks in advance.
8 2 7 1 1 2 3 5 3 2 3 2 1 2 2 3 0 1 1 3 4 5 6 7 8 9 Figure 2 A shape of type "L" placed at (0,7) A shape of type "L" placed at (5,8) A shape of type "I" placed at (9,6) A shape of type "X" placed at (5,4) A shape of type "O" placed at (8,5) A shape of type "U" placed at (0,2) A shape of type "O" placed at (1,0) A shape of type "S" placed at (4,0). A shape of type "S" placed at (7,1).Explanation / Answer
Please use the below Shape.cpp file with Shape.h and testShape.cpp from the question. The output of main program is shown below. Please don't forget to rate the answer if it helped. Thank you.
Shape.cpp
#include "Shape.h"
#include <iostream>
#include <stdexcept>
using namespace std;
Shape::~Shape(void)
{
delete []x;
delete []y;
}
void Shape::print(void) const
{
int sz = size();
cout << name() << " at ";
for(int i = 0; i < sz; i++)
cout << "(" << x[i] << "," << y[i] << ") " ;
cout << endl;
}
void Shape::move (int dx, int dy)
{
int sz = size();
for(int i = 0; i < sz; i++)
{
x[i] += dx;
y[i] += dy;
}
}
bool Shape::overlap(const Shape &t) const
{
int sz1 = size(), sz2 = t.size();
for(int i = 0; i < sz1; i++)
{
for(int j = 0; j < sz2; j++)
if(x[i] == t.x[j] && y[i] == t.y[j])
return true;
}
return false;
}
Shape* Shape::makeShape(char ch,int posx,int posy)
{
Shape *s = NULL;
string errmsg;
switch(ch)
{
case 'o':
case 'O':
s = new O(posx, posy);
break;
case 'x':
case 'X':
s = new X(posx, posy);
break;
case 'u':
case 'U':
s = new U(posx, posy);
break;
case 'i':
case 'I':
s = new I(posx, posy);
break;
case 'l':
case 'L':
s = new L(posx, posy);
break;
case 's':
case 'S':
s = new S(posx, posy);
break;
default:
errmsg = "Invalid character " + to_string(ch);
throw invalid_argument(errmsg);
}
return s;
}
//functions of shape O
O::O(int posx, int posy)
{
x = new int[1];
y = new int[1];
x[0] = posx;
y[0] = posy;
}
char O::name(void) const
{
return 'O';
}
int O::size(void) const
{
return 1;
}
//functions of shape I
I::I(int posx, int posy)
{
x = new int[2];
y = new int[2];
//coordinates of 1st location on grid
x[0] = posx;
y[0] = posy;
//coordinates of 2nd location on grid
x[1] = posx;
y[1] = posy + 1;
}
char I::name(void) const
{
return 'I';
}
int I::size(void) const
{
return 2;
}
//functions of shape L
L::L(int posx, int posy)
{
x = new int[3];
y = new int[3];
//coordinates of 1st location on grid
x[0] = posx;
y[0] = posy;
//coordinates of 2nd location on grid
x[1] = posx + 1;
y[1] = posy ;
//coordinates of 3rd location on grid
x[2] = posx ;
y[2] = posy + 1;
}
char L::name(void) const
{
return 'L';
}
int L::size(void) const
{
return 3;
}
//functions of shape X
X::X(int posx, int posy)
{
x = new int[5];
y = new int[5];
//coordinates of 1st location on grid
x[0] = posx;
y[0] = posy;
//coordinates of 2nd location on grid
x[1] = posx - 1;
y[1] = posy + 1;
//coordinates of 3rd location on grid
x[2] = posx ;
y[2] = posy + 1;
//coordinates of 4th location on grid
x[3] = posx + 1;
y[3] = posy + 1 ;
//coordinates of 5th location on grid
x[4] = posx ;
y[4] = posy + 2;
}
char X::name(void) const
{
return 'X';
}
int X::size(void) const
{
return 5;
}
//functions of shape S
S::S(int posx, int posy)
{
x = new int[4];
y = new int[4];
//coordinates of 1st location on grid
x[0] = posx;
y[0] = posy;
//coordinates of 2nd location on grid
x[1] = posx + 1;
y[1] = posy ;
//coordinates of 3rd location on grid
x[2] = posx + 1;
y[2] = posy + 1;
//coordinates of 4th location on grid
x[3] = posx + 2;
y[3] = posy + 1 ;
}
char S::name(void) const
{
return 'S';
}
int S::size(void) const
{
return 4;
}
//functions of shape U
U::U(int posx, int posy)
{
x = new int[5];
y = new int[5];
//coordinates of 1st location on grid
x[0] = posx;
y[0] = posy;
//coordinates of 2nd location on grid
x[1] = posx + 1;
y[1] = posy ;
//coordinates of 3rd location on grid
x[2] = posx + 2;
y[2] = posy ;
//coordinates of 4th location on grid
x[3] = posx ;
y[3] = posy + 1 ;
//coordinates of 5th location on grid
x[4] = posx + 2;
y[4] = posy + 1;
//coordinates of 6th location on grid
x[5] = posx ;
y[5] = posy + 2 ;
//coordinates of 7th location on grid
x[6] = posx + 2;
y[6] = posy + 2;
}
char U::name(void) const
{
return 'U';
}
int U::size(void) const
{
return 7;
}
output
$ ./a.out
X -2 5
X at (-2,5) (-3,6) (-2,6) (-1,6) (-2,7)
L -2 3
L at (-2,3) (-1,3) (-2,4)
L at (-1,2) (0,2) (-1,3)
no overlap
============================
$ ./a.out
U 3 -2
U at (3,-2) (4,-2) (5,-2) (3,-1) (5,-1) (3,0) (5,0)
O 3 1
O at (3,1)
O at (4,0)
no overlap
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.