Surcharges des operators

leroiloup Messages postés 4 Date d'inscription vendredi 14 mars 2008 Statut Membre Dernière intervention 19 avril 2009 - 23 déc. 2008 à 15:34
lglandeur Messages postés 28 Date d'inscription samedi 11 juin 2005 Statut Membre Dernière intervention 20 janvier 2009 - 29 déc. 2008 à 01:01
salut a tout j'ais un probleme d'implementer les deux operator (operator+, operator*) et comment l'etuliser dans main()

#include

#include<conio.h>

using namespace std;

class Matrice

{

int li,co;

int **mat;

public:

Matrice(const int&,const int&,const int&);

~Matrice(){};

int Getli(){return li;}

int Getco(){return co;}

void Setel(const int i,const int j ,const int el)

{

mat[i][j]=el;

}

int Getel(int i,int j )const{ return mat[i][j]; }

void LireMat();

void AffMat();

Matrice operator +(const Matrice&);

Matrice operator *(const Matrice&);

};

Matrice::Matrice(const int &n=3,const int &m=3,const int &init=0)

{

li=n; co=m;

mat = new int *[n];

int i,j;

for(i=0;i<li;i++)

mat[i]=new int[co];

for(i=0;i<li;i++)

for(j=0;j<co;j++)

mat[i][j]=init;

}

void Matrice::AffMat()

{

int i,j;

for(i=0;i<li;i++)

for(j=0;j<co;j++)

cout<<"Mat["<>el;

mat[i][j]=el;

}

}

int main()

{

Matrice A;

A.LireMat();

A.AffMat();

getch();

return 0;

}

1 réponse

lglandeur Messages postés 28 Date d'inscription samedi 11 juin 2005 Statut Membre Dernière intervention 20 janvier 2009
29 déc. 2008 à 01:01
Bonjour, je te conseille de surchargé d'abord l'opérateur =, aussi en plus des opérateur +, et * car comme cela tu pourras faire des opération basique avec tes matrices du style A = B + C.

Ensuite pour la surcharge des opérateurs +, et * personnellement j'utiliserais des fonctions amie.

<hr size="2" width="100%" />class Matrice
{

    ...

    friend Matrice operator+(const Matrice &, const Matrice &);

    friend Matrice operator*(const Matrice &, const Matrice &);
}

Matrice operator+(const Matrice &M1, const Matrice &M2)
{



    Matrice M(M1.Getli(), M1.Getco());




    if((M1.Getli() M2.Getli()) && (M1.Getco() M2.Getco()))




    {




   



    // ton code pour l'addition des matrices a enregistrer dans la matrice M




    }




    return M;
}

// la surchage de l'operateur * se fait sur le même principe.
<hr size="2" width="100%" />
j'espère avoir était assez clair.
0
Rejoignez-nous