#include<iostream>
using namespace std;
class State{
protected:
State* next_a, * next_b;
char output;
public:
State(State &a, State &b):next_a(&a), next_b(&b){cout << "0" << endl;}
virtual State* transition(char)=0;
};
class State1: public State{
public:
State1(State &a, State &b):State(a,b){cout << "1" << endl;}
State* transition(char);
};
class State2: public State{
public:
State2(State &a, State &b):State(a,b){cout << "2" << endl;}
State* transition(char);
};
class State3: public State{
public:
State3(State &a, State &b):State(a,b){cout << "3" << endl;}
State* transition(char);
};
State* State1::transition(char input){
cout << endl << " Current state: 1";
switch(input){
case 'a':
output='y';
cout << endl << "Output:" << output;
cout << endl << "Next state: 1";
return next_a;
case 'b':
output='x';
cout << endl << "Output:" << output;
cout << endl << "Next state: 2";
return next_b;
default:
cout << endl << "Undefined input";
cout << endl << "Next state: Unchanged";
return this;
}
}
State* State2::transition(char input){
cout << endl << " Current state: 2";
switch(input){
case 'a':
output='y';
cout << endl << "Output:" << output;
cout << endl << "Next state: 3";
return next_a;
case 'b':
output='x';
cout << endl << "Output:" << output;
cout << endl << "Next state: 2";
return next_b;
default:
cout << endl << "Undefined input";
cout << endl << "Next state: Unchanged";
return this;
}
}
State* State3::transition(char input){
cout << endl << " Current state: 3";
switch(input){
case 'a':
output='y';
cout << endl << "Output:" << output;
cout << endl << "Next state: 1";
return next_a;
case 'b':
output='x';
cout << endl << "Output:" << output;
cout << endl << "Next state: 2";
return next_b;
default:
cout << endl << "Undefined input";
cout << endl << "Next state: Unchanged";
return this;
}
}
class FMS{
State1 s1;
State2 s2;
State3 s3;
State *current;
public:
FMS():s1(s1,s2), s2(s3,s2), s3(s1,s3), current(&s1){cout << "5" << endl;};
void run();
};
int main(void)
{
FMS f1;
return 0;
}
Cho mình hỏi là khi tạo ra 1 đối tượng FMS, hàm khởi tạo sẽ được gọi đến, lúc đó thì các s1 s2 s3 đã được tạo ra chữa ?