01 /****************************/
02 /* private 変数 */
03 /* coded by Y.Suganuma */
04 /****************************/
05 #include <iostream>
06 using namespace std;
07
08 /***********************/
09 /* クラスExampleの定義 */
10 /***********************/
11 class Example {
12 // private メンバー変数
13 int x;
14
15 public:
16 // public メンバー変数
17 int y;
18 // public メンバー関数,値の設定
19 // void v_set1();
20 void v_set1()
21 {
22 x = 10; // メンバー関数からは参照可能
23 y = 20;
24 }
25 // public メンバー関数,値の設定
26 void v_set2()
27 {
28 x = 30; // メンバー関数からは参照可能
29 y = 40;
30 }
31 // public メンバー関数,値の出力
32 void output()
33 {
34 cout << " x = " << x << ", y = " << y << endl;
35 }
36 }; // 「;」が必要なことに注意
37 // public メンバー関数,値の設定
38 /*
39 void Example::v_set1()
40 {
41 x = 10;
42 y = 20;
43 }
44 */
45
46 /*************/
47 /* main 関数 */
48 /*************/
49 int main()
50 {
51 Example t1; // Example 型オブジェクト
52 Example *t2 = new Example; // Example 型オブジェクトへのポインタ
53
54 t1.v_set1(); // メンバー関数の呼び出し
55 t2->v_set2(); // メンバー関数の呼び出し
56
57 cout << "オブジェクト t1\n";
58 cout << " y = " << t1.y << endl; // 変数 y の参照,変数 x の参照はできない
59 t1.output();
60 cout << "オブジェクト t2\n";
61 cout << " y = " << t2->y << endl; // 変数 y の参照,変数 x の参照はできない
62 t2->output();
63
64 return 0;
65 }