Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

If you want to define Matrix Operations in a class like way. You need to look at a matrix as an object (not a thing that holds three matrixes).

###Matrix as an object.

Matrix as an object.

What are the properties of a matrix?

1) Width
2) Height
3) Values (Data)

What are the action that can be performed on a matrix.

1) Sum
2) Diff
3) Transpose

Looking at these properties I would define the following.

class Matrix
{
 std::size_t width;
 std::size_t height;
 std::vector<int> data;
 public:
 Matrix sum(Matrix const& rhs);
 Matrix diff(Matrix const& rhs);
 Matrix transpose();
};

You also need to define a couple of standard functions (or disable some standard functions).

class Matrix
{
 public:
 // Create a matrix (of specified width/height with all data set to zero
 Matrix(int width, int height);
 // Copy a matrix
 Matrix(Matrix const& copy);
 // Allow people to set matrixes via assignment
 Matrix operator=(Matrix const& copy);
 // Input and output operations so you can print a matrix
 // And read a matrix from a stream (stdin/stdout)
 friend std::ostream operator<<(std::ostream& str, Matrix const& output);
 friend std::istream operator>>(std::istream& str, Matrix& input);
};

Once you have defined these you should be all set.

If you want to define Matrix Operations in a class like way. You need to look at a matrix as an object (not a thing that holds three matrixes).

###Matrix as an object.

What are the properties of a matrix?

1) Width
2) Height
3) Values (Data)

What are the action that can be performed on a matrix.

1) Sum
2) Diff
3) Transpose

Looking at these properties I would define the following.

class Matrix
{
 std::size_t width;
 std::size_t height;
 std::vector<int> data;
 public:
 Matrix sum(Matrix const& rhs);
 Matrix diff(Matrix const& rhs);
 Matrix transpose();
};

You also need to define a couple of standard functions (or disable some standard functions).

class Matrix
{
 public:
 // Create a matrix (of specified width/height with all data set to zero
 Matrix(int width, int height);
 // Copy a matrix
 Matrix(Matrix const& copy);
 // Allow people to set matrixes via assignment
 Matrix operator=(Matrix const& copy);
 // Input and output operations so you can print a matrix
 // And read a matrix from a stream (stdin/stdout)
 friend std::ostream operator<<(std::ostream& str, Matrix const& output);
 friend std::istream operator>>(std::istream& str, Matrix& input);
};

Once you have defined these you should be all set.

If you want to define Matrix Operations in a class like way. You need to look at a matrix as an object (not a thing that holds three matrixes).

Matrix as an object.

What are the properties of a matrix?

1) Width
2) Height
3) Values (Data)

What are the action that can be performed on a matrix.

1) Sum
2) Diff
3) Transpose

Looking at these properties I would define the following.

class Matrix
{
 std::size_t width;
 std::size_t height;
 std::vector<int> data;
 public:
 Matrix sum(Matrix const& rhs);
 Matrix diff(Matrix const& rhs);
 Matrix transpose();
};

You also need to define a couple of standard functions (or disable some standard functions).

class Matrix
{
 public:
 // Create a matrix (of specified width/height with all data set to zero
 Matrix(int width, int height);
 // Copy a matrix
 Matrix(Matrix const& copy);
 // Allow people to set matrixes via assignment
 Matrix operator=(Matrix const& copy);
 // Input and output operations so you can print a matrix
 // And read a matrix from a stream (stdin/stdout)
 friend std::ostream operator<<(std::ostream& str, Matrix const& output);
 friend std::istream operator>>(std::istream& str, Matrix& input);
};

Once you have defined these you should be all set.

Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

If you want to define Matrix Operations in a class like way. You need to look at a matrix as an object (not a thing that holds three matrixes).

###Matrix as an object.

What are the properties of a matrix?

1) Width
2) Height
3) Values (Data)

What are the action that can be performed on a matrix.

1) Sum
2) Diff
3) Transpose

Looking at these properties I would define the following.

class Matrix
{
 std::size_t width;
 std::size_t height;
 std::vector<int> data;
 public:
 Matrix sum(Matrix const& rhs);
 Matrix diff(Matrix const& rhs);
 Matrix transpose();
};

You also need to define a couple of standard functions (or disable some standard functions).

class Matrix
{
 public:
 // Create a matrix (of specified width/height with all data set to zero
 Matrix(int width, int height);
 // Copy a matrix
 Matrix(Matrix const& copy);
 // Allow people to set matrixes via assignment
 Matrix operator=(Matrix const& copy);
 // Input and output operations so you can print a matrix
 // And read a matrix from a stream (stdin/stdout)
 friend std::ostream operator<<(std::ostream& str, Matrix const& output);
 friend std::istream operator>>(std::istream& str, Matrix& input);
};

Once you have defined these you should be all set.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /