Starting from:
$30

$24

Homework # 5 Operator Overloading and Vectors Solution




You will rewrite your CPUProgram class so that uses vectors and operator overloading. Your CPUProgram class has the same pubic member functions as before but now it supports the following operators

• Operator[] that takes an integer and works like the getLine function. It returns the program line as a string.

• Operator+ that takes a CPUProgram and an instruction line as a string. It returns a new CPUProgram that has the original CPUProgram with appended last line from the parameter string.

• Operator+= that takes an instruction line as a string and appends the instruction to the end of the program.

• Operator+ that takes two programs and returns a new program that appends the second programs to the first one

• All comparison operators ==, !=, <, =, etc. All comparison operators compare the number of lines of the programs.

• Operator that prints the program

• Post and pre decrement operators - - that delete the last line of the program.

• Function call operator() that takes two integers and returns a new program that contains the instructions between the given integers.




You should keep your instruction strings in a vector<string data member. You may use any vector functions as you like.




Important Notes:

 There is no maximum number of lines limit.

 All error checking should be done, for example [] should exit the program if out of index error is encountered.

 Do not forget to indent your code and provide comments.

 You should submit your work to the moodle page. Use the following main function as one of the test cases. You will also provide other test cases of your own.

#include "requiredIncs.h"










int main(int argc, char** argv){




//////////////////////////////////////////////////////////////////////////

//command line parameters

const char* filename = argv[1];

int option = atoi(argv[2]);

//////////////////////////////////////////////////////////////////////////




//////////////////////////////////////////////////////////////////////////

//Testing class CPUProgram

//op []

CPUProgram myCPUProgram(option);




myCPUProgram.ReadFile(filename);




cout << myCPUProgram[0] << endl;

cout << myCPUProgram[myCPUProgram.size() - 1] << endl;




//op +

cout << ((myCPUProgram + "MOV R1, #45")[myCPUProgram.size() - 1]) << endl;




//op +=

myCPUProgram += "MOV R2, #50";

cout << myCPUProgram[myCPUProgram.size() - 1] << endl;




//op + <<

CPUProgram myOtherCPUProgram(option);

myOtherCPUProgram.ReadFile(filename);




cout << (myCPUProgram + myOtherCPUProgram) << endl;




//op COMP --

cout << (myCPUProgram == myOtherCPUProgram ? "DONE" : "FAIL") << endl; cout << (myCPUProgram <= myOtherCPUProgram ? "DONE" : "FAIL") << endl; cout << (myCPUProgram myOtherCPUProgram ? "FAIL" : "DONE") << endl;




--myOtherCPUProgram;




cout << (myCPUProgram != myOtherCPUProgram ? "DONE" : "FAIL") << endl; cout << (myCPUProgram = myOtherCPUProgram ? "DONE" : "FAIL") << endl; cout << (myCPUProgram < myOtherCPUProgram ? "FAIL" : "DONE") << endl;




//op ()

cout << myCPUProgram(5, 10) << endl;




//error check

cout << myCPUProgram[myCPUProgram.size()] << endl;




myCPUProgram += "";

cout << myCPUProgram[myCPUProgram.size() - 1] << endl;

//////////////////////////////////////////////////////////////////////////







return 0;

}

More products