Thread: Do you have any recommendation regarding the consistency of UML diagram and code?

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    18

    Do you have any recommendation regarding the consistency of UML diagram and code?

    Hello Everybody,
    I have translated the following class diagram (click on the link below) into c++ code. This is my early stage experience indeed for such an implementation from UML class diagram. I am curious to share my improvement. If you have any advice for further improvement, please feel free to drop a line. Do you have any recommendation regarding the consistency of UML diagram and code?
    Code:
    
    
    Code:
    ---------------------------DataSource.h---------------------------------
    #ifndef DATASOURCE_H
    #define DATASOURCE_H
    class DataSource
    {
    public:
        double** dataElement = nullptr;
        int nRow, nColumn;
        DataSource();
        ~DataSource();
    
    
    protected:
    private:
    };
    #endif
    --------------------------DataSource.cpp--------------------------------
    #include "DataSource.h"
    DataSource :: DataSource()
    {
        nRow    = 4;
        nColumn = 4;
    
    
        double dataFromFile[4][4] = { { 10.0, -1.0, 2.0, 0.0 },
                                      { -1.0, 11.0, -1.0, 3.0 },
                                      { 2.0, -1.0, 10.0, -1.0 },
                                      { 0.0, 3.0, -1.0, 8.0 } };
    
    
        allocateData2DbyMalloc(dataElement, nRow, nColumn);
        for (int row = 0; row < nRow; row++)
        {
            for (int col = 0; col < nColumn; col++)
            {
                dataElement[row][col] = dataFromFile[row][col];
            }
        }
    }
    
    
    DataSource :: ~DataSource()
    {
        deAllocateData2DbyFree(dataElement, nRow);
        dataElement = nullptr;
    }
    --------------------------Matrix.h---------------------------------
    #ifndef MATRIX_H
    #define MATRIX_H
    class Matrix
    {
    public:
        int numberOfRow, numberOfColumn;
        double** matrixElement = nullptr;
        Matrix();
        virtual ~Matrix();
        void filterDataElement();
    };
    #endif
    
    
    --------------------------Matrix.cpp---------------------------------
    #include "DataSource.h"
    #include "Matrix.h"
    #include <iostream>
    Matrix :: Matrix()
    {
        DataSource  *dataObj  = new DataSource;
        numberOfRow    = dataObj->nRow;
        numberOfColumn = dataObj->nColumn;
        allocateData2DbyMalloc(this->matrixElement, numberOfRow, numberOfColumn);
    //..Copy the data from Data Source to Matrix array. Filtarazation of data might necessary before copying. 
        for (int row = 0; row < numberOfRow; row++)
        {
            for (int col = 0; col < numberOfColumn; col++)
            {
                this->matrixElement[row][col] = dataObj->dataElement[row][col]; 
            }
        }
        delete dataObj;
        dataObj = nullptr;
    }
    ----------------------MatrixAlgorithm.h-----------------------------
    Matrix :: ~Matrix()
    {
        deAllocateData2DbyFree(matrixElement, numberOfRow);
        matrixElement = nullptr;
    }
    
    
    #ifndef MATRIXALGORITHM_H
    #define MATRIXALGORITHM_H
    class MatrixAlgorithm
    {
    public:
        MatrixAlgorithm();
        ~MatrixAlgorithm();
        bool checkDimOfMatrixAndConstrain();
    };
    #endif
    ----------------------MatrixAlgorithm.cpp-----------------------------
    #include "Matrix.h"
    #include "MatrixAlgorithm.h"
    #include <iostream>
    MatrixAlgorithm :: MatrixAlgorithm()
    {
        Matrix* matObj = new Matrix;
        ItarativeAlgorithm* itarativeAlgorithmObj = new GaussSeidel(matObj->numberOfRow, matObj->numberOfColumn);   
        itarativeAlgorithmObj->matrixA = matObj->matrixElement;
        itarativeAlgorithmObj->vectorB = userInputObj->constrainVector;
        itarativeAlgorithmObj->vectorX = decisionObj->decisionVector;
        itarativeAlgorithmObj->algorithm();
        delete itarativeAlgorithmObj;
        itarativeAlgorithmObj = nullptr;
        delete matObj;
        matObj = nullptr;
    }
    
    
    MatrixAlgorithm :: ~MatrixAlgorithm()
    {
    }
    ---------------------ItarativeAlgorithm.h---------------------------
    #ifndef ITARATIVE_H
    #define ITARATIVE_H
    class ItarativeAlgorithm
    {
    public:
        double** matrixA;
    
    
        double* vectorB;
        double* vectorX;
        double* vectorXnext;
    
    
        int numberOfRow;
        int numberOfColumn;
        int maxItarationNnumber;
    
    
        bool converge;
        double relativeError;
        double maxTolerance;
        double minTolerance;
    
    
        ItarativeAlgorithm();
        virtual ~ItarativeAlgorithm();
        void itarativePrint();
        virtual void algorithm();
    };
    #endif
    ---------------------ItarativeAlgorithm.cpp---------------------------
    #include "ItarativeAlgorithm.h"
    #include <iostream>
    ItarativeAlgorithm::ItarativeAlgorithm()
    {
        matrixA = nullptr;
        vectorB = nullptr;
        vectorX = nullptr;
        vectorXnext = nullptr;
        converge       = false;
        accuracyTolerance   = 0.0;
        maxItarationNnumber = 10;
    }
    
    
    ItarativeAlgorithm::~ItarativeAlgorithm()
    {
    }
    
    
    void ItarativeAlgorithm::algorithm()
    {
    
    
    }
    ---------------------GaussSeidel.h------------------------------------
    #ifndef GAUSSSEIDEL_H
    #define GAUSSSEIDEL_H
    #include "ItarativeAlgorithm.h"
    class GaussSeidel : public ItarativeAlgorithm
    {
    public:
        GaussSeidel(int, int);
        ~GaussSeidel();
        void algorithm();
    };
    
    
    #endif // !JACOBI_H
    ---------------------GaussSeidel.cpp------------------------------------
    #include "GaussSeidel.h"
    #include <iostream>
    GaussSeidel :: GaussSeidel(int nRow, int nCol) // The reason to define the derive constructor is to observe the deallocation of matrix properly//
    {
        this->numberOfRow = nRow;
        this->numberOfColumn = nCol;
    }
    
    
    GaussSeidel :: ~GaussSeidel()
    {
    }
    
    
    void GaussSeidel :: algorithm()
    {
        int itarationNumber = 0;
        double          sum = 0.0;
        while (itarationNumber < this->maxItarationNnumber)
        {
            std::cout << "Step == " << itarationNumber << std::endl;
                for (int i = 0; i < this->numberOfRow; i++)
                {
                    for (int j = 0; j < this->numberOfColumn; j++)
                    {
                        if (i != j)
                            sum = sum + matrixA[i][j] * vectorX[j] * (-1);
                    }
                    vectorX[i] = (1.0 / matrixA[i][i])*(sum + vectorB[i]); // The only line changed from Jacobi Algorithm
                    sum = 0;
                }
            for (int i = 0; i < this->numberOfRow; i++)
            {
                std::cout << vectorX[i] << std::endl;
            }
            itarationNumber = itarationNumber + 1;
        }
        vectorX = nullptr;
    }



  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,456
    Double post. Please don't post the same question in multiple sub-forums.

    Also this is a C++ question not a C question.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    18
    Sorry for that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UML class diagram to code translation
    By Shafiul in forum C++ Programming
    Replies: 2
    Last Post: 3 Weeks Ago, 10:56 PM
  2. C Code for a flow diagram
    By unity006 in forum C Programming
    Replies: 4
    Last Post: 11-08-2015, 04:24 PM
  3. Data type consistency
    By audinue in forum C Programming
    Replies: 14
    Last Post: 07-03-2008, 08:33 AM
  4. Consistency of MOUSEEVENT
    By Mr Silvertongue in forum Windows Programming
    Replies: 2
    Last Post: 04-02-2004, 05:45 AM
  5. Error consistency checking
    By Tibo in forum C Programming
    Replies: 1
    Last Post: 03-27-2003, 06:40 PM

Tags for this Thread