#include <iostream>
using namespace std;
void koukan(int x, int y){
int tmp = x;
x = y;
y = tmp;
}
int main(){
int x=3;
int y=4;
cout << "交換前\n";
cout << "x=" << x << ", y=" << y << "\n";
koukan(x,y);
cout << "交換後\n";
cout << "x=" << x << ", y=" << y << "\n";
return 0;
}
|
int main(){
int in1, in2;
AND o;
in1 = 1; // 二つの入力の設定
in2 = 0;
o.calc_out(in1,in2); // 二つの入力を渡すことで、メンバ変数 out が計算される
cout << "input1=" << in1 << "\n";
cout << "input2=" << in2 << "\n";
cout << "output=" << o.get_out() << "\n";
return 0;
}
|
input1=1 input2=0 output=0 |

// クラス宣言部
#include <iostream>
using namespace std;
#define EDGE_NUM 2 // ノードに接続可能なエッジの数
class Edge;
class Node{
private:
Edge *edge[EDGE_NUM]; // エッジへのポインタの配列。
//ここでは、二つのポインタ edge[0] と edge[1] があると思えば良い。
int edgenum; // 現在接続しているエッジの数
public:
// コンストラクタ
Node();
// デストラクタ
~Node();
void edgeAdd(Edge *e); // ノードにエッジを接続
};
class Edge{
private:
Node *node;
public:
// コンストラクタ
Edge();
// デストラクタ
~Edge();
void nodeAdd(Node *n); // エッジにノードを接続
};
|
// クラス利用部
int main(){
Node node0, node1, node2;
Edge edge1, edge2;
node0.edgeAdd(&edge1);
node0.edgeAdd(&edge2);
edge1.nodeAdd(&node1);
edge2.nodeAdd(&node2);
return 0;
}
|
// クラス利用部
int main(){
Node node0, node1, node2;
Edge edge1, edge2;
node0.edgeAdd(&edge1);
node0.edgeAdd(&edge2);
edge1.nodeAdd(&node1);
edge2.nodeAdd(&node2);
// 以下からが追加した部分
node1.set_out(1); // 入力1に 1 をセット
node2.set_out(0); // 入力2に 0 をセット
node0.set_from_edges(); // 入力1と入力2から値を取得し、AND 演算の結果を node0 の out に格納
cout << "input1=" << node1.get_out() << "\n";
cout << "input2=" << node2.get_out() << "\n";
cout << "output=" << node0.get_out() << "\n";
return 0;
}
|
input1=1 input2=0 output=0 |