Question

i created a two dimensional matrix class which holds my object complex number. I am trying...

i created a two dimensional matrix class which holds my object complex number.

I am trying to overload the parenthesis () so that I can access and modify the value at specified location of the matrix. say matrix[a][b]

= 5;

and my_vals is a private member of the matrix class. What is the problem with my code

//constructor & inlitilize dynamic array
matrix::matrix(int rows, int cols) {


complex** m_vals = new complex * [rows];
for (int i = 0; i < rows; ++i) {
m_vals[i] = new complex[cols];
}
// clean up and free the memory
for (int i = 0; i < rows; ++i) {
delete[]m_vals[i];
}
delete[]m_vals;
}

complex& operator()(int rows, int cols) {
return m_vals[rows][cols];
  

0 0
Add a comment Improve this question Transcribed image text
Answer #1


//constructor & inlitilize dynamic array
matrix::matrix(int rows, int cols) {
   // don't define m_vals as local variable as it is private member of the class matrix (complex **m_vals)
   m_vals = new complex *[rows];
   for (int i = 0; i < rows; ++i) {
       m_vals[i] = new complex[cols];
   }
  
   // no need to deallocate the matrix allocated
}

// define the below function in the matrix class

// overload subscript operator
// returns complex* when called for the first index i.e row since each row entry contains an array
// and when called for second index i.e column, returns the reference to the element
// to invoke this you need to use m[2][3] = 5;
// where m is of matrix object and 2 and 3 are index corresponding to row and column
// the above statement sets 5 to matrix entry of where row index = 2 and column index = 3
// index called must be valid.
complex *& operator[](int index){
return m_vals[index];
}

// overloading parenthesis operator

// returns the reference to the element at given indices
// to invoke this you need to use m(2,3) = 5;
// where m is of matrix object and 2 and 3 are index corresponding to row and column
// the above statement sets 5 to matrix entry of where row index = 2 and column index = 3
complex& operator()(int rows, int cols) {
return m_vals[rows][cols];
}

Add a comment
Know the answer?
Add Answer to:
i created a two dimensional matrix class which holds my object complex number. I am trying...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #ifndef __MATRIX__ #define __MATRIX__ #include "complex.h" namespace gtmath { class matrix { public: matrix(int , int...

    #ifndef __MATRIX__ #define __MATRIX__ #include "complex.h" namespace gtmath { class matrix { public: matrix(int , int );    //TODO: Add implementation here    //TODO: Implement parenthesis operator complex& operator()(int, int) { return m_vals[rows][cols]; } friend std::ostream& operator<<(std::ostream& os, gtmath::matrix& m);    int get_num_cols(); int get_num_rows();          private: complex** m_vals; int rows; int cols; }; } #include "matrix.h" #include <limits> namespace gtmath { //constructor & inlitilize dynamic array matrix::matrix(int rows, int cols) { this->cols = cols; this->rows = rows;...

  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • Please help me out with this assignment. Please read the requirements carefully. And in the main...

    Please help me out with this assignment. Please read the requirements carefully. And in the main function please cout the matrix done by different operations! Thanks a lot! For this homework exercise you will be exploring the implementation of matrix multiplication using C++ There are third party libraries that provide matrix multiplication, but for this homework you will be implementing your own class object and overloading some C+ operators to achieve matrix multiplication. 1. 10pts] Create a custom class called...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a...

    NO VECTORS PLEASE Start a simple Matrix template class In this lab, you’ll be writing a template class to represent a matrix. Because it’s a template, your matrix class will be able to work with different types of underlying data. Start by creating a new file matrix.hpp. Inside this file, start to write your matrix template class. Here’s a start for the class definition: template <class T> class Matrix { private: int _rows; int _cols; T** data; public: Matrix(int rows,...

  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • Context of my question: I am reading C#. Trying to understand Static keyword. I understand that...

    Context of my question: I am reading C#. Trying to understand Static keyword. I understand that when Static is applied to a member of a class, it can only be modified by the class and not the class object references. I will take an example here. public class BluePrint {    public static string BluePrintCreatorName; } If I need to know the BluePrintCreator's Name, I can call BluePrint.BluePrintCreatorName; But if a house that is created from the blueprint wants to...

  • C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the...

    C++ CODE: The matrix class consists of two files: matrix.h and matrix.cpp. The class has the following definition: matrix -size:int -mat:int** ---------------- +matrix(file:string) +~matrix() +operator +(add:matrix*):matrix & +operator-(sub:matrix*):matrix & +friend operator<<(out:ostream &, t:const matrix&):ostream & +displayRow(r:int):void The class variables are as follows: • size: the number of rows and columns in a square matrix • mat: the integer matrix that contains the numeric matrix itself. The methods have the following behaviour: 2 • matrix(file:string): The default constructor. It receives the...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT