In: Computer Science
I am trying to create a program That works with two other programs in c++ and a makefile.
Only Shape.cpp can be modified. and it needs to work on a unix machine. It isn't running on my machine. And gives me an error message that it doesn't recomize cin and endl
The program will accept a character and an X and Y coordinate. Dependign on the Charactor, It will then tell you what Cells that shape occupies.
I almost have the program working, but I am getting several bugs. Can someone fix the Shape.cpp file, and tell me what they did to fix it?
Thanks.
all: testShape
CXXFLAGS=-g -Wall
Shape.o: Shape.cpp Shape.h
testShape.o: testShape.cpp Shape.h
testShape: testShape.o Shape.o
$(CXX) -o $@ $^ $(LDFLAGS)
clean:
rm -f *.o testShape
// end of makefile
//
// testShape.cpp
//DO NOT MODIFY
#include "Shape.h"
#include
#include
using namespace std;
int main()
{
Shape *t1, *t2;
char ch;
int x,y;
try
{
cin >> ch >> x >> y;
t1 = Shape::makeShape(ch,x,y);
t1->print();
cin >> ch >> x >> y;
t2 = Shape::makeShape(ch,x,y);
t2->print();
t2->move(1,-1);
t2->print();
if ( t1->overlap(*t2) )
cout << "overlap" << endl;
else
cout << "no overlap" << endl;
delete t1;
delete t2;
}
catch ( invalid_argument &exc )
{
cout << exc.what() << ": " << ch << " "
<< x << " " << y << endl;
}
}
//end of testshape
//
// Shape.h
// DO NOT MODIFY
#ifndef SHAPE_H
#define SHAPE_H
class Shape
{
public:
virtual ~Shape(void);
virtual char name(void) const = 0;
virtual int size(void) const = 0;
void print(void) const;
void move (int dx, int dy);
bool overlap(const Shape &t) const;
static Shape *makeShape(char ch,int posx,int posy);
protected:
int *x, *y;
};
class O: public Shape
{
public:
O(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class I: public Shape
{
public:
I(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class L: public Shape
{
public:
L(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class S: public Shape
{
public:
S(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class X: public Shape
{
public:
X(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
class U: public Shape
{
public:
U(int posx, int posy);
virtual char name(void) const;
virtual int size(void) const;
};
#endif
//end of shape.h
// THIS IS THE FILE I NEED HELP WITH Shape.cpp
#ifndef SHAPE_CPP
#define SHAPE_CPP
#include "Shape.h"
// Constructor for O
O::O(int posx, int posy)
{
x = new int[1];
y = new int[1];
x[0] = posx;
y[0] = posy;
}
char O::name() const
{
return 'O';
}
int O::size() const
{
return 1;
}
// Constructor for I
I::I(int posx, int posy)
{
x = new int[2];
y = new int[2];
x[0] = x[1] = posy;
y[0] = posy;
y[1] = posy+1;
}
char I::name() const
{
return 'I';
}
int I::size() const
{
return 2;
}
// Constructor for L
L::L(int posx, int posy)
{
x = new int[3];
y = new int[3];
x[0] = x[2] = posx;
y[0] = y[1] = posy;
x[1] = posx+1;
y[2] = posy+1;
}
char L::name() const
{
return 'L';
}
int L::size() const
{
return 3;
}
// Constructor for S
S::S(int posx, int posy)
{
x = new int[4];
y = new int[4];
x[0] = posx;
x[1] = posx+1;
x[2] = posx+2;
x[3] = posx+3;
y[0] = y[1] = posy;
y[2] = y[3] = posy+1;
}
char S::name() const
{
return 'S';
}
int S::size() const
{
return 4;
}
/*
Constructor for X
the constructor initialises the cell co-ordinates
X is spread across 5 cells
*
* * *
*
The Numbering of cells is done from bottom-top and
left to right
(x[0], y[0]) is the position of bottom most cell
(x[1], y[1]) is the position of left most cell of the
2nd row from bottom
(x[2], y[2]) is the position of the middle cell in the
2nd row from bottom
....
*/
X::X(int posx, int posy)
{
x = new int[5];
y = new int[5];
x[0] = x[2] = x[4] =posx;
x[1] = posx-1;
x[3] = posx+1;
y[0] = posy;
y[1] = y[2] = y[3] = posy+1;
y[4] = posy+2;
}
char X::name() const
{
return 'X';
}
int X::size() const
{
return 5;
}
// Constructor for U
U::U(int posx, int posy)
{
x = new int[7];
y = new int[7];
x[0] = x[3] = x[5] = posx;
x[1] = posx+1;
x[2] = x[4] = x[6] = posx+2;
y[0] = y[1] = y[2] = posy;
y[3] = y[4] = posy+1;
y[5] = y[6] = posy+2;
}
char U::name() const
{
return 'U';
}
int U::size() const
{
return 7;
}
void Shape::print() const
{
//get the size of the object i.e no of cells across
which this is spread
int sz = size();
// get the type of object i.e O, L, X...
char n = name();
cout<
//Print all the cell co-ordinates for this
object
for(int i = 0 ; i < sz ; i++)
{
cout<<"("<
if(i != sz-1)
cout<<"
";
}
cout< }
void Shape::move(int dx, int dy)
{
int sz = size();
for(int i = 0 ; i < sz ; i++)
{
x[i] += dx;
y[i] += dy;
}
}
/*
This function checks if the object(this) and t
overlap
* First add all the cells of t to a set
* the iterate over all the cells of (this) and
check
if they are already present in the set
* If any of the cell is already present , objects
overlap
and return true
*/
bool Shape::overlap(const Shape &t) const
{
set > st;
int sz1 = t.size();
int sz2 = size();
for(int i = 0 ; i < sz1 ; i++)
{
st.insert(make_pair(t.x[i],
t.y[i]));
}
for(int i = 0 ; i < sz2 ; i++)
{
if(st.find(make_pair(x[i], y[i]))!=
st.end())
return
true;
}
return false;
}
/*
This function creates a new object of type ch
*/
Shape * Shape::makeShape(char ch, int posx, int posy)
{
if(ch == 'O')
return new O(posx, posy);
else if(ch == 'I')
return new I(posx, posy);
else if(ch == 'L')
return new L(posx, posy);
else if(ch == 'S')
return new S(posx, posy);
else if(ch == 'X')
return new X(posx, posy);
else if(ch == 'U')
return new U(posx, posy);
else
throw std::invalid_argument("Invalid syntax.");
}
// base class virtual destructor
Shape::~Shape()
{
delete [] x;
delete [] y;
}
#endif
//EXAMPLE PROGRAM FOR MULTIPLE INHERITANCE AND PROTECTED
//MEMBERS
//TWO OR MORE BASE CLASSES ONE DERIVED CLASS IS CALLED
//MULTIPLE INHERITANCE
# include <iostream.h>
# include <conio.h>
class employ
{
protected:
int eno,bs;
char name[20],eadd[20];
public:
void input()
{
cout<<"ENTER EMPLOY NUMBER, NAME, ADDRESS, BASIC SALARY
";
cin>>eno>>name>>eadd>>bs;
}
};
class allowance
{
protected:
int da,hra,cca;
public:
void accept()
{
cout<<"ENTER DA, HRA, CCA "<<endl;
cin>>da>>hra>>cca;
}
};
class deduction
{
protected:
int pf,it;
public:
void dedinput()
{
cout<<"ENTTER PF,IT "<<endl;
cin>>pf>>it;
}
};
class salary : public employ,public allowance,
public deduction
{
int tall,tded,gs,ns;
public:
void calc_sal()
{
tall=da+hra+cca;
tded=pf+it;
gs=bs+tall;
ns=gs-tded;
}
void output()
{
cout<<"EMPLOY NUMBER "<<eno<<endl;
cout<<"EMPLOY NAME "<<name<<endl;
cout<<"EMPLOY ADDRESS "<<eadd<<endl;
cout<<"BASIC SALARY "<<bs<<endl;
cout<<"DA IS "<<da<<endl;
cout<<"HRA IS "<<hra<<endl;
cout<<"CCA IS "<<cca<<endl;
cout<<"PF IS "<<pf<<endl;
cout<<"IT IS "<<it<<endl;
cout<<"TOTAL ALLOWANCES "<<tall<<endl;
cout<<"TOTAL DEDUCTIONS "<<tded<<endl;
cout<<"GROSS SALARY IS "<<gs<<endl;
cout<<"NET SALARY IS "<<ns<<endl;
}
};
void main()
{
salary s;
clrscr();
s.input(); //MEMBER FUNCTION OF
EMPLOY CLASS
s.accept(); //MEMBER FUNCTION OF
ALLOWANCE CLASS
s.dedinput(); //MEMBER FUNCTION OF
DEDUCTION CLASS
clrscr();
s.calc_sal(); //MEMBER FUNCTION OF
SALARY CLASS
s.output();
getch();
}