Question

In: Computer Science

I am trying to create a program That works with two other programs in c++ and...

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

Solutions

Expert Solution

//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();
}


Related Solutions

I am trying to create a makefile for the following program in Unix. The C++ program...
I am trying to create a makefile for the following program in Unix. The C++ program I am trying to run is presented here. I was wondering if you could help me create a makefile for the following C++ file in Unix and show screenshots of the process? I am doing this all in blue on putty and not in Ubuntu, so i don't have the luxury of viewing the files on my computer, or I don't know how to...
I am trying to create a basic shell program in C that runs 10 commands and...
I am trying to create a basic shell program in C that runs 10 commands and then quits. Only one word commands are required, like: cal, date, ls, ps, pwd, who, quit The part I am struggling with is passing the scanned command into my array in the child process to be executed using execvp(). Here is my code: #include <stdio.h> #include <string.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/wait.h> #include<readline/readline.h> #include<readline/history.h> #define MAX_CMD_NUMBER 10 int main() {    int i;    char...
I am trying to create a program that reads from a csv file and finds the...
I am trying to create a program that reads from a csv file and finds the sum of total volume in liters of liquor sold from the csv file by county and print that list out by county in descending order. Currently my program runs and gives me the right answers but it is not in descending order. I tried this:     for county, volume in sorted(sums_by_volume.items(), key=lambda x: x[1], reverse=True):         index +=1         print("{}. {} {:.2f}".format(county, sums_by_volume[county]))      When I run...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
I am having a trouble with a python program. I am to create a program that...
I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code. #!/usr/bin/env python3 #Arrival Date/Time Estimator # # from datetime import datetime import locale mph = 0 miles = 0 def get_departure_time():     while True:         date_str = input("Estimated time of departure (HH:MM AM/PM): ")         try:             depart_time = datetime.strptime(date_str, "%H:%M %p")         except ValueError:             print("Invalid date format. Try again.")             continue        ...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am trying to create a classified balance sheet and I am unsure what is involved...
I am trying to create a classified balance sheet and I am unsure what is involved when reporting the current assets, liabilities and owners equity?
I am working on a project for my Computer Science course. I am trying to create...
I am working on a project for my Computer Science course. I am trying to create a Battleship game where a user names two coordinates on a grid and is then told whether this results in a "Hit" or a "Miss". Once the ship has been hit a certain number of times (based on the size of the ship) the ship is sunk. I have written all the code but it now fails to execute when I try to run...
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
I am writing a program that will work with two other files to add or subtract...
I am writing a program that will work with two other files to add or subtract fractions for as many fractions that user inputs. I need to overload the + and - and << and >> opperators for the assignment. The two files posted cannot be modified. Can someone correct the Fraction.ccp and Frction.h file that I am working on? I'm really close. // // useFraction.cpp // // DO NOT MODIFY THIS FILE // #include "Fraction.h" #include<iostream> using namespace std;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT