Références ou pointeurs dans un constructeur
Bonjour a tous,
Si je résume mon problème :
j'ai une classe Point et un classe Ligne (composé de 2 instances de Points), si je déplace les coordonnées d'un Point je souhaite que la ligne suive....
j'y arrive très bien en python, mais je découvre en ce moment le c++, et entre les pointeurs et les référence , je suis un peu perdu, d'après mes explorations sur Google, je pense que la solution se trouve au niveau des pointeurs, mais je n'arrive pas a modifier ma ligne après modification d'un des 2 points.
en python :
Code:
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
| class Point:
def __init__(self,x=0.,y=0.):
self.x=x
self.y=y
def dist(self,autre):
d=((autre.x-self.x)**2+(autre.y-self.y)**2)**0.5
return d
class Line:
def __init__(self,P1,P2):
self.A=P1
self.B=P2
def lg(self):
d=self.A.dist(self.B)
return d
if __name__ == '__main__':
# Creation des points
P1=Point(0.,0.)
P2=Point(5.,0.)
# creation d'une ligne
L1=Line(P1,P2)
print "Longeur L1: ",L1.lg()
# modification du point P2
P2.x=100
# verification de la longeur de la ligne
print "Longeur L1: ",L1.lg() |
resultat Python:
Citation:
Longeur L1: 5.0
Longeur L1: 100.0
j'ai conscience que si je part sur les pointeurs je vais avoir des soucis si je détruit un point, que deviennet les lignes...mais je verrais plus tards...
voici mon code c++ qui pose probleme.
Point.h :
Code:
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
|
#ifndef DEF_POINT
#define DEF_POINT
#include <math.h>
#include <iostream>
// using namespace std; a ne JAMAIS activer, risque de pb pour la suite, mais ajouter std::string a la palce
class Point
{
// attributs
private:
int id;
float x;
float y;
float z;
public:
// Constructeur
Point();
Point(float x, float y, float z);
// methodes
void move(float a = 0.0, float b = 0.0, float c = 0.0);
void print() const;
float dist(Point other);
// Destructeur
~Point();
};
#endif |
Point.cpp :
Code:
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
|
#include "Point.h"
using namespace std;
// attributs
int id;
float x;
float y;
float z;
// Constructeur
Point::Point(): id (1),x (0.),y(0.),z(0.)
{
// Rien a mettre ici
}
Point::Point(float x, float y, float z): id(1), x(x), y(y), z(z){}
// methodes
void Point::move(float a , float b , float c )
{
x = a;
y = b;
z = c;
}
void Point::print() const
{
cout << "Point: " << id << " ( " << x << ", " << y << ", " << z << " )"<< endl;
}
float Point::dist(Point other)
{
float d = sqrt(pow(other.x - x,2) + pow(other.y - y,2) + pow(other.z - z,2));
return d;
}
// Destructeur
Point::~Point()
{
// Rien a mettre ici, car pas d allocation dynamique fait avec new...
} |
Line.h :
Code:
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
|
#ifndef DEF_LINE
#define DEF_LINE
#include <iostream>
#include "Point.h"
class Line
{
// attributs
private:
int id2;
Point ptA;
Point ptB;
public:
// Constructeur
Line(Point &P1, Point &P2);
// Methodes
double longeur() ;
void print() const;
// Destructeur
~Line();
};
#endif |
Line.cpp :
Code:
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
|
#include "Line.h"
//#include "Point.h"
using namespace std;
// attributs
int id2;
Point *ptA;
Point *ptB;
// Constructeur
Line::Line(Point &P1,Point &P2):id2(1),ptA(P1),ptB(P2)
{
this->ptA = P1;
this->ptB = P2;
}
// Methodes
double Line::longeur()
{
float d = ptA.dist(ptB);
cout << "LineLg: " << d << endl;
return d;
}
void Line::print() const
{
cout << "Line: " << id2 << " ( ";
}
// Destructeur
Line::~Line()
{
} |
en enfin mon main
Code:
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
|
#include <iostream>
#include <string>
#include <math.h>
#include "Point.h"
#include "Line.h"
using namespace std;
int main()
{
Point P1, P2;
Point P3(5., 6., 6.);
P1.print();
cout << "adresse de P1:" << &P1<<endl;
P2.print();
cout << "adresse de P2:" << &P2 << endl;
P3.print();
cout << "adresse de P3:" << &P3 << endl;
float d = P1.dist(P3);
cout <<"distance P1-P3: "<< d << endl;
Line L1(P1, P3);
cout <<"lg L1:" << L1.longeur()<< endl;
cout << "deplacement de P3:" << endl;
P3.move(0., 0., 100.);
P3.print();
cout << L1.longeur();
system("PAUSE");
return 0;
} |
Par avance merci pour votre aide.