#include #include #include /* Tout est public dans cette classe */ class BToto { public: static int nb; static int cons, dest, conscpy, opeg; static void InfoCons(); float px,py; int id; BToto(int oid=0, float x=1., float y=2.); BToto(BToto const & bt); ~BToto(void); /* Les methodes virtuelles sont la pour pouvoir etre utilisees */ /* par les classes derivees de celle la par defaut. */ void Set(float a=0., float b=0.); BToto& operator=(const BToto& bt); virtual BToto Scale(float sc); virtual int GetId() { return id; } ; virtual void Display(); virtual char * ClassNom() { return "BToto"; } ; }; int BToto::nb = 0; int BToto::cons = 0; int BToto::dest = 0; int BToto::conscpy = 0; int BToto::opeg = 0; void BToto::InfoCons() { printf("BToto::InfoCons() NbConsCall= %d NbCopyConsCall= %d NbDestCall= %d NbOpegal=%d \n", cons, conscpy, dest, opeg); } BToto::BToto(int oid, float x, float y) { cons++; nb += 10; if (oid == 0) oid = nb; printf("BToto::BToto(int oid) : %d -Pointer= %lx\n", oid,(long)this); px = x; py = y; id = oid; } BToto::BToto(BToto const & b) { conscpy++; printf("BToto::BToto(BToto const & : %d -Pointer= %lx ) Pointer= %lx\n", b.id,(long)(&b),(long)this); id = b.id; px = b.px; py = b.py; } BToto::~BToto(void) { dest++; printf("BToto::~BToto: %d \n", id); return; } void BToto::Set(float a, float b) { px = a; py = b; return; } BToto BToto::Scale(float sc) { printf("BToto::Scale : %d -Pointer= %lx\n", id,(long)this); BToto ret(*this); ret.px *= sc; ret.py *= sc; return(ret); } BToto& BToto::operator=(const BToto& b) { opeg++; printf("BToto::Operateur= (BToto const & : %d -Pointer= %lx ) Pointer= %lx\n", b.id,(long)(&b),(long)this); id = b.id; px = b.px; py = b.py; return(*this); } void BToto::Display() { printf("BToto::Display() %g %g (Id= %d) Point=%lx\n", px, py, id,(long)this); return; } class PointBToto { public: static int num; PointBToto(); virtual ~PointBToto(); operator BToto (); BToto * bt; }; int PointBToto::num = 0; PointBToto::PointBToto() { num++; bt = new BToto(10000*num, num*3., num*6.); } PointBToto::~PointBToto() { delete bt; } PointBToto::operator BToto () { printf("PointBToto::operator BToto %d \n", bt->GetId()); return(*bt); } void test_cons(); main(int narg, char *arg[]) { printf(" ---- Programme test constructeur et operateur egal ----- \n"); test_cons(); printf("> BToto::InfoCons() \n"); BToto::InfoCons(); } void test_cons() { printf("> BToto b1; b1.Display(); \n"); BToto b1; b1.Display(); printf("> BToto b2(344, 5, 15.); b2.Display(); b1 = b2; b1.Display(); \n"); BToto b2(344, 5, 15.); b2.Display(); b1 = b2; b1.Display(); printf("> BToto b3(666, 8., 16.); BToto b4 = b3.Scale(2.); b4.Display(); \n"); BToto b3(666, 8., 16.); BToto b4 = b3.Scale(2.); b4.Display(); printf("> BToto b5(788, 10., 20.); BToto b6; b6 = b5.Scale(3.); b6.Display(); \n"); BToto b5(788, 10., 20.); BToto b6; b6 = b5.Scale(3.); b6.Display(); printf("> PointBToto pb1; ((BToto)pb1).Display(); \n"); PointBToto pb1; ((BToto)pb1).Display(); printf("> PointBToto pb2; BToto bb1 = pb1; bb1.Display(); \n"); PointBToto pb2; BToto bb1 = pb1; bb1.Display(); }