CECS 282
LAB ASSIGNMENT 8
Assigned date: 11/12
Due date: Tue 11/19

30 points

Problem 1 [20 points] Redo the matrix problem in the lab assignment 5 using overloading operator.

a. Implement the following overloading operator functions:

Matrix (int rowSize, int colSize);
~Matrix ();
Matrix operator + (Matrix & m);
Matrix operator += (Matrix & m);
Matrix operator += (const int &num);
Matrix operator * (Matrix & m);
Matrix operator ++();
friend Matrix operator +(const int &num, const Matrix &m);
friend istream& operator>> (istream& in, const Matrix& m);
friend ostream &operator<<(ostream &os, const Matrix &m);

b. Test your Matrix class withe following main function:

cout << "Matrix 1:" << endl;
Matrix matrix1 (2, 2);
cin>>matrix1;
cout<<matrix1<<endl;

cout << "Matrix 2:" << endl;
Matrix matrix2 (2, 2);
cin>>matrix2;
cout<<matrix2<<endl;

// Instantiation and setup of matrix3
cout << "Matrix 3:" << endl;
Matrix matrix3 (2, 2);
cin>>matrix3;
cout<<matrix3<<endl;
cout << "Result of matrix1 + matrix2: " << endl;
Matrix addResult (2, 2);
addResult = matrix1 + matrix2;
cout<<addResult<<endl;
cout << "Result of matrix1 * matrix3: " << endl;
Matrix product(2,2);
product = matrix1 * matrix3;
cout<<product<<endl;
cout<<"++ operator: "<<endl;
Matrix matrix4(2,2);
matrix4 = ++matrix1;
cout<<"Matrix 4:"<<endl;
cout<<matrix4<<endl;
cout<<"Matrix 1 after incremment by one:"<<endl;
cout<<matrix1<<endl;
matrix1 = 2 + matrix1;
cout<<"Matrix 1 after incremment by constant:"<<endl;
cout<<matrix1<<endl;
cout<<"Use += with a matrix"<<endl;
matrix1 += matrix2;
cout<<matrix1<<endl;
cout<<"Use += with a constant"<<endl;
matrix1 += 5;
cout<<matrix1<<endl;

Output

Matrix 1:
Enter the array:
2
3
5
1
Output:
2 3
5 1

Matrix 2:
Enter the array:
5
5
4
4
Output:
5 5
4 4

Matrix 3:
Enter the array:
3
5
1
1
Output:
3 5
1 1

Result of matrix1 + matrix2:
Output:
7 8
9 5

Result of matrix1 * matrix3:
Output:
9 13
16 26

++ operator:
Matrix 4:
Output:
3 4
6 2

Matrix 1 after incremment by one:
Output:
3 4
6 2

Matrix 1 after incremment by constant:
Output:
5 6
8 4

Use += with a matrix:
Output:
10 11
12 8

Use += with a constant:
Output:
15 16
17 13

Grading

Inside the program, put your name at the beginning of your program
//YOUR NAME
// CECS 282 LAB 8

Problem 2 - [10 points]

Create a class Polar that represents the points on the plain as polar coordinates (radius and angles). Create an overloaded + operator for addition of two Polar quantities. "Adding" two points on the plain can be accomplished by adding their X coordinates and then adding their Y coordinates. This gives the X and Y coordinates of the "answer." Thus you'll need to convert two sets of polar coordinates to rectangular coordinates, add them, then convert the resulting  rectangular representation back to polar.

Use the following main function to test the class Polar:

int main()
{
Polar p1(10.0, 0.0); //line to the right
Polar p2(10.0, 1.570796325); //line straight up
Polar p3; //uninitialized Polar

p3 = p1 + p2; //add two Polars

cout << "\np1="; p1.display(); //display all Polars
cout << "\np2="; p2.display();
cout << "\np3="; p3.display();
cout << endl;
return 0;
}

Output

p1=(10, 0)
p2=(10, 1.5708)
p3=(14.1421, 0.785398)


GRADING