/****************************/
/* オブジェクトの複製 */
/* coded by Y.Suganuma */
/****************************/
#include <stdio.h>
class Complex {
public :
double r_part;
double i_part;
Complex(double x, double y)
{
r_part = x;
i_part = y;
}
};
class Example {
public :
double x;
double y;
Complex *c;
Example(double x1, double y1)
{
x = x1;
y = y1;
c = new Complex(x, y);
}
};
int main() {
// 基本オブジェクト
printf(" 基本オブジェクト\n");
Example e10(1, 2); // new を使用しない
Example *e11 = new Example(1, 2); // new の利用(ポインタ)
printf("e10 x %f y %f\n", e10.x, e10.y);
printf(" c (%f, %f)\n", (e10.c)->r_part, (e10.c)->i_part);
printf("e11 x %f y %f\n", e11->x, e11->y);
printf(" c (%f, %f)\n", (e11->c)->r_part, (e11->c)->i_part);
// e10,e11 を e20,e21 に代入後,e20.y,e21->y を 20,
// (e20.c)->r_part,(e21->c)->r_part を 100 に変更
printf(" e10,e11 を e20,e21 に代入後,e20.y,e21->y を 20,\n");
printf(" (e20.c)->r_part,(e21->c)->r_part を 100 に変更\n");
Example e20 = e10;
Example *e21 = e11;
e20.y = 20;
(e20.c)->r_part = 200;
e21->y = 20;
(e21->c)->r_part = 200;
printf("e10 x %f y %f\n", e10.x, e10.y);
printf(" c (%f, %f)\n", (e10.c)->r_part, (e10.c)->i_part);
printf("e11 x %f y %f\n", e11->x, e11->y);
printf(" c (%f, %f)\n", (e11->c)->r_part, (e11->c)->i_part);
return 0;
}