want to declare arrays by using constructor with variables - c++

class matrix
{
public:
matrix();
matrix(int row, int column);
~matrix();
private:
const int DEFAULT_SIZE;
int size_row, size_column;
double *entry;
};
// main function
int
main()
{
//make a matrix of default size
matrix A; /* no error */
delete A;
//make 5 matrices of default size
matrix *B = new matrix [5]; /* no error */
delete [] B;
//make a matrix of size 15x15
matrix C(15, 15); /* no error */
delete C;
//make 5 matrices of size 15x15
matrix *D = new matrix(15, 15) [5]; /* compile error !! */
return 0;
}
//Define functions
matrix::matrix() : DEFAULT_SIZE(10)
{
size_row = DEFAULT_SIZE;
size_column = DEFAULT_SIZE;
entry = new double [size_row*size_column];
}
matrix::matrix(int row, int column) : DEFAULT_SIZE(10)
{
size_row = row;
size_column = column;
entry = new double [size_row*size_column];
}
matrix::~matrix()
{
delete [] entry;
}
I'm studying about constructor.
I'd like to declare arrays by using constructor with variables.
Can you correct my code line?
Please see the line below '//make 5 matrices of size 15x15'
Also, in this case, how can I use destructor?

To make an array of 5 elements that each get constructed with (15,15), you could do the following in C++11:
matrix* D = new matrix[5]{
{15,15}, {15,15}, {15,15}, {15,15}, {15,15}
};
But it would be much simpler to just use a vector:
std::vector<matrix> D(5, {15,15}); // C++11
std::vector<matrix> D(5, matrix(15,15)); // pre-C++11

After looking at your source code here is what I have seen! In your class for your private members your row and column sizes should not be of type int, they should be unsigned int. The reason they should be unsigned int is because an int without an unsigned is a signed value by default which means it can have negative numbers. For the amount of rows and columns in a matrix that does not make sense. With an unsigned int value it ranges from [0, max_value] of an unsigned int. Another thing to take care in is what if someone entered in 0 for either the row or column size or both? Then you would still not have a valid matrix. A little logic needs to be done here; if someone enters in a 0 for either then you have to make a decision as what the default behavior should be. Do you want to use the default value of 10? Also a value of 1x1 does not make sense either! Now you could have 1x4 or 4x1 then these would not be exactly a Matrix but they would be vectors that are either row or column major in order. In my honest opinion your default matrix size from the base constructor without any arguments should be 2x2. If a user specifies 1 for either parameter then the other parameter should be greater then 1. Also there is no need for 2 constructors here, this is duplicate code and can be done with just 1 constructor. Also there is no need to call your destructor, this gets automatically called when this class object goes out of scope.
For matrices would you think that the elements should be public for easy access?
Right now as it stands your (element) array is private meaning no outside class can access them, now if this is the behavior you want then you will need functions to access them and to set them if the values need to change.
Based on your class implementation here is what I did to show you the changes I have mentioned above.
#include <vector>
#include <array>
class Matrix {
private:
const unsigned int DEFAULT_SIZE;
unsigned int size_row, size_column;
double* elements;
public:
explicit Matrix( unsigned int row = 0; unsigned int column = 0 );
~Matrix();
// Copy Constructor
Matrix( const Matrix& c );
// Operator = Needed
Matrix& operator=( const Matrix& c );
}; // Matrix
Matrix::Matrix( unsigned int row, unsigned int column ) : DEFAULT_SIZE(2) {
// Check if both are 0 Or both are 1 - use default sizes
if ( (row == 0 && column == 0) ||
(row == 1 && column == 1) ) {
row = DEFAULT_SIZE;
column = DEFAULT_SIZE;
}
// Check [Row,Column] For [0,1] && [1,0]
if ( row == 0 && column == 1 ) {
row = 1;
column = DEFAULT_SIZE;
} else if ( row == 1 && column == 0 ) {
row = DEFAULT_SIZE;
column = 1;
}
size_row = row;
size_column = column;
element = new double[size_row * size_column];
} // Matrix
Matrix::~Matrix() {
delete [] elements
} // ~Matrix
Matrix::Matrix( const Matrix& c ) : DEFAULT(2) {
this->size_row = c.size_row;
this->size_column = c.size_column;
this->element = c.element;
} // Matrix(copy)
Matrix& Matrix::operator=( const Matrix& c ) {
// Assignment Can Only Happen If Both Matrices Are Of The Same Dimensions:
// You can not set a 4x3 = 6x9 for this does not make sense
if ( (this->size_row == c.size_row) &&
(this->size_column == c.size_column) ) {
this->element = c.element;
return *this;
} // operator=
As for the code in your main function I see a few errors that need to be fixed. The first two lines:
Matrix A;
delete A;
You are declaring Matrix A; then on the next line you are calling delete on A.
This is your first error: A is an instance of Matrix. This is a local stack variable that belongs to the scope of the main function. There is no need to call delete on this! When your instance of Matrix (A) goes out of scope the Matrix::~Matrix() is called implicitly for you behind the scenes, there is no need to invoke it.
Your next two lines of code:
Matrix* B = new Matrix[5];
delete [] B;
These two are correct and there are no problems here. You instance B is a pointer to a type object Matrix and because new is used here it is created on the heap and because you have [5] B is not just a pointer to this type but is a pointer on the heap to the first address of you dynamically allocated array. Then you call delete with the [] operator on B which is correct.
Your next two lines of code:
Matrix C(15,15);
delete C;
You are declaring an instance of a Matrix object called C on the stack where you are now using your defined constructor. Again on the second line here there is no need to call delete on C.
As for your last 2 lines of code this is not as simple as trying to do want you want but isn't all that trivial. In order for you to create your dynamic array of Matrices that all have a default size of 15x15 needs a little more work. In order to do this properly you can see from the class implementation that I added a Copy Constructor and an overloaded = operator. There are three steps to achieve this.
// First Step - Create A Single Stack Instance With Size [15,15]
Matrix D = Matrix( 15, 15 ); // This Will Be Used Later To Populate Your Array
// Second Step - Create An Array of 5 Matrices On The Heap
Matrix* E = new Matrix[5]; // Right Now All Five Are Using Default Size 2x2.
// Step Three Now We Update Them Using Our operator=()
for ( unsigned int i = 0; i < 5; i++ ) {
E[i] = D;
}
// Then Delete the Array
delete [] E; // Only Need To Delete E
However This WILL NOT WORK: Due to how our operator=() is defined! So how do we achieve this? There are two ways and both work. So You Can Remove Matrix E, the for loop and the delete [] E lines. Leave the Matrix D for that will be needed!
The First is easier: If you noticed I included both vector and array from the STL for this case I will use the vector class since it is the most commonly used.
// We already have: Matrix D = Matrix( 15, 15 );
// You Want an Array of 5
std::vector<Matrix> vMatrices; // This will create a vector of stack Matrices but at first it will be "EMPTY"
for ( unsigned int i = 0; i < 5; i++ ) {
vMatrices.push_back( D );
}
// Now You Have A vector of 5 Matrices.
// If You Want Vector of Pointers
std::vector<Matrix*> vpMatrices;
for ( unsigned int i = 0; i < 5; i++ ) {
vpMatrices.push_back( &D );
}
// To clean up your vectors you can simply do
vMatrices.clear();
vpMatrices.clear();
// The Other Way Would Be To Use std::array but it is less commonly used.
// If You know you will only ever need a set amount this works good
std::array<Matrix, 5> aMatrices;
for ( unsigned int i = 0; i < 5; i++ ) {
aMatrices.at(i) = D;
}
// For Holding Pointers
std::array<Matrix*, 5> apMatrices;
for ( unsigned int i = 0; i < 5; i++ ) {
apMatrices.at(i) = &D;
}
// To Clean Up std::array To Be Honest Not really sure for I do not use them
// But using it here just as an illustration that there are plenty of predefined containers
// to use to hold multiple elements of the same data type.
As a final note about your class, you are creating your size of your matrices and have the elements array defined based on size, however being that everything is private, you have no way to populate it or to access it. Keeping it private means you would have to implement functions to do this and considering the nature of matrices either for them to "Store" data or "Function Calls" or Being a Mathematical Object of Calculations and Operations. You do not want the overhead of accessory functions for this would be a performance hit. There is nothing wrong with keeping the size of the row and columns private, for there is no need to access these outside of the class once they have been defined. But your single dimensional array that is representing your MxN matrix should be publicly defined. Also you should have operator[] defined as well.
If you want to see how a basic math library implements matrices take a look at GLM's math library that is written to work with OpenGL shader language GLSL.
This library is an Headers Only Library. No need to link or include *.dlls or *.libs. All you need to do is store it into a folder on you computer and set you system to have an environment variable that points to its main folder. Then in you IDE if you are using VS all you have to do in the additional include section of the projects properties is use the environment variable defined. This is for using it! But to just look over its contents you can just down load it to any folder and open any of the *.h files. Then you can see how they implemented Matrix classes! You will notice that they are template types and they have predefined types of matrices such as mat2x2, mat2x3, mat2x4, mat3x2, mat3x3, mat3x4, mat4x2, mat4x3, & mat4x4. This is do to the nature of writing Programs and Shaders that are focused on 2D and 3D Graphics so the min required is a 2x2 and the largest that is truly needed is a 4x4. They also have vector classes defined of different sizes, and your matrices can be constructed from different size vectors. But this should serve as a guideline. Here is their link
GLM

Related

Dynamic Memory Allocation Matrix

What is the best option to allocate memory for Matrix?
I need to allocate memory in the first Mat Constructor .
The second Mat Constructor needs 1 argument, when Matrix is considered 1D array, so the number of rows will be 1. I also have to allocate memory in this second constructor.
Matrix data should be double
this is what I have tried:
#include<iostream>
class Mat{
uint16_t rows;
uint16_t colls;
double *mData; // the 2D array// matrix
Mat(uint16_t r, uint16_t c){ // constructor with 2 parameters -
// alocating memory for matrix
this-> rows = r;
this-> colls = c;
this-> mData = new double [rows][colls];
}
Mat(uint16_t x){ //constructor with 1 parameter // alocating memory
//for the case when number of rows = 1 , so the matrix becomes 1D array[]
this-> rows = x;
this-> mData = new double [rows];
}
};
I'm not good at pointers, so i can't figure out what I am missing.
"Array size is not a constant expression"
--UPDATE --
Full request:
Create the Mat class by implementing its contents as follows:
The class must contain 2 private attributes called mCols andmRows.These are the dimensions of the matrix, and their type is a 16-bit integer without a 16-bit mark.
The class must contain 1 private attribute named mData. This is a pointer to fractional numbers on double precision, and the memory for this pointer will be allocated in the constructor.
A constructor that takes as an argument two unmarked integers that represent the number of lines and the number of columns of an array and allocates memory to store these values.
A second constructor to take as an argument a single number. In this case, the array is considered a vector, the number of lines will be by default 1 and the received argument represents the number of columns. Of course, the allocation of memory to be able to store numbers must also be done in this case.
A third constructor who doesn't take any arguments. It will only initialize the integer attributes with the value of 0.
A fourth constructor, which will be the copy constructor. It receives as an argument an object of type const Mat & and its role is to copy all the values of the attributes of the given object as a parameter in the current object (this).
New code:
class Mat{
private:
uint16_t mRows;
uint16_t mCols;
double *mData;
public:
Mat(uint16_t r, uint16_t c){
mRows = r;
mCols = c;
mData = new double[mRows * mCols];
}
Mat(uint16_t c){
mCols = c;
mRows = 1;
mData = new double [1 * mCols];
}
Mat(){
mRows = 0;
mCols = 0;
}
Mat(const Mat &mat) : mRows(mat.mRows), mCols(mat.mCols), mData(mat.mData){
}
};
First of all, you need to use double** instead of double* for implementing 2d array
Secondly, I guess you need public constructors(now you have private)
And of course is better to use std::vector, but if you don`t want to, you can use my suggested code:
Mat(uint16_t r, uint16_t c){ // constructor with 2 parameters -
// alocating memory for matrix
this-> rows = r;
this-> colls = c;
this->mData = new double*[rows];
for(int i = 0; i < rows; i++) {
mData[i] = new double[colls];
}
}

C++ Set 2d array sizes of class member array in constructor

I only found solutions for 1d arrays, but couldn't apply them to 2d arrays.
The possible solutions included "vectors", "templates", and "pointers to arrays".
I know I can get vectors to work, but I would rather use either of the other 2. Preferably templates because I don't want to manually destruct, but pointers work too. (the pointer would be pointed to an array created in the constructor).
The class contains an empty 2d array called screen. The constructor is supposed to set its size. I tried too many things for me to list them all here, but I'll show what I currently have. (last thing i tried were pointers to arrays created in the constructor. in this case screen was a char pointer)
Screen::Screen(const int w, const int h) : screen(new char[h][w]) {} {
width = w;
height = h;
}
array size in new-expression must be constant
I failed implementing either of those strategies and received many kinds of errors while trying to make it work. How would I solve this problem? (primarily I want to know how to do this with templates. if not possible then with pointers to arrays created in the constructor)
The question was a little ambiguous, but it sounds like you want to dynamically allocate an array given some input.
Edit I changed the answer to match the code you provided. This creates a 2d array of chars given the height and width.
class Screen {
private:
char **data;
int rows;
int columns;
public:
Screen(int num_rows, int num_cols);
};
Screen::Screen(int num_rows, int num_cols) {
data = new char * [num_rows];
for (int i = 0; i < num_rows; ++i) {
data[i] = new char[num_cols];
}
rows = num_rows;
columns = num_cols;
}
This creates an empty 2D array of chars.
Explanation: All arrays in c are just pointers to the first block in memory of the type you have declared. By having the member variable as double pointer, you have an array of char pointers, which each point to the first value in each of their respective arrays.
BUT be careful, you WILL need to free the data variable to avoid memory leaks, by declaring a destructor.
Screen::~Screen() {
for (int i = 0; i < rows; ++i) {
delete[] data[i];
}
delete[] data;
}

Initializing a two-dimensional object array in c++

I know this has been asked before (or some similar variations) however I cannot get it to work.
I am trying to create a board game that is composed of a board filled with squares. I am trying to model my board as a 2d array of Square objects. The board can be created with any width or height, and those parameters are passed in through the constructor. Here is the code I am working with:
Board.h
#ifndef BOARD_H
#define BOARD_H
#include "Square.h"
class Board{
public:
Board(int w, int h);
private:
Square **squares;
const int width;
const int height;
};
#endif
Board.cpp
#include "Board.h"
Board::Board(int w, int h):width(w), height(h) {
squares = new Square*[width];
for (int i = 0; i < width; i++) {
squares[i] = new Square[height];
}
}
However, when I try to compile this I get an error which seems to indicate the squares[i] = new Square[height] is trying to call the default constructor for the Square object (which I do not want to exist nor call in this case).
Board.cpp: In constructor ‘Board::Board(int, int)’:
Board.cpp:7:33: error: no matching function for call to ‘Square::Square()’
squares[i] = new Square[height];
Any ideas? Is this possible within C++?
Your code is equivalent to this:
struct Foo
{
Foo(int){} // no default constructor
// Foo() = default; /* uncomment this and it will work */
};
int main()
{
Foo* pFoo = new Foo[10]; // need a default ctor
delete[] pFoo;
}
The problem is that on the rhs of Foo* pFoo = new Foo[10];, you are allocating the memory as well as creating 10 Foo objects. The compiler doesn't know how to do the latter (creating the objects), as you don't provide a default constructor. To make the above code work as is, you need to specify all arguments for the non-default ctor of each object, like:
Foo* pFoo = new Foo[10]{1,2,3,4,5,6,7,8,9,0}; // this will work
The better alternative is to use std::vector instead. If you wonder why the latter works without the need for objects that don't have a default constructor, it is because it uses the placement new, and initializes the elements on demand.
Here you can see how to do it with placement new.
EDIT
The code
Foo* pFoo = new Foo[10]{1,2,3,4,5,6,7,8,9,0};
compiles in gcc, but clang fails to compile it (with -std=c++11 flag). Follow up question here.
You asked it to construct a buffer of n Squares. It tried to do so, and failed, becuase it could not construct them.
If you want memory for n squares, use new std::aligned_storage_t<sizeof(Square),alignof(Square)>[Count]. Then use placement new to construct each element in turn.
But that is a dumb idea. It is complex, opaque, and error prone.
In short, Stop managing memory manually.
vector solves this problem for you.
Create a vector of vector of squares. Or create a flat vector, and do math to find the index. It does the aligned storage bit internally, and uses placement new to construct on demand, so you do not havr to write that dangerous tricky code yourself.
Vectors let you grow them dynamically and efficiently, and replace manually managed arrays nicely. But you need to grow them from the least to the greatest element. If you do not want that, use vectors of unique ptrs to Squares, and allocate as needed (or, std::experimental::optional<Square> if you use C++17/1z compilers).
Or just use a map from pair<int,int> to Square and make it sparse. Note that you'll beed to emplace and not call [] if you want no default ctor for a Square. Again, you can use unique ptrs to Square instead of Squares directly.
There are many, many options.
Also consider having a default Square ctor: regular types are awesome.
As another answer mentioned, you could create a flat vector and do some simple arithmetic to calculate the index.
int main() {
// dimensions of the board
int width = 100;
int height = 102;
// some coordinates on the board
int x = 10;
int y = 32;
// allocate memory for every element on the board (10200 elements)
int * board = new int[width * height];
// access element (x, y) of the board
int val = board[y*width + x]
// don't forget to delete dynamic memory!
delete [] board;
}
No two distinct coordinates will have the same index.

Initializing an internally allocated matrix

I have to write a constructor for implementing a function for initializing an internally allocated matrix. The given code looks like (only constructor):
Matrix(const float* m, size_t n) : _n(n), _m(0lu)
{
//Missing
}
So, my first question is: What does the part behind the ':' mean (_n(n), _m(0lu))?
Furthermore, as far as I know, I need a return pointer to the memory I am allocating. Is this correct? My first idea was to use posix_memalign(...). Would this be correct?
Thank you very much!
I am assuming this basic object:
class Matrix
{
// stuff
private:
size_t _n;
float* _m;
}
The part of the constructor is an initialization list. It is synonymous to wiring:
Matrix(const float* m, size_t n)
{
_n = n;
_m = 0lu;
}
Here is a good decription, why you want to use them: [10.6] Should my constructors use "initialization lists" or "assignment"?
But that does not solve your initial problem: "function for initializing an internally allocated matrix"
What the constructor does is copy the size (n) and initialize the pointer to NULL. (NULL is synonymous with 0 [1]) So you need some way to internally allocate and initialize.
I have one problem with the Matrix class. Normally a matrix has 2 dimension, so either it is a NxN matrix or n is the element count and we have no idea what dimension the matrix is. I will assume that it is NxN matrix, since this is quite often used in computer graphics.
Step 1: internally allocated
So, allocate some memory:
_m = new float[n*n];
This can replace the assignment to NULL, since why should it be first set to NULL and then change right after.
Step2: initialized
Assuming that the calling code put sufficient data into m, just use memcpy:
std::memcpy(_m, m, n*n*sizeof(float));
If you feel masochistic, you can also copy the elements each:
for (unsigned int i = 0; i < n*n; i++)
{
_m[i] = m[i];
}
So your final constructor looks like so:
#include <cstring>
Matrix(const float* m, size_t n)
: _n(n), _m(new float[n*n])
{
std::memcpy(_m, m, n*n*sizeof(float));
}
Finally, since you allocated memory you should not forget to delete it in the destructor:
Matrix::~Matrix()
{
delete [] _m;
}
Note the array deleting operator.
[1] In C++11 and C99 this is not fully true under certain circumstances, but these are details and irrelevant.
:_n(n), _m(0lu)
is member internalizer list. Means _n(which seems to be member variable) is assing n to it and simialr for _m
More details here

How to access a dynamically allocated matrix in c++?

I have created a dynamic matrix of class objects but i have made a big mess with handling the returned pointers.
My intention is to create a matrix of class Point( Int x,Int y) and later to use it in different ways in the program.
Everything is working but i can't figure out the returned pointers game between the functions.
class Point
{
private:
int x;
int y;
public:
Point(int x,int y);
void SetPoint(int x,int y);
};
In a second class I use a Point object as class member.
Init_Pallet() is used to Initialize the Matrix.
class Warehouse
{
private:
Robot r1,r2;
Point *Pallet_Matrix;
public:
Point* Init_Pallet();
};
This is the Init function
Point* Warehouse::Init_Pallet()
{
int rows =10,cols =10;
Point** Pallet_Matrix = new Point*[rows];
for (int i = 0; i < rows; i++)
Pallet_Matrix[i] = new Point[cols];
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; j++) //Pallet matrix Init, x point for robots amount in position and y for box amount
Pallet_Matrix[i][j].SetPoint(0,0);
return *Pallet_Matrix;
}
The Init function is called by WareHouse C'Tor (ignore the other vars)
Warehouse::Warehouse(Robot p_r1,Robot p_r2): r1(p_r1),r2(p_r2)
{
this->r1=p_r1;
this->r2=p_r2;
Point *p =Init_Pallet();
this->Pallet_Matrix=p;
}
My question is: How do I return the address to the beginning of the matrix from the Init function to the C'Tor who called it?
And second question: how do i access the matrix different locations in the format of Matrix[i][j] after returning the matrix adress to the C'Tor.
Thank you in advance for all the help and your time.
You should just have Init_Pallet return a Point** and then do return Pallet_Matrix;. Currently you're copying one of the Point*s that you allocated out of the function, so the copy is no longer part of a contiguous array that you can index.
Don't forget to delete[] the dynamically arrays in your destructor.
However, you should much prefer to use the standard library containers like std::array or std::vector. Then you don't need to worry about the dynamic allocation yourself and no pointers to get in a mess with.
If I were doing it, I would just have:
class Warehouse
{
public:
Warehouse() : Pallet_Matrix() { }
private:
Robot r1,r2;
std::array<std::array<Point, 10>, 10> Pallet_Matrix;
};
And that's it. No init needed. No dynamic allocation. No assigning 0 to every element (if you give Point a default constructor that zero-initialises). Done.
How do I return the address to the beginning of the matrix from the Init function to the C'Tor?
In case you would really need just an address of first element, pretty straightforward would be:
return &Pallet_Matrix[0][0];
how do i access the matrix different locations in the format of Matrix[i][j] after returning the matrix address
Init_Pallet is a member function, which could simply work with the Pallet_Matrix member directly. Otherwise, the Init_Pallet function could actually return Point**, which should however make you feel that something's wrong with this code.
Better[1] solution would be:
Define the default constructor for Point:
class Point
{
public:
Point() : x(0), y(0){}
...
Use std::vectors instead of dynamically allocated arrays:
class Warehouse
{
private:
std::vector< std::vector<Point> > Pallet_Matrix;
and instead of:
Point *p =Init_Pallet();
this->Pallet_Matrix=p;
you would simply use std::vector's constructor:
int rows = 10, cols = 10;
Pallet_Matrix = std::vector< std::vector<Point> >(rows, cols);
[1] Better = You don't want to handle the memory management on your own.
The problem is that the returned type of Init_Pallet() is wrong — its a row, not a matrix. And in the last line of Warehouse::Init_Pallet() you dereference the proper pointer to matrix obtaining the pointer to the first row of the matrix.
You need to write Point **Pallet_Matrix; in Warehouse, use Point** Warehouse::Init_Pallet() definition of Init_pallet(), and return Pallet_Matrix in the last line of Init_Pallet().
The notation Point *row means the row is "the array of points" or "the pointer to the beginning of the array of points". The notation Point **matrix means the matrix is "the array of pointers to the beginnings of the arrays of points" or "the pointer to the beginning of such an array".
First: are the dimensions really constant, or is this just an
artifact of your having simplified the code for posting? If
they are really constant, there's no need for dynamic
allocation: you can just write:
Point palletMatrix[10][10];
and be done with it. (If you have C++11, it's even better; you
can use std::array, and palletMatrix will have full object
semantics.)
If you do need dynamic indexes, the only reasonable way of
doing this is to write a simple matrix class, and use it:
class Matrix
{
int m_rows;
int m_columns;
std::vector<Point> m_data;
public:
Matrix( int rows, int columns )
: m_rows( rows )
, m_columns( columns )
, m_data( rows * columns, Point( 0, 0 ) )
{
}
Point& operator()( int i, int j )
{
return m_data[ i * m_columns + j ];
}
// ...
};
Trying to maintain a table of pointers to tables is not a good
solution: it's overly complex, it requires special handling to
ensure that each row has the same number of columns, and it
generally has poor performance (on modern machines, at least,
where locality is important and multiplication is cheap).
Note too that the actual data is in an std::vector. There are
practically no cases where a new[] is a good solution; if you
didn't have std::vector (and there was such a time), you'd
start by implementing it, or something similar. (And
std::vector does not use new[] either.)
EDIT:
One other thing: if you're putting Point in a matrix, you might
want to give it a default constructor; this often makes the code
simpler.