Question: please code the following using c++. Forward 10 Right 90 Forward 10 Right 90 Forward 10 Right 90 Forward 10 Right 90 To practice dynamically
please code the following using c++.
Forward 10 Right 90 Forward 10 Right 90 Forward 10 Right 90 Forward 10 Right 90
To practice dynamically allocated arrays and operator overloading, create a TurtleProgram class that supports the following functionality. For simplicity, use F instead of Forward and R instead of Right.
-
Constructors and destructor
-
Overload <<, so programs can be printed as below
-
Overload equality and inequality operators: operator== and operator!=. Two programs are == if all their
instructions are the same.
-
Overload the following operators: operator=, operator+ and operator+=. Adding 2 programs creates a
longer program.
-
Multiplying a program with an integer creates a larger program where the same program is repeated that many
times. Write the * and *= operators. Multiplying by 0 or negative numbers is not defined. You can silently ignore the operation, throw an error or handle it in a different way. Similarly, multiplying two TurtlePrograms is not defined.
-
Index 0 of a program is defined as the first string in the program (i.e. for program [F 10] index 0 is "F". Implement getLength, getIndex and setIndex so the program can be modified.
getLength() returns the number of strings in the program. getLength() for [F 10] would be 2
The data for the TurtleProgram must be in a private dynamically allocated array of just the right size. Normally, we would allocate a much larger array, but for this exercise, we are practicing dynamically resizing our data array.
int main() {
TurtleProgram tp1; cout << "tp1: " << tp1 << endl; TurtleProgram tp2("F", "10"); cout << "tp2: " << tp2 << endl; TurtleProgram tp3("R", "90"); tp1 = tp2 + tp3; cout << "tp1 now as tp2+tp3: " << tp1 << endl; tp1 = tp2 * 3; cout << "tp1 now as tp2 * 3: " << tp1 << endl; TurtleProgram tp4(tp1); cout << "tp4 is a copy of tp1: " << tp4 << endl; TurtleProgram tp5("F", "10"); cout << "tp5: " << tp5 << endl; cout << boolalpha; cout << "tp2 and tp5 are == to each other: " << (tp2 == tp5) << endl; cout << "tp2 and tp3 are != to each other: " << (tp2 != tp3) << endl; cout << "index 0 of tp2 is " << tp2.getIndex(0) << endl; tp2.setIndex(0, "R"); tp2.setIndex(1, "90"); cout << "tp2 after 2 calls to setIndex: " << tp2 << endl; cout << "tp2 and tp3 are == to each other: " << (tp2 == tp3) << endl; // need to write additional tests for += *= cout << "Done." << endl; return 0;
}
output:
tp1: [] tp2: [F 10] tp1 now as tp2+tp3: [F 10 R 90] tp1 now as tp2 * 3: [F 10 F 10 F 10] tp4 is a copy of tp1: [F 10 F 10 F 10] tp5: [F 10] tp2 and tp5 are == to each other: true tp2 and tp3 are != to each other: true index 0 of tp2 is F tp2 after 2 calls to setIndex: [R 90] tp2 and tp3 are == to each other: true Done.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
