Answer to Question #329779 in C++ for Faisal

Question #329779

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)

1
Expert's answer
2022-04-17T13:53:55-0400
#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();
}









Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS