Advertisement
salahgo

Notions de classe, objet et encapsulation

Oct 10th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std ;
  4.  
  5. class Point {
  6.     private:
  7.     float abs;
  8.     float ord;
  9.     char pnt;
  10.     public:
  11.     void initialise(char, float,float);
  12.     void deplace(float dx,float dy);
  13.     void affiche();
  14.     float GetAbs() {return abs;};
  15.     float GetOrd();
  16.     char GetPnt();
  17.     };
  18.  
  19. void Point::initialise(char a,float x,float y)
  20.  {
  21.      pnt= a;
  22.      abs= x;
  23.      ord= y;
  24.  }
  25.  
  26.  void Point::deplace(float dx, float dy)
  27.  {
  28.      abs=abs+dx;
  29.      ord=ord+dy;
  30.  }
  31.  
  32.  void Point::affiche()
  33.  {
  34.      cout<<"le point "<<pnt<<" a pour coordonnées:("<<abs<<","<<ord<<")"<<endl;
  35.  }
  36.  
  37. inline float Point::GetOrd() {
  38.     return ord;
  39.     }
  40. char Point::GetPnt(){
  41.     return pnt;
  42.     }
  43.  
  44.  float distant(Point t1, Point t2)
  45.  {
  46.      float d,x1,x2,y1,y2;
  47.      x1=t1.GetAbs();
  48.      x2=t2.GetAbs();
  49.      y1=t1.GetAbs();
  50.      y2=t2.GetAbs();
  51.      d = sqrt(pow((x1-x2),2)+pow((y1-y2),2));
  52.      return d;
  53.  }
  54.  int main ()
  55.  {
  56.      Point p1, p2;
  57.      p1.initialise('p',1,2);
  58.      p2.initialise('r',2,4);
  59.      p1.affiche();
  60.      p2.affiche();
  61.      cout<<"distance entre "<<p1.GetPnt()<<" et "<<p1.GetPnt()<<" est "<<distant(p1,p2)<<endl;
  62.      p1.deplace(3,4);
  63.      p2.deplace(0,5);
  64.      p1.affiche();
  65.      p2.affiche();
  66.      return 0;
  67.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement