Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Following CPP Files: //Matrix.H #Ifndef __Matrix_H__ #Define __Matrix_H__ Class Matrix { Public: // Creates An Empty Matrix Of Size 0 Times 0. Matrix(); // Creates

Following CPP Files: //Matrix.H #Ifndef __Matrix_H__ #Define __Matrix_H__ Class Matrix { Public: // Creates An Empty Matrix Of Size 0 Times 0. Matrix(); // Creates An Identity Matrix Of

Following CPP Files:

//Matrix.h

#ifndef __Matrix_H__ #define __Matrix_H__ class Matrix { public: // Creates an empty matrix of size 0 times 0. Matrix(); // Creates an identity matrix of size times . Matrix(int size); // Creates a matrix of size times filled with 0s. Matrix(int height, int width); // Copy constructor Matrix(const Matrix&); // Move constructor Matrix(Matrix&&); // Destructor ~Matrix(); int getWidth() const; int getHeight() const; void resize(int height, int width); void transpose(); // Copy assignment Matrix& operator=(const Matrix&); // Move assignment Matrix& operator=(Matrix&&); // Returns the value at the specified position in the matrix. long& operator()(const int row, const int col); long operator()(const int row, const int col) const; // Determines if two matrices are equal. bool operator==(const Matrix&) const; private: int width; int height; long** values; }; #endif

//end matrix.h

//CPP Test file

#include #include #include #include \"matrix.h\" using namespace std; void printSize(const Matrix &m) { cout } void printMatrix(const Matrix &m) { for (int h = 0; h { for (int w = 0; w { cout } cout } } Matrix getRndMatrix(int height, int width) { Matrix m(height, width); for (int h = 0; h { for (int w = 0; w { m(h, w) = rand() % 10; } } return m; } inline Matrix getRndMatrix() { int height = rand() % 7 + 1; int width = rand() % 7 + 1; return getRndMatrix(height, width); } int main() { srand (time(NULL)); // Copy and Move { cout cout

// Constructor // Move is forced here since the compiler often optimizes it away. Matrix m = move(getRndMatrix()); Matrix m2 = m; printMatrix(m); cout printMatrix(m2); cout // Assignment m = getRndMatrix(); m2 = m; printMatrix(m); cout printMatrix(m2); } // Equality { cout cout bool makeEqual = rand() % 2 == 0; Matrix m1 = getRndMatrix(); Matrix m2 = makeEqual ? m1 : getRndMatrix() /* very likely not equal */; printMatrix(m1); cout printMatrix(m2); cout cout }

}

//End CPP Test

//Start of Implementation file

#include // provides min() using namespace std; #include \"matrix.h\" Matrix::Matrix() { width = 0; height = 0; values = nullptr; } Matrix::Matrix(int size) : Matrix(size, size) { for (int s = 0; s { values[s][s] = 1; } } Matrix::Matrix(int height, int width) : Matrix() { resize(height, width); } Matrix::~Matrix() { if (values != nullptr) { for (int r = 0; r { delete[] values[r]; } delete[] values; } } int Matrix::getWidth() const { return width; } int Matrix::getHeight() const { return height; } void Matrix::resize(int hei, int wid) { long** tmpValues = values; values = new long*[hei]; for (int h = 0; h { values[h] = new long[wid]; for (int w = 0; w { values[h][w] = 0; }

} for (int h = 0; h { for (int w = 0; w { values[h][w] = tmpValues[h][w]; } } for (int h = 0; h { delete[] tmpValues[h]; } delete[] tmpValues; width = wid; height = hei; } long& Matrix::operator()(int row, int col) { return values[row][col]; } long Matrix::operator()(int row, int col) const { return values[row][col]; } // -------------------------------

//end of implementation file

Implement Here: C++ Please

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions