1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
   | // En-tête //---------------------------------------------------------------------------------------------
#include "Matrice.h"
#include <stdexcept>
 
// ----------------------------------------------------------------------------------- // Constructeurs //
template<class T>
Matrice<T>::Matrice() :
    _nbrLig(0), _nbrCol(0), _nbrElt(0)
{
    _matrice = NULL;
}
 
template<class T>
Matrice<T>::Matrice(unsigned int inNbrLig, unsigned int inNbrCol) :
    _nbrLig(inNbrLig), _nbrCol(inNbrCol), _nbrElt(inNbrLig * inNbrCol)
{
    try {
        _matrice = new T[_nbrElt];
    }
    catch (std::bad_alloc & ba) {
        std::cerr << "bad_alloc caught : " << ba.what() << "\n";
    }
}
 
template<class T>
Matrice<T>::Matrice(const Matrice<T> & inMatrice) {
    _nbrLig = inMatrice.getNbrLig();
    _nbrCol = inMatrice.getNbrCol();
    _nbrElt = _nbrLig * _nbrCol;
 
    try {
        _matrice = new T[_nbrElt];
        for (unsigned int i = 0; i < _nbrElt; ++i) {
            _matrice[i] = inMatrice(i);
        }
    }
    catch (std::bad_alloc & ba) {
        std::cerr << "bad_alloc caught : " << ba.what() << "\n";
    }
}
 
// ------------------------------------------------------------------------------------- // Destructeur //
template<class T>
Matrice<T>::~Matrice() {
    delete[] _matrice;
}
 
// ---------------------------------------------------------------------------------------- // Méthodes //
template<class T>
T Matrice<T>::operator()(unsigned int i, unsigned int j) const {
    try {
        return _matrice[_nbrCol * i + j];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
T & Matrice<T>::operator()(unsigned int i, unsigned int j) {
    try {
        return _matrice[_nbrCol * i + j];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
T Matrice<T>::operator()(unsigned int indice) const {
    try {
        return _matrice[indice];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
T & Matrice<T>::operator()(unsigned int indice) {
    try {
        return _matrice[indice];
    }
    catch (std::out_of_range & oor) {
        std::cerr << "out of range : " << oor.what() << "\n";
        exit(EXIT_FAILURE);
    }
}
 
template<class T>
void Matrice<T>::operator=(const Matrice<T> & inMatrice) {
    _nbrLig = inMatrice.getNbrLig();
    _nbrCol = inMatrice.getNbrCol();
    _nbrElt = _nbrLig * _nbrCol;
 
    delete[] _matrice;
    try {
        _matrice = new T[_nbrElt];
        for (unsigned int i = 0; i < _nbrElt; ++i) {
            _matrice[i] = inMatrice(i);
        }
    }
    catch (std::bad_alloc & ba) {
        std::cerr << "bad_alloc caught : " << ba.what() << "\n";
    }
}
 
// ----------------------------------------------------------------------------------------- // Getters //
template<class T> unsigned int Matrice<T>::getNbrLig() const { return _nbrLig; }
template<class T> unsigned int Matrice<T>::getNbrCol() const { return _nbrCol; }
template<class T> unsigned int Matrice<T>::getNbrElt() const { return _nbrElt; }
 
// Fin //------------------------------------------------------------------------------------------------- | 
Partager