1 | #include<stdlib.h>
|
---|
2 | #include<stdio.h>
|
---|
3 | #include<string.h>
|
---|
4 |
|
---|
5 | /* Tout est public dans cette classe */
|
---|
6 |
|
---|
7 | class BToto
|
---|
8 | {
|
---|
9 | public:
|
---|
10 | static int nb;
|
---|
11 | static int cons, dest, conscpy, opeg;
|
---|
12 |
|
---|
13 | static void InfoCons();
|
---|
14 |
|
---|
15 | float px,py;
|
---|
16 | int id;
|
---|
17 |
|
---|
18 | BToto(int oid=0, float x=1., float y=2.);
|
---|
19 | BToto(BToto const & bt);
|
---|
20 | ~BToto(void);
|
---|
21 |
|
---|
22 |
|
---|
23 | /* Les methodes virtuelles sont la pour pouvoir etre utilisees */
|
---|
24 | /* par les classes derivees de celle la par defaut. */
|
---|
25 | void Set(float a=0., float b=0.);
|
---|
26 | BToto& operator=(const BToto& bt);
|
---|
27 | virtual BToto Scale(float sc);
|
---|
28 | virtual int GetId() { return id; } ;
|
---|
29 | virtual void Display();
|
---|
30 | virtual char * ClassNom() { return "BToto"; } ;
|
---|
31 |
|
---|
32 | };
|
---|
33 |
|
---|
34 | int BToto::nb = 0;
|
---|
35 | int BToto::cons = 0;
|
---|
36 | int BToto::dest = 0;
|
---|
37 | int BToto::conscpy = 0;
|
---|
38 | int BToto::opeg = 0;
|
---|
39 |
|
---|
40 | void BToto::InfoCons()
|
---|
41 | {
|
---|
42 | printf("BToto::InfoCons() NbConsCall= %d NbCopyConsCall= %d NbDestCall= %d NbOpegal=%d \n", cons, conscpy, dest, opeg);
|
---|
43 | }
|
---|
44 |
|
---|
45 | BToto::BToto(int oid, float x, float y)
|
---|
46 | {
|
---|
47 | cons++;
|
---|
48 | nb += 10;
|
---|
49 | if (oid == 0) oid = nb;
|
---|
50 | printf("BToto::BToto(int oid) : %d -Pointer= %lx\n", oid,(long)this);
|
---|
51 | px = x; py = y;
|
---|
52 | id = oid;
|
---|
53 | }
|
---|
54 |
|
---|
55 | BToto::BToto(BToto const & b)
|
---|
56 | {
|
---|
57 | conscpy++;
|
---|
58 | printf("BToto::BToto(BToto const & : %d -Pointer= %lx ) Pointer= %lx\n",
|
---|
59 | b.id,(long)(&b),(long)this);
|
---|
60 | id = b.id;
|
---|
61 | px = b.px;
|
---|
62 | py = b.py;
|
---|
63 | }
|
---|
64 |
|
---|
65 | BToto::~BToto(void)
|
---|
66 | {
|
---|
67 | dest++;
|
---|
68 | printf("BToto::~BToto: %d \n", id);
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void BToto::Set(float a, float b)
|
---|
73 | {
|
---|
74 | px = a; py = b;
|
---|
75 | return;
|
---|
76 | }
|
---|
77 |
|
---|
78 | BToto BToto::Scale(float sc)
|
---|
79 | {
|
---|
80 | printf("BToto::Scale : %d -Pointer= %lx\n", id,(long)this);
|
---|
81 |
|
---|
82 | BToto ret(*this);
|
---|
83 | ret.px *= sc;
|
---|
84 | ret.py *= sc;
|
---|
85 | return(ret);
|
---|
86 | }
|
---|
87 |
|
---|
88 | BToto& BToto::operator=(const BToto& b)
|
---|
89 | {
|
---|
90 | opeg++;
|
---|
91 | printf("BToto::Operateur= (BToto const & : %d -Pointer= %lx ) Pointer= %lx\n",
|
---|
92 | b.id,(long)(&b),(long)this);
|
---|
93 | id = b.id;
|
---|
94 | px = b.px;
|
---|
95 | py = b.py;
|
---|
96 | return(*this);
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | void BToto::Display()
|
---|
101 | {
|
---|
102 | printf("BToto::Display() %g %g (Id= %d) Point=%lx\n", px, py, id,(long)this);
|
---|
103 | return;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | class PointBToto
|
---|
108 | {
|
---|
109 | public:
|
---|
110 | static int num;
|
---|
111 | PointBToto();
|
---|
112 | virtual ~PointBToto();
|
---|
113 | operator BToto ();
|
---|
114 |
|
---|
115 | BToto * bt;
|
---|
116 | };
|
---|
117 |
|
---|
118 |
|
---|
119 | int PointBToto::num = 0;
|
---|
120 | PointBToto::PointBToto()
|
---|
121 | {
|
---|
122 | num++;
|
---|
123 | bt = new BToto(10000*num, num*3., num*6.);
|
---|
124 | }
|
---|
125 |
|
---|
126 | PointBToto::~PointBToto()
|
---|
127 | {
|
---|
128 | delete bt;
|
---|
129 | }
|
---|
130 |
|
---|
131 | PointBToto::operator BToto ()
|
---|
132 | {
|
---|
133 | printf("PointBToto::operator BToto %d \n", bt->GetId());
|
---|
134 | return(*bt);
|
---|
135 | }
|
---|
136 |
|
---|
137 | void test_cons();
|
---|
138 |
|
---|
139 | main(int narg, char *arg[])
|
---|
140 | {
|
---|
141 | printf(" ---- Programme test constructeur et operateur egal ----- \n");
|
---|
142 | test_cons();
|
---|
143 | printf("> BToto::InfoCons() \n");
|
---|
144 | BToto::InfoCons();
|
---|
145 | }
|
---|
146 |
|
---|
147 | void test_cons()
|
---|
148 | {
|
---|
149 | printf("> BToto b1; b1.Display(); \n");
|
---|
150 | BToto b1;
|
---|
151 | b1.Display();
|
---|
152 | printf("> BToto b2(344, 5, 15.); b2.Display(); b1 = b2; b1.Display(); \n");
|
---|
153 | BToto b2(344, 5, 15.);
|
---|
154 | b2.Display();
|
---|
155 | b1 = b2;
|
---|
156 | b1.Display();
|
---|
157 | printf("> BToto b3(666, 8., 16.); BToto b4 = b3.Scale(2.); b4.Display(); \n");
|
---|
158 | BToto b3(666, 8., 16.);
|
---|
159 | BToto b4 = b3.Scale(2.);
|
---|
160 | b4.Display();
|
---|
161 | printf("> BToto b5(788, 10., 20.); BToto b6; b6 = b5.Scale(3.); b6.Display(); \n");
|
---|
162 | BToto b5(788, 10., 20.);
|
---|
163 | BToto b6;
|
---|
164 | b6 = b5.Scale(3.);
|
---|
165 | b6.Display();
|
---|
166 | printf("> PointBToto pb1; ((BToto)pb1).Display(); \n");
|
---|
167 | PointBToto pb1;
|
---|
168 | ((BToto)pb1).Display();
|
---|
169 | printf("> PointBToto pb2; BToto bb1 = pb1; bb1.Display(); \n");
|
---|
170 | PointBToto pb2;
|
---|
171 | BToto bb1 = pb1;
|
---|
172 | bb1.Display();
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|