Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7509585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:57:38+00:00 2026-05-29T22:57:38+00:00

I need a Matrix class for a project I’m doing in C++ but I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:57:40+00:00Added an answer on May 29, 2026 at 10:57 pm

    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:

    class Matrix() {
    public:
        Matrix(int nRows, int nColumns);
    private:
        int numRows;
        int numColums;
        std::vector<VALUE_TYPE> val;
    };
    Matrix::Matrix(int nRows, int nColums) {
        int numRows = nRows;
        int numColumns = nColums;
        std::vector<VALUE_TYPE> val(nRows * nColums);
    }
    

    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:

    template <typename InIt>
    Matrix::Matrix(int nRows, int nColums, InIt begin, InIt end) {
        int numRows = nRows;
        int numColumns = nColums;
        //[begin, end) now holds the data - manipulate it at will.
        std::vector<VALUE_TYPE> val(begin, end);
        val.resize(nColums * nRows);
    }
    

    But once again: I’d take a book as the question to me seems unclear and answered by books.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a sparse matrix class. I need to have a node class,
Note: I don't talk about sharing variables between Activities or the need to save
Need to know if such a 3d matrix would be created in the stack
I need to present the user with a matrix of which one column is
I need to write a program that uses matrix multiplication to rotate an image
I need to call the index of a matrix made using the linspace command,
I need a way to represent a 2-D array (a dense matrix) of doubles
I have a need for a sparse matrix in up to 4 dimensions in
I have a need to basically create a matrix of values in my Silverlight
I have a matrix in MATLAB and I need to find the 99% value

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.