Advertisement
informaticage

MatrixForPhysicsClass

Sep 23rd, 2017
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. // class DoubleMatrix: Defining a simple Matrix class
  4. class DoubleMatrix {
  5.     // Hidden
  6.     private:
  7.         double ** doubleMatrix;
  8.         int dimensionWidth;
  9.         int dimensionHeight;
  10.  
  11.     // Visible
  12.     public:
  13.         DoubleMatrix(int dimensionWidth, int dimensionHeight) {
  14.             // Dimensions assignment
  15.             this->dimensionWidth = dimensionWidth;
  16.             this->dimensionHeight = dimensionHeight;
  17.  
  18.             // Matrix initialization
  19.             this->doubleMatrix = new double*[dimensionWidth];
  20.  
  21.             // For each array contained in the first array
  22.             for (int heightInitializer = 0; heightInitializer < dimensionWidth; heightInitializer++) {
  23.                 doubleMatrix[heightInitializer] = new double[dimensionHeight];
  24.             }
  25.         }
  26.  
  27.         // Function getMatrix: Returns the matrix as a pointer to pointer object
  28.         double** getMatrix() {
  29.             return this->doubleMatrix;
  30.         }
  31.  
  32.         // Function getMatrix @overload(int, int): Returns a specified element for the intended matrix
  33.         double getMatrix(int dimensionWidth, int dimensionHeight) {
  34.             return this->doubleMatrix[dimensionWidth][dimensionHeight];
  35.         }
  36.  
  37.         // Function setMatrix: Defines the matrix to be the specified matrix
  38.         void setMatrix(double** matrix) {
  39.             this->doubleMatrix = matrix;
  40.         }
  41.  
  42.         // Function setMatrix @overload(int, int, double): Defines a particular element in the matrix to be the specified value
  43.         void setMatrix(int dimensionWidth, int dimensionHeight, double value) {
  44.             this->doubleMatrix[dimensionWidth][dimensionHeight] = value;
  45.         }
  46. };
  47.  
  48. int main()
  49. {
  50.     using namespace std;
  51.  
  52.     DoubleMatrix* dm = new DoubleMatrix(3, 2);
  53.     dm->setMatrix(0, 0, 1.618);
  54.  
  55.  
  56.     cout << dm->getMatrix(0, 0) << endl;
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement