|
Trouver une ressource
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum. Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
CONSTRUCTEURS ET OPERATEUR + HERITAGE SIMPLE
Information sur la source
Description
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
- #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
- //-------------------------------------------------------------------------------
#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
Sources du même auteur
Sources de la même categorie
Sources en rapport avec celle ci
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Constructeur par recopie et Héritage [ par heleos ]
Bonjour, J'ai un légé soucis avec un programme c++ Je n'arrive pas à faire un constructeur par recopie d'une sous classe. J'ai test&#
Besoin d'un peu d'aide sur l'héritage et les constructeurs [ par neodelphi ]
Bonjour tout le monde, j'ai deux petites question à propose du langage c++ sur l'héritage et les constructeurs:Tout d'abord, je voudrai savo
question de "cours" : constructeurs de classe dérivée et classe de base [ par mhassine ]
Bonjour,j'ai une classe personne et une classe etudiant dérivée de personne.J'ai le constructeur Personne(string n)J'ai au
Help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! [ par Miss1 ]
pb avec constructeur par recopie. il faut que je crée une classe tableau dans lequels je dois mettre un constructeur prenant en compte un paramétre ta
voici mon code [ par Miss1 ]
voici mon code mais je n'arrive pas a compiler class tableau { private: int taille; // variables// int *pointeur
erreur dans la classe [ par Miss1 ]
je n'arrive pas a compiler. le sujet est de crée une taille, un pointeur sur le 1er element du tableau, un constructeur, un constructeur par recopie ,
opérateur [ par mmuller57 ]
Dans la source de factorielle, y'a ça :i*=j, je comprend pas a quoi sert le i* ? Qqun peut m'aider?Merci @+
Donner des valeurs au éléments d'un tableaux dans le constructeur d'une classe [ par ProGamer ]
Dans la définition de ma classe, j'ai un tableau d'entiers. Dans le constructeur de la classe, j'ai besoin de donner des valeurs aux éléments du table
copie contructeur [ par MrKribou ]
Je voudrai avoir des sujet a propos des copie constructeur. J'avais faire un prog qui plantait pour une raison assez obscure et en tatonnant une bonne
Constructeur sur un tableau de classes [ par Supo ]
Lorsque je crée un tableau de classe, disons avecnomdelaclasse instance[10];et que j'ai un constructeur pour cette classe, comment je fait pour donner
|
Téléchargements
Logiciels à télécharger sur le même thème :
|