Propriétés des objets et des méthodes
CHAPITRE 10
226
class Point
{ public Point (int x, int y)
{ abs = x ; ord = y ; }
public void affiche ()
{ System.out.println ("Je suis un point de coordoonnées : " + abs + " " + ord ) ;
}
public Point symetrique ()
{ Point res = new Point (-abs, -ord) ;
return res ;
}
public boolean coincide (Point p) // coincide reçoit une copie du paramètre
{ return ( (p.abs == abs) && (p.ord == ord) ) ;
}
public void permute (Point p)
{ Point c = new Point (0, 0) ;
c.abs = p.abs ;
c.ord = p.ord ;
p.abs = this.abs ; p.ord = this.ord ;
this.abs = c.abs ; this.ord = c.ord ;
}
private int abs, ord ;
}
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
C++
#include
using namespace std ;
// déclaration classe Point (en général, dans un fichier séparé)
class Point
{ public :
Point (int, int) ;
void affiche () ;
Point symetrique () ;
bool coincide (Point) ;
void permute (Point) ;
private :
int abs, ord ;
} ;
// attention au point-virgule ici
CHAPITRE 10
226
class Point
{ public Point (int x, int y)
{ abs = x ; ord = y ; }
public void affiche ()
{ System.out.println ("Je suis un point de coordoonnées : " + abs + " " + ord ) ;
}
public Point symetrique ()
{ Point res = new Point (-abs, -ord) ;
return res ;
}
public boolean coincide (Point p) // coincide reçoit une copie du paramètre
{ return ( (p.abs == abs) && (p.ord == ord) ) ;
}
public void permute (Point p)
{ Point c = new Point (0, 0) ;
c.abs = p.abs ;
c.ord = p.ord ;
p.abs = this.abs ; p.ord = this.ord ;
this.abs = c.abs ; this.ord = c.ord ;
}
private int abs, ord ;
}
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
C++
#include
using namespace std ;
// déclaration classe Point (en général, dans un fichier séparé)
class Point
{ public :
Point (int, int) ;
void affiche () ;
Point symetrique () ;
bool coincide (Point) ;
void permute (Point) ;
private :
int abs, ord ;
} ;
// attention au point-virgule ici
