Point.h #ifndef _Point_h_ #define _Point_h_ class Point { private

Transcription

Point.h #ifndef _Point_h_ #define _Point_h_ class Point { private
Point.h
#ifndef _Point_h_
#define _Point_h_
class Point
{
private:
float X;
float Y;
public:
Point(float x = 0, float y = 0);
Point Add(Point pt) const;
void Afficher() const;
float GetX() const;
float GetY() const;
void SetX(float x);
void SetY(float y);
float Distance(Point P) const;
};
//surcharge opérateeur + pour Point
Point operator+(Point const& p1,Point const& p2);
#endif
Point.cpp
#include <iostream >
#include <math.h>
#include "Point.h"
using namespace std ;
Point::Point(float x, float y)
{
X=x;
Y=y;
}
Point Point::Add(Point pt) const
{
Point result(pt.X + X, pt.Y + Y);
return result;
}
void Point::Afficher() const
{
cout<< "(" << X << " ," << Y << " )"<<endl;
}
float Point::GetX() const
{
return X; }
float Point::GetY() const
{
return Y; }
void Point::SetX(float x)
{
X = x; }
void Point::SetY(float y)
{
Y = y; }
float Point::Distance(Point P) const
{
return sqrt(pow(X-P.X,2)+pow(Y-P.Y,2));
}
//surcharge opérateeur + pour Point
Point operator+(Point const& p1,Point const& p2)
{
Point p(p1.Add(p2));
return p;
}
Carre.h
#ifndef Carre_h
#define Carre_h
#include "Point.h"
#include<iostream>
class Carre
{
private:
Point Som_Sup_Drt;
Point Som_Inf_Gch;
public:
//constructeur par défaut
Carre();
//constructeur pour initialiser les sommets
Carre(float x1 , float y1, float x2 , float y2);
//constructeur en utilisant constructeur de copie
Carre(Point p1,Point p2);
//accesseurs en lecture
void Get_Som_Sup_Drt(float &x1, float &y1) const;
void Get_Som_Inf_Gch(float &x1, float &y1) const;
//accesseurs en ecriture
void Set_Som_Sup_Drt(float x1, float y1);
void Set_Som_Inf_Gch(float x1, float y1);
//affichage du carré
void Affichage() const;
//périmètre du carré
float primetre() const;
//translation suivant un point
Carre translation(Point p);
//aire du carré
float aire() const;
};
#endif
Carre.cpp
#include <iostream >
#include <math.h>
#include <string.h>
#include "Point.h"
#include "Carre.h"
using namespace std;
//constructeur par défaut
Carre::Carre():Som_Sup_Drt(0,0),Som_Inf_Gch(1,1)
{
//ou bien
//Som_Sup_Drt(0,0);
//Som_Inf_Gch(1,1);
}
//constructeur pour initialiser les sommets
Carre::Carre(float x1 , float y1, float x2, float y2):
Som_Sup_Drt(x1,y1),Som_Inf_Gch(x2,y2)
{
}
//constructeur en utilisant deux points
Carre::Carre(Point p1,Point p2): Som_Sup_Drt(p1),Som_Inf_Gch(p2)
{
}
//accesseurs en lecture
void Carre::Get_Som_Sup_Drt(float &x1, float &y1) const
{
x1=Som_Sup_Drt.GetX();
y1=Som_Sup_Drt.GetY();
}
void Carre::Get_Som_Inf_Gch(float &x1, float &y1) const
{
x1=Som_Inf_Gch.GetX();
y1=Som_Inf_Gch.GetY();
}
//accesseurs en ecriture
void Carre::Set_Som_Inf_Gch(float x1, float y1)
{
Som_Inf_Gch.SetX(x1);
Som_Inf_Gch.SetY(y1);
}
void Carre::Set_Som_Sup_Drt(float x1, float y1)
{
Som_Sup_Drt.SetX(x1);
Som_Sup_Drt.SetY(y1);
}
//affichage du carré
void Carre::Affichage() const
{
cout<<"*****sommet superieur droit*****"<<endl;
Som_Sup_Drt.Afficher();
cout<<"*****sommet inferieur gauche*****"<<endl;
Som_Inf_Gch.Afficher();
}
//périmètre du carré
float Carre::primetre() const
{
Point p;
float distX,distY;
p.SetX(Som_Sup_Drt.GetX());
p.SetY(Som_Inf_Gch.GetY());
distX=Som_Inf_Gch.Distance(p);
p.SetX(Som_Inf_Gch.GetX());
p.SetY(Som_Sup_Drt.GetY());
distY=Som_Inf_Gch.Distance(p);
return
2*(distX+distY);
}
//translation suivant un point
Carre Carre::translation(Point p)
{
Carre c;
Point p1(Som_Inf_Gch.Add(p)),p2(Som_Sup_Drt.Add(p));
//p1(Som_Inf_Gch.Add(p));
c.Set_Som_Inf_Gch(p1.GetX(),p1.GetY());
c.Set_Som_Sup_Drt(p2.GetX(),p2.GetY());
return c;
}
//aire du carré
float Carre::aire() const
{
Point p;
float distX,distY;
p.SetX(Som_Sup_Drt.GetX());
p.SetY(Som_Inf_Gch.GetY());
distX=Som_Inf_Gch.Distance(p);
p.SetX(Som_Inf_Gch.GetX());
p.SetY(Som_Sup_Drt.GetY());
distY=Som_Inf_Gch.Distance(p);
return distX*distY;
}
Cercle.h
#ifndef Cercle_h
#define Cercle_h
#include "Point.h"
#include<iostream>
class Cercle
{
private:
Point Centre;
float rayon;
public:
//constructeur par défaut
Cercle();
//constructeur pour initialiser les sommets
Cercle(float x1 , float y1, float r);
//constructeur en utilisant un point et un
Cercle(Point p1,float r);
rayon
//accesseurs en lecture
void Get_Centre(float &x1, float &y1) const;
float Get_Rayon() const;
//accesseurs en ecriture
void Set_Centre(float x1, float y1);
void Set_Rayon(float r);
//affichage du carré
void Affichage() const;
};
//surcharge opérateur + pour Cercle
Cercle operator+(Cercle const& c1,Cercle const& c2);
#endif
Cercle.cpp
#include <iostream >
#include
#include
#include
#include
<math.h>
<string.h>
"Point.h"
"Cercle.h"
using namespace std;
//constructeur par défaut
Cercle::Cercle():Centre(0,0),rayon(1)
{
}
//constructeur pour initialiser les sommets
Cercle::Cercle(float x1 , float y1, float r):Centre(x1,y1),rayon(r)
{
}
//constructeur en utilisant un point et un rayon
Cercle::Cercle(Point p1,float r):Centre(p1),rayon(r)
{
}
//accesseurs en lecture
void Cercle::Get_Centre(float &x1, float &y1) const
{
x1=Centre.GetX();
y1=Centre.GetY();
}
float Cercle::Get_Rayon() const
{
return rayon;
}
//accesseurs en ecriture
void Cercle::Set_Centre(float x1, float y1)
{
Centre.SetX(x1);
Centre.SetY(y1);
}
void Cercle::Set_Rayon(float r)
{
rayon=r;
}
//affichage du carré
void Cercle::Affichage() const
{
cout<<"******Affichage du Cercle*****"<<endl;
Centre.Afficher();
cout<<"rayon ="<<rayon<<endl;
}
//surcharge opérateur + pour Cercle
Cercle operator+(Cercle const& c1,Cercle const& c2)
{
Cercle c;
float x1,x2,y1,y2;
c1.Get_Centre(x1,y1);
c2.Get_Centre(x2,y2);
c.Set_Centre(x1+x2,y1+y2);
c.Set_Rayon(c1.Get_Rayon()+c2.Get_Rayon());
return c;
}
Main.cpp
#include
#include
#include
#include
<iostream >
"Point.h"
"Cercle.h"
"Carre.h"
using namespace std ;
int main( void )
{
float x1,y1,x2,y2;
cout << "***Saisie d'un carre****" << endl;
cout<<"donner les coords d'un carre"<<endl;
cin>>x1;
cin>>y1;
cin>>x2;
cin>>y2;
Carre car(x1,y1,x2,y2);
cout << "***Saisie d'un' cercle***" << endl;
cout<<"donner les coords d'un cercle"<<endl;
cin>>x1;
cin>>y1;
cin>>x2;
Cercle cer1(x1,y1,x2);
cout << "***Saisie d'un autre cercle***" << endl;
cout<<"donner les coords d'un cercle"<<endl;
cin>>x1;
cin>>y1;
cin>>x2;
Cercle cer2(x1,y1,x2);
cout<<"***Affichage des INFOS***"<<endl;
car.Affichage();
cout<<"le périmetre du carre est ="<<car.primetre()<<endl;
cout<<"l'aire du carre est ="<<car.aire()<<endl;
cer1.Affichage();
cer2.Affichage();
cout<<"***Translation du carre***"<<endl;
Point pt(1,1);
Carre c(car.translation(pt));
c.Affichage();
cout<<"***Somme des deux cercles***"<<endl;
Cercle cer(cer1+cer2);
cer.Affichage();
cout<<"***saisie des 3 points***"<<endl;
cer1.Get_Centre(x1,y1);
Point p[3],p2(x1,y1);
for(int i=0;i<3;i++)
{
cout<<"donner coords point "<<i<<endl;
cin>>x1;
cin>>y1;
p[i].SetX(x1);
p[i].SetY(y1);
if (p[i].Distance(p2)<cer1.Get_Rayon())
cout<<"Ce point appartient au cercle"<<endl;
else
cout<<"Ce point n'appartient pas au cercle"<<endl;
}
cin.get();
return 0;
}

Documents pareils