We are going to create class of Matrix. You have to write the definition of the following function given below in the Matrix class. class Matrix{ private: int noOfRows ; int noOfColumns; int ** data; public: Matrix(int noOfRows, int noOfColums); void displayData(); -Matrix(); Matrix(const Matrix & ref);You have to write down the definition of following functions. 1. Matrix(int noOfRows, int noOfColums) 2. void displayData0; 3. -Matrix(); 4.Matrix(const Matrix & ref)
#include <iostream>
using namespace std;
class Matrix
{
int noOfRows;
int noOfColumns;
int ** data;
public:
Matrix(int _noOfRows, int _noOfColums)
:noOfRows(_noOfRows), noOfColumns(_noOfColums),data(NULL)
{
data = new int*[noOfRows];
for (int i = 0; i<noOfRows; i++)
data[i] = new int[noOfColumns];
for (int i = 0; i<noOfRows; i++)
for (int j = 0; j<noOfColumns; j++)
{
cout << "Enter a value for [" << i << "][" << j << "] element: ";
cin>>data[i][j];
}
}
void displayData()
{
for (int i = 0; i < noOfRows; i++)
{
for (int j = 0; j < noOfColumns; j++)
{
cout << data[i][j] << " ";
}
cout << endl;
}
}
~Matrix()
{
delete[]data;
};
Matrix(const Matrix & ref)
{
noOfRows = ref.noOfRows;
noOfColumns = ref.noOfColumns;
data = new int*[noOfRows];
for (int i = 0; i<noOfRows; i++)
data[i] = new int[noOfColumns];
for (int i = 0; i < noOfRows; i++)
{
for (int j = 0; j < noOfColumns; j++)
{
data[i][j] = ref.data[i][j];
}
}
}
};
int main()
{
Matrix m(2, 3);
m.displayData();
Matrix m2(m);
cout << "\nCopy constructor:\n";
m.displayData();
}
Comments
Leave a comment