CECS 282
LAB ASSIGNMENT 8
Assigned date: 11/27
Due date: 12/4
30 points

Pre-lab

Before doing this lab assignment, you need to do the following activities:

Problem 1 - [23 points]

Define a Matrix class to represent a dynamic matrix. Implement the following operations on the matrices using operator overloading.

#ifndef Matrix_h
#define Matrix_h
#include <iostream>
class Matrix
{
private:
int rows;
int cols;
int **M;

public:
Matrix(const int &rows, const int &cols);
Matrix(const Matrix &other);
~Matrix();
int* & operator[](const int &index) const;
void operator=(const Matrix &other);
Matrix operator -()const;
Matrix operator -(const Matrix &other)const;
Matrix operator +(const Matrix &other)const;
Matrix operator *(const Matrix &other)const;
Matrix operator *(const int &num)const;
Matrix operator++();
Matrix operator--();
int getMatrixRows();
int getMatrixCols();
friend Matrix operator *(const int & num, const Matrix &m)
friend Matrix operator +(const int &num, const Matrix &t)
friend ostream &operator<<(ostream &os, const Matrix &m)
friend istream& operator>> (istream& in, const Matrix& m);
};
#endif

Write a main function to test all overloading operators in the class Matrix.

GRADING

Test driver

int main()
{
Matrix m1(2, 2);
Matrix m2(2, 2);
Matrix m3(2, 2);
Matrix m4(2, 3);
Matrix m5(3, 2);
m1[0][0] = 2;
m1[1][1] = 2;
m2[0][0] = 1;
m2[1][1] = 2;
const Matrix s = -m1;
//a.
cout << m1 << endl << s << endl;
m3 = m1 + m2;
//b.
cout << m3 << endl;
m3 = m1 - m2;
//c.
cout << m3 << endl;
m3 = m1 * m2;
//d.
cout << m3 << endl;
m3 = 1 + m3;
//e.
cout << m3 << endl;
m2 = m3;
//f.
cout << m2 << endl;
m3 = 5*m3;
//g.
cout << m3 << endl;
//h
cout << m3[1][1] << endl;
m2=++m3;
//k
cout << m2 << endl;
//l
cout << m3 << endl;
cin >> m4;
cin >> m5;
m1 = m4*m5;
//m
cout << m1;
return 0;
}

Problem 2 - [7 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