Exemples langages
227
// définition des méthodes de Point
Point::Point (int x, int y)
{ abs = x ; ord = y ; }
void Point::affiche ()
{ cout << "Je suis un point de coordoonnées : " << abs << " " << ord << "\n" ;
}
Point Point::symetrique ()
{ Point res (-abs, -ord) ;
// point local
return res ;
// renvoyé par valeur
}
bool Point::coincide (Point p)
// coincide reçoit une copie du paramètre
{ return ( (p.abs == abs) && (p.ord == ord) ) ;
}
void Point::permute (Point p)
// permute reçoit une copie du paramètre
{ Point c (0, 0) ; // point local géré par valeur
c = p ;
p = *this ;
// this représente un pointeur sur l’objet courant
// *this la valeur pointée
*this = c ;
}
// fonction principale
main()
{ Point a (1, 2), b (0, 0), c(1, 2), d(-1, -2) ;
if (a.coincide(c)) cout << "a coincide avec c\n" ;
cout << "a : " ; a.affiche () ;
b = a.symetrique() ;
cout << "b : " ; b.affiche () ;
if (b.coincide(d)) cout << "b coincide avec d\n";
a.permute (b) ;
cout << "a : " ; a.affiche () ;
cout << "b : " ; b.affiche () ;
}
a coincide avec c
a : Je suis un point de coordoonnées : 1 2
b : Je suis un point de coordoonnées : -1 -2
b coincide avec d
a : Je suis un point de coordoonnées : -1 -2
b : Je suis un point de coordoonnées : -1 -2
PHP
// programme principal
$a = new Point (1, 2);
$b = new Point (0, 0) ;
$c = new Point (1, 2) ;
$d = new Point (-1, -2) ;
Précédent

- 250/370

Suivant