関数(様々な引数)

プログラム例 6.1] 関数(様々な引数)

/****************************/
/* 関数(様々な引数)       */
/*      coded by Y.Suganuma */
/****************************/
#include <iostream>

using namespace std;

				// クラス Complex
class Complex {
	public :
		int x, y;
		Complex(int x1, int y1) {
			x = x1;
			y = y1;
		}
};
				// 関数 func
int func(int a, int b, int *c, Complex d1, Complex *d2, int e = 5)
{
	d1.x    = -100;
	(*d2).x = -200;
	a      += 10;
	c[0]    = 30;
	return (a + b + e);
}
				// main
int main()
{
		int x   = 1;
		int y   = 2;
		int z[] = {10, 20};
		Complex w1(100, 200);
		Complex *w2 = new Complex(100, 200);
		cout << "関数を呼ぶ前:\n";
		cout << "    x: " << x << " y: " << y << " z: " << z[0] << " " << z[1] << " w1.x: " << w1.x << " w2.x: " << (*w2).x << endl;
		int r = func(x, y, z, w1, w2);
		cout << "関数を呼んだ後:\n";
		cout << "    x: " << x << " y: " << y << " z: " << z[0] << " " << z[1] << " w1.x: " << w1.x << " w2.x: " << (*w2).x << " r: " << r << endl;

	return 0;
}