Thread: UML class diagram to code translation

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

    UML class diagram to code translation

    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.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    * moved to C++ programming forum *
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It started over there -> UML class diagram to code translation - C++ Forum
    No idea if any progress has been made.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Code for a flow diagram
    By unity006 in forum C Programming
    Replies: 4
    Last Post: 11-08-2015, 04:24 PM
  2. UML Diagram for single class program
    By JonathanS in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2012, 09:36 PM
  3. Source code translation
    By audinue in forum General Discussions
    Replies: 25
    Last Post: 09-09-2009, 09:16 PM
  4. translation code
    By yusnita in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2009, 12:56 AM
  5. Code Translation
    By Linette in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2002, 01:48 PM

Tags for this Thread