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

  • SEARCH
  • Home
  • 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 4608912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:54:43+00:00 2026-05-22T00:54:43+00:00

I am working on an exercise in operator overloading. I’ve created a matrix class

  • 0

I am working on an exercise in operator overloading. I’ve created a matrix class and I am supposed to overload operators so I can do arithmetic on matrices efficiently.

My directions say that I am supposed to make two matrix arrays using the class constructor that has 2 parameters and a third matrix array that will be used to store the result of arithmetic using the default constructor (1 parameter).

Since I am going to use these arrays to overload operators they are going to need to be data members of the class (I think). However, I thought that classes were supposed to be as representative of real life things as possible so making a matrix class with multiple arrays doesn’t make sense to me (a matrix is only one matrix).

Am I misunderstanding classes or is there a different way to make additional matrices using the class constructor I am not thinking of? Thanks all, here is the code in question.

class matrix
{
    friend ostream& operator << (ostream&, const matrix&); // << overloader 

    private:
    int size // size indicates length of rows and cols, so size 3 means a 3 x 3 matrix
    int array[10][10];

    public:
    matrix(int);
    matrix(int, int);
};

matrix:: matrix (int sizeIn) //default constructor, use to make result matrix
{
    int MAX_SIZE = 10;

    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }

    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
             array[i][j] = 0;
}

matrix:: matrix (int sizeIn, int rangeIn) //use to make first 2 matrices that will be added
{
    int range;
    int MAX_SIZE = 10;
    int MAX_RANGE = 20;

    if (0 > sizeIn && sizeIn > 10)
    {
     size = MAX_SIZE;
    }
    else
    {
     size = sizeIn;
    }

    if (0 > rangeIn && rangeIn > 20)
    {
      range = MAX_RANGE;
    }
    else
    {
     range = rangeIn;
    }

    for (int i = 0; i < size; i++)
        for (int j = 0; j < size; j++)
            array[i][j] = (rand() % (2 * range + 1) - range); //random number for each index
}

ostream & operator << (ostream & os, const matrix & arrayPrint) // << overloader
{
    for (int i = 0; i < arrayPrint.size; i++)
    {
        cout << '|';
        for (int j = 0; j < arrayPrint.size; j++)
            {
            os << setw(4) << arrayPrint.array[i][j] << " ";
            }
        os << setw(2) << '|' << endl;
    }
return os;
}
  • 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-22T00:54:44+00:00Added an answer on May 22, 2026 at 12:54 am

    You are misunderstanding the question. You are required to make a Matrix class that has 1 two dimensional array and then use the constructor to make two different matrices and then add them and store the result into a third one. So you will end up with something like this

        matrix::matrix(int Size, int Range)
        {
             if(Range < 0 || Range > RANGE_MAX)
                 Range = RANGE_MAX;
    
             if(Size < 0 || Size > SIZE_MAX)
                 Size = SIZE_MAX;
    
            for (int i = 0; i < Size; i++)
                for (int j = 0; j < Size; j++)
                   array[i][j] = (rand() % (2 * Range + 1) - Range);
        }
    

    Then you will make two matrices A and B as follows

    matrix A(4,10); // Create a Matrix A size 4 with random entries in range of 10
    matrix B(3,8); // Create a Matrix B with size 3 with random entries in range of 8
    

    You will then overload the = operator and + operator to let you do the following (of course you need to check if the sizes allow the operation first)

    matrix C = A + B
    

    To overload the “=” operator

    matrix& matrix::operator=(const matrix &rhs)
    {
       // if we have A = B then A is me and B is the rhs
       // assuming all members are public you might need to write get/set functions
       size = rhs.size;         // my size equals the right hand side's size
       range = rhs.range;
       for(int i = 0; i < size; i++)
         for(int j = 0; j < size; j++)
            array[i][j] = rhs.array[i][j];
    
        return *this;   // return me
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a learning exercise in expression trees. I have this working code: class
I am working on an exercise which asks me to take a base class
I've been working through Practical Common Lisp and as an exercise decided to write
I'm working on building a pretty simple site mainly as an exercise in learning
Working with a SqlCommand in C# I've created a query that contains a IN
I am working on the exercise questions of book The Lambda calculus . One
While working on a simple programming exercise, I produced a while loop (DO loop
I'm working through a JPanel exercise in a Java book. I'm tasked with creating
I'm working through a book exercise to generate some random serial number, and here's
So I'm working through some initial chapter exercise of Real World Haskell and I

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.