I need a Matrix class for a project I’m doing in C++ but I don’t know how to declare the constructor.
I know I need a height and a width. But how do I go about the part where I store the values? What’s the best practice?
I thought about having my constructor be:
Matrix::Matrix(int height, int width, int[] values)
and so the private attributes of my class would be height, width and? How can I say that I want to store a number of values that I don’t have yet?
I hope this makes sense… I’m finding it very hard to explain and that’s probably because I’m very confuse. I’m very new with C++, so any extra help will be appreciated.
I would really try to look for a book about OO design and their application in C++. As this question (besides hard to understand) shows a general misunderstanding of how object oriented languages work. I could explain it here in this topic; but that would be redundant and book authors can do it much better than me.
Basically what I understood from this question is that you know the sizes of your matrix, yet don’t know the values they will get when you create them?
In that case simply make a constructor with only the sizes. And initialize the matrix to some “NULL” value. (Possibly even set a private member to false, indicating it isn’t completely usable yet). The class and constructor would look like:
Simply said: the constructor only makes sure the memory is “allocated”. Then you use other functions to fill in the actual values.
If you wish to provide “something”, where you don’t know exactly what “something” is. But you do know it is a container of data – you should use standard iterators (or templates). This is the manner how c++ handles inserting of data to standard containers. The function would be like this:
But once again: I’d take a book as the question to me seems unclear and answered by books.