Design a class to perform various matrix operations A matrix
Design a class to perform various matrix operations. A matrix is a set of numbers arranged in rows and columns. Therefore, every element of a matrix has a row position and a column position. If A is a matrix of 5 rows and 6 columns, we say that matrix A is of the size 5 X 6 and sometimes denote it as A5X6. Clearly, a convenient place to store a matrix is in a two-dimensional array. Two matrices can be added and subtracted if they have the same size. Suppose that A = [aij] and B =[bij] are two matrices of the size m n, in which aij denotes the element of A in the ith row and the jth column, and so on. The sum and difference of A and B is given by: A + B = [aij + bij] A - B = [aij -bij]
The multiplication of A and B (A * B) is defined only if the number of columns of A are the same as the number of rows of B. If A is of the size m n and B is of the size n t, then A * B = [cik] is of the size m x t and the element cik is given by the formula: cik = (a)i1( b)1k +( a)i2(b)2k + . . . + (a)in(b)nk
Design and implement a class matrixType that can store a matrix of any size. Overload the operators +, -, and * to perform the addition, subtraction, and multiplication operations, respectively, and overload the operator << to output a matrix. Also, write a test program to test various operations on matrices.
Remember, classes with pointer member variables must:
1. Explicitly overload the assignment operator
2.Include the copy constructor
3.Include the destructor
Solution
main.cpp
#include \"testMatrixType.h\"
#include \"matrixType.h\"
#include <iostream>
using namespace std;
int main(){
int rows, cols,x;
matrixType h,k,sum,sub,mul;
//Input Values from the user
cout << \"Enter the number of rows and columns of Matrix A\";
cin >> rows >> cols;
cout<<\"MATRIX\"<<rows<<\"*\"<<cols<<endl;
h=matrixType(rows,cols);
h.setMatrix();
cout << \"Enter the number of rows and columns of Matrix B\";
cin >> rows >> cols;
cout<<\"Matrix\"<<rows<<\"*\"<<cols<<endl;
k=matrixType(rows,cols);
k.setMatrix();
//Input for selecting Addition, Subtraction and Multiplication
do{
cout<<\"Enter 1. Addition.\"<<endl;
cout<<\"2. Subtraction.\"<<endl;
cout<<\"3. Multiplication.\"<<endl;
cin>>x;
switch(x){
case 1:
if (h.equal(k)){
sum= h+k;
}else{
cout<<\"Addition is not possible\ \";
}
break;
case 2:
if (h.equal(k)){
sub=h-k;
}else{
cout<<\"Subtraction is not possible\ \";
}
break;
case 3:
if(h.equate(k)){
mul=h*k;
}else
cout<<\"Multiplication is not possible\ \";
break;
default:
cout<<\"Give valid input please\ \";
break;
}
}while(x>0);
return 0;
}
matrixType.h
#ifndef MATRIXTYPE_H_
#define MATRIXTYPE_H_
#include<iostream>
class matrixType {
private:
//Three private member variables
int rowSize, colSize;
int** matrix;
public:
// Declarations of the member functions of class matrixType
//Constructors
matrixType();
matrixType(int, int);
matrixType(const matrixType &obj);
//General Member functions
void setMatrix();
bool equal(matrixType &obj);
bool equate(matrixType &obj);
//overloading operators
void operator=(const matrixType &matrix2) const;
matrixType operator+(const matrixType &matrix2) const;
matrixType operator-(const matrixType &matrix2) const;
matrixType operator*(const matrixType &matrix2) const;
friend std::ostream& operator<<(std::ostream&osobj, const matrixType &mat);
friend std::istream& operator>>(std::istream&isobj, const matrixType &matin);
//Destructor
virtual ~matrixType();
};
#endif /* MATRIXTYPE_H_ */
matrixType.cpp
#include \"matrixType.h\"
#include<iostream>
using namespace std;
//Default Constructor to Intialize private members of class
matrixType::matrixType() {
this->rowSize = 0;
this->colSize = 0;
this->matrix=0;
}
//Parameterized Constructor which performs deep copy of class member variables
//Purpose:- To Store the input into the object of class
//Pre-condition :- Class object is initialized to zero by the default constructor
//Post-condition:- Object is initialized with the parameters
matrixType::matrixType(int rowSize1, int colSize1) {
cout<<\"In Parameterized Constructor\";
this->rowSize = rowSize1;
this->colSize = colSize1;
matrix=new int*[rowSize];
for(int i = 0 ; i < rowSize ; i++ )
matrix[i] = new int [colSize];
}
//Copy constructor which performs deep copy of class member variables
//Purpose:- To run before any operations on matrices
//Pre-condition :- Resultant matrix is zero
//Post-condition:- We get the resultant Matrix
matrixType::matrixType(const matrixType &matrix2) {
cout<<\"In copy Constructor\";
rowSize = matrix2.rowSize;
colSize = matrix2.colSize;
matrix = matrix2.matrix;
}
//Overload outStream << operator
//Purpose:- To show output on screen
ostream& operator<<(ostream&osobj, const matrixType &mat)
{
cout << \"matrix is::\" << endl;
for (int i = 0; i < mat.rowSize; i++)
{
for (int j = 0; j < mat.colSize; j++)
{
osobj << mat.matrix[i][j];
}
cout << endl;
}
return osobj;
}
//Overload instream operator
//Purpose:- To take the input from user
istream& operator>>(istream&isobj, const matrixType &matin)
{
for (int i = 0; i < matin.rowSize; i++)
{
for (int j = 0; j < matin.colSize; j++)
{
isobj >> matin.matrix[i][j];
}
}
return isobj;
}
// member function setMatrix() to read matrix elements from console and initialize
//Purpose:- To read the values of matrices and set them inobject
void matrixType::setMatrix() {
int c, d;
cout << \"\ Enter the elements of matrix\ \";
for (c = 0; c < rowSize; c++)
for (d = 0; d < colSize; d++) {
cin>>this->matrix[c][d];
}
cout << \"\ \ Matrix :\ \ \";
for (c = 0; c < rowSize; c++) {
for (d = 0; d < colSize; d++) {
cout <<\"\\t\"<<this->matrix[c][d];
}
cout <<\"\ \ \";
}
}
//Equal member function to compare the row and column size of matrices for addition and subtraction
bool matrixType::equal(matrixType &h){
if (this->rowSize==h.rowSize && this->colSize==h.colSize)
return true;
else
return false;
}
//Equate member function to compare the row and column size of matrices for Multiplication
bool matrixType::equate(matrixType &h){
if (this->colSize==h.rowSize)
return true;
else
return false;
}
//Overloading Assignment Operator
//Purpose:- To perform assigning operations between the objects of class
void matrixType:: operator=(const matrixType &matrix2) const
{
cout <<\"Assign\";
for (int i= 0;i <matrix2.rowSize; i++)
{
for (int j = 0;j< matrix2.colSize;j++)
{
matrix2.matrix[i][j]=matrix[i][j];
}
}
}
//Overloading Addition Operator
//Purpose:- To perform addition of two objects
//Pre-condition :- Two class objects of matrixType
//Post-condition:- Output Object that is the sum of two objects
matrixType matrixType:: operator+(const matrixType &matrix2) const
{
cout <<\"Add\";
matrixType matrixSum(rowSize,colSize);
for (int i= 0;i <matrix2.rowSize; i++)
{
for (int j = 0;j< matrix2.colSize;j++)
{
matrixSum.matrix[i][j] = matrix[i][j] + matrix2.matrix[i][j];
}
}
cout << \"\ \ Matrix :\ \ \";
for (int c = 0; c < rowSize; c++) {
for (int d = 0; d < colSize; d++) {
cout <<\"\\t\"<<matrixSum.matrix[c][d];
}
cout <<\"\ \ \";
}
return matrixSum;
}
//Overloading subtraction operator
//Purpose:- To perform subtraction of two objects
//Pre-condition :- Two class objects of matrixType
//Post-condition:- Output Object that is the subtraction of two objects
matrixType matrixType:: operator-(const matrixType &matrix2) const
{
cout <<\"Subtract\";
matrixType matrixSum(rowSize,colSize);
for (int i= 0;i <matrix2.rowSize; i++)
{
for (int j = 0;j< matrix2.colSize;j++)
{
matrixSum.matrix[i][j] = matrix[i][j] - matrix2.matrix[i][j];
}
}
cout << \"\ \ Matrix :\ \ \";
for (int c = 0; c < rowSize; c++) {
for (int d = 0; d < colSize; d++) {
cout <<\"\\t\"<<matrixSum.matrix[c][d];
}
cout <<\"\ \ \";
}
return matrixSum;
}
//Overloading Multiplication Operator
//Purpose:- To perform multiplication of two objects
//Pre-condition :- Two class objects of matrixType
//Post-condition:- Output Object that is the multiplication of two input objects
matrixType matrixType:: operator*(const matrixType &matrix2) const
{
cout <<\"Multiplication\";
matrixType matrixM(rowSize,colSize);
for (int i= 0;i <this->rowSize; i++)
{
for (int k = 0;k< matrix2.colSize;k++)
{
matrixM.matrix[i][k]=0;
for (int j=0;j<matrix2.rowSize;j++){
matrixM.matrix[i][k] = matrix[i][j] * matrix2.matrix[j][k];
}
}
}
cout << \"\ \ Matrix :\ \ \";
for (int c = 0; c < rowSize; c++) {
for (int d = 0; d < colSize; d++) {
cout <<\"\\t\"<<matrixM.matrix[c][d];
}
cout <<\"\ \ \";
}
return matrixM;
}
//destructor which properly removes any resources and deallocates memory
//Purpose:- To empty the memory used , to make it avaible again
//Pre-condition:- Objects are present in memory
//Post-condition:- Memory is empty after removing resources
matrixType::~matrixType(){
for (int i = 0; i < rowSize; i++)
delete[] matrix[i];
delete[] matrix;
}
testMatrixType.h
#ifndef TESTMATRIXTYPE_H_
#define TESTMATRIXTYPE_H_
class testMatrixType {
public:
testMatrixType();
virtual ~testMatrixType();
};
#endif /* TESTMATRIXTYPE_H_ */





