I am new in C++. please correct me!.

Write your question here.

Question: Does the behavior of the following two codes are correct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

DataSource.cpp:
#include "DataSource.h"
#include "Memory.h"
DataSource :: DataSource()
{
numberOfRow = 4;
numberOfColumn = 4;
int mat[4][4] = { { 2, 4, 1, -3 },
{ -1, -2, 2, 4 },
{ 4, 2, -3, 5 },
{ 5, -4, -3, 1 } };
// For a large data file, it's essential to put the data elements from the file to heap due to filtering. 
// Once the filtered data elements is transfered to Matrix array, delete all the data element from dataElement array and free the space.
dataElement = allocateData2DbyNew(numberOfRow, numberOfColumn);
for (int row = 0; row < numberOfRow; row++)
{
for (int col = 0; col < numberOfColumn; col++)
{
dataElement[row][col] = mat[row][col];
}
}


}
DataSource :: ~DataSource()
{
destroyData2DbyDelete(dataElement, numberOfRow, numberOfColumn);
dataElement = nullptr;
}

Matrix.cpp:
#include "DataSource.h"
#include "Matrix.h"
#include "Memory.h"
#include <iostream>


Matrix :: Matrix()
{
DataSource *dataObj = new DataSource;
this->matrixElement = allocateData2DbyNew(dataObj->numberOfRow, dataObj->numberOfColumn);
//..Copy the data from Data Source to Matrix array. Filtarazation might necessary before copying. 
for (int row = 0; row < dataObj->numberOfRow; row++)
{
for (int col = 0; col < dataObj->numberOfColumn; col++)
{
this->matrixElement[row][col] = dataObj->dataElement[row][col];
std::cout << this->matrixElement[row][col] << " ";
}
std::cout << std::endl;
}
delete dataObj;
dataObj = nullptr;
}


Matrix :: ~Matrix()
{




}

well, given that can't compile and run them to actually see their behaviour, and that I have no idea what they are supposed to do, I'll say that sure, they're alright.
Is it correct? That depends on what it's supposed to do. Since we don't know what it's supposed to do, we can't say.

What it does is a little odd. When you instantiate a Matrix object, it copies a hard coded array to a 2D array on the heap. Then it copies that array to another array. Then it prints the third array. Then it deletes the second one.

That's a whole lot of copying.

Also, it's a little odd to have the constructor print out the contents. It would be better to add a print() method or << operator to Matrix and let the caller print the contents if want.
Topic archived. No new replies allowed.