Constructeurs et operateur + heritage simple

Contenu du snippet

Exemple de programmation avec des constructeurs,destructeur
Utilisation de quelques type opérateurs + exemple d'heritage simple
avec une classe coloré pour les opérateurs et constructeurs.
deux classe développé : Parent : Cl_point et Fille : Cl_PointColore
+ Programme test (compilé avec Borland C++ 5.5

Source / Exemple :


#include "Mylib.h"

//--------------------------------------------------------
//                      DEBUT CLASSE Cl_Point
//--------------------------------------------------------
  class Cl_Point{
    //accessible par les membres de la classe uniquement 
    private:
      static int NbrPoints;
    //accessible par les membres de la classe et par les classes dérivées
    protected:
      char nom;
      int x;
      int y;
    //accessible par tout le monde (membres de la classe, 
    //classes dérivées et clients de la classe)
    public:
      Cl_Point();
      Cl_Point(const char,const int,const int);
      Cl_Point(const Cl_Point &);
      ~Cl_Point();
      void affiche(void)const;
      void nbrPoints(void)const;
      friend ostream & operator << (ostream &,const Cl_Point &);
      Cl_Point & operator + (const Cl_Point &);
      Cl_Point & operator - (const Cl_Point &);
      Cl_Point & operator = (const Cl_Point &);
      Cl_Point & operator = (const Cl_Point *);
      Cl_Point & operator += (const Cl_Point &);
      Cl_Point & operator -= (const Cl_Point &);
      Cl_Point & operator ++ (int);
      Cl_Point & operator -- (int);
      Cl_Point & operator ()(const int, const int);
  };//end class
//--------------------------------------------------------
//                      FIN ClASSE Cl_Point
//--------------------------------------------------------
  int Cl_Point::NbrPoints = 0;
//------------------------------------------------------------------------------
// FONCTION      : Constructeur
// DESCRIPTION   : Constructeur par défaut
//-------------------------------------------------------------------------------
  Cl_Point::Cl_Point(){
    nom = 'A';
    x = 0;
    y = 0;
    NbrPoints++;
    printf("constructeur %c (par défaut)         |Cl_Point|      : %p\n",nom, this);
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : Constructeur
// DESCRIPTION   : Constructeur par initilisation
//-------------------------------------------------------------------------------
  Cl_Point::Cl_Point(const char _nom,const int _x,const int _y){
    nom = _nom;
    x = _x;
    y = _y;
    NbrPoints++;
    printf("constructeur %c (par initilisation)  |Cl_Point|      : %p\n",nom,this);
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : Constructeur
// DESCRIPTION   : Constructeur par recopie
//-------------------------------------------------------------------------------
  Cl_Point::Cl_Point(const Cl_Point & _a){
    nom = _a.nom;
    x = _a.x;
    y = _a.y;
    NbrPoints++;
    printf("constructeur %c (par recopie)        |Cl_Point|      : %p\n",nom,this);
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : Destructeur
// DESCRIPTION   : Destructeur
//-------------------------------------------------------------------------------
  Cl_Point::~Cl_Point(){
    NbrPoints--;
    printf("Destructeur %c                       |Cl_Point|      : %p\n",nom,this);
  }//end procedure
//------------------------------------------------------------------------------
// FONCTION      : affiche
// DESCRIPTION   : Affichage des coordonnées du point à l'écran
//-------------------------------------------------------------------------------
  void Cl_Point::affiche(void)const{
    printf("%c=[%d,%d]\n",nom,x,y);
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : nbrPoints
// DESCRIPTION   : Affichage le nombre de points créé
//-------------------------------------------------------------------------------
  void Cl_Point::nbrPoints(void)const{
    cout << "Nombre de Point(s) créé(s) = " << NbrPoints << endl;
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : operator <<
// DESCRIPTION   : << Cl_Point
//-------------------------------------------------------------------------------
  ostream & operator << (ostream & sortie,const Cl_Point & _P1){
    sortie << _P1.nom << "=[" << _P1.x << "," << _P1.y << "]";
    return sortie;
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : operator =
// DESCRIPTION   : Cl_Point = Cl_Point = Cl_Point...
//------------------------------------------------------------------------------
   Cl_Point & Cl_Point::operator = (const Cl_Point & _rPx){
       Cl_Point tampon(_rPx);
       nom = tampon.nom;
       x = tampon.x;
       y = tampon.y;
       return *this; 
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : operator +
// DESCRIPTION   : Cl_Point + Cl_Point + Cl_Point...
//------------------------------------------------------------------------------
   Cl_Point & Cl_Point::operator + (const Cl_Point & _rPx){
       Cl_Point tampon(_rPx);
       x += tampon.x;
       y += tampon.y;
       return *this; 
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : operator -
// DESCRIPTION   : Cl_Point - Cl_Point - Cl_Point...
//------------------------------------------------------------------------------
   Cl_Point & Cl_Point::operator - (const Cl_Point & _rPx){
       Cl_Point tampon(_rPx);
       x -= tampon.x;
       y -= tampon.y;
       return *this; 
  }//end procedure

//------------------------------------------------------------------------------
// FONCTION      : operator =
// DESCRIPTION   : Cl_Point = Cl_Point*
//------------------------------------------------------------------------------
   Cl_Point & Cl_Point::operator = (const Cl_Point * _Px){

  • this=*_Px;
return *this; }//end procedure //------------------------------------------------------------------------------ // FONCTION : operator += // DESCRIPTION : Cl_Point += Cl_Point //------------------------------------------------------------------------------- Cl_Point & Cl_Point::operator += (const Cl_Point & _rPx){
  • this = *this + _rPx;
return *this; }//end procedure //------------------------------------------------------------------------------ // FONCTION : operator -= // DESCRIPTION : Cl_Point -= Cl_Point //------------------------------------------------------------------------------- Cl_Point & Cl_Point::operator -= (const Cl_Point & _rPx){
  • this = *this - _rPx;
return *this; }//end procedure //------------------------------------------------------------------------------ // FONCTION : operator ++ // DESCRIPTION : Cl_Point++ //------------------------------------------------------------------------------- Cl_Point & Cl_Point::operator ++(int){ x++; y++; return *this; }//end procedure //------------------------------------------------------------------------------ // FONCTION : operator -- // DESCRIPTION : Cl_Point-- //------------------------------------------------------------------------------- Cl_Point & Cl_Point::operator --(int){ x--; y--; return *this; }//end procedure //------------------------------------------------------------------------------ // FONCTION : operator () // DESCRIPTION : Cl_Point() //------------------------------------------------------------------------------- Cl_Point & Cl_Point::operator ()(const int _x, const int _y){ x = _x; y = _y; return *this; }//end procedure //-------------------------------------------------------- // DEBUT CLASSE Cl_PointColore //-------------------------------------------------------- class Cl_PointColore:public Cl_Point{ private: char Color; public: Cl_PointColore(); Cl_PointColore(const char,const int,const int,const char); Cl_PointColore(const Cl_PointColore &); ~Cl_PointColore(); void affiche_pointColore(void)const; friend ostream & operator << (ostream &,const Cl_PointColore &); };//end class //-------------------------------------------------------- // FIN CLASSE Cl_PointColore //-------------------------------------------------------- //------------------------------------------------------------------------------ // FONCTION : Constructeur // DESCRIPTION : Constructeur par défaut //------------------------------------------------------------------------------- Cl_PointColore::Cl_PointColore():Cl_Point(){ Color = 'N'; printf("constructeur %c (par défaut) |Cl_PointColore|: %p\n",Cl_Point::nom, this); }//end procedure //------------------------------------------------------------------------------ // FONCTION : Constructeur // DESCRIPTION : Constructeur par initilisation //------------------------------------------------------------------------------- Cl_PointColore::Cl_PointColore(const char _nom,const int _x,const int _y, const char _Color='N'):Cl_Point(_nom,_x,_y){ Color = _Color; printf("constructeur %c (par initialisation) |Cl_PointColore|: %p\n",Cl_Point::nom, this); }//end procedure //------------------------------------------------------------------------------ // FONCTION : Constructeur // DESCRIPTION : Constructeur de recopie //------------------------------------------------------------------------------- Cl_PointColore::Cl_PointColore(const Cl_PointColore & _S):Cl_Point(_S){ Color = _S.Color; printf("constructeur %c (de recopie) |Cl_PointColore|: %p\n",Cl_Point::nom, this); }//end procedure //------------------------------------------------------------------------------ // FONCTION : Destructeur // DESCRIPTION : Destructeur //------------------------------------------------------------------------------- Cl_PointColore::~Cl_PointColore(){ printf("Destructeur %c |Cl_PointColore|: %p\n",Cl_Point::nom,this); }//end procedure //------------------------------------------------------------------------------ // FONCTION : affiche_pointColore // DESCRIPTION : Affichage des coordonnées et couleur //------------------------------------------------------------------------------- void Cl_PointColore::affiche_pointColore(void)const{ Cl_Point::affiche(); cout << "Couleur du point = " << Color ; }//end procedure //------------------------------------------------------------------------------ // FONCTION : operator << // DESCRIPTION : << Cl_Point //------------------------------------------------------------------------------- ostream & operator << (ostream & sortie,const Cl_PointColore & _P1){ _P1.affiche_pointColore(); return sortie; }//end procedure //------------------------------------------------------------------------------ // START PROGRAM TEST CLASS //------------------------------------------------------------------------------- typedef Cl_Point point; typedef Cl_PointColore pointc; void main(){ pointc a('A',5,5,'V'); pointc b('B',3,2); a.nbrPoints(); cout << a << "\n" << b << "\n" ; a + b; cout << a << endl; } //------------------------------------------------------------------------------ // END PROGRAM TEST CLASS //-------------------------------------------------------------------------------

Conclusion :


Ceci n'est qu'un exemple de référence

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.