N
nik
hello friends,
I have a serious problem .i have a base class having 15 member function
and there r 4 classes derived from this base class .the situation is
like if certain condion is met then the particular function get
executed.I have a example following with two classes
// fsmvir.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream.h>
#include "statemachineconst.h"
int a;
class State
{
protected:
int currState;
int currEvent;
public:
int GetdataState() ;
// display the current state from the user
int GetdataEvent() ;
// get the current event from the user
void CurrentStateDisplay(int) ;
int Event1_Handler()
{
return state1;
}
int Event2_Handler()
{
return state2;
}
int Event3_Handler()
{
return state1;
}
virtual void ChangeState()
{
}
};
int State::GetdataState()
{
cout<<"\nEnter the state ";
cin>>currState;
return currState;
}
int State::GetdataEvent()
{
cout<<"\nEnter the event ";
cin>>currEvent;
return currEvent;
}
void State::CurrentStateDisplay(int b)
{
if(b==0)
cout<<"\nERROR";
else
cout<<"\nYOU R IN STATE "<<a;
}
//derived State 1 from the base class State
class State1ublic State
{
public:
void ChangeState()
{
switch(GetdataEvent())//certain condition
{
case event1:a=Event1_Handler();
break;
case event2:a=Event2_Handler();
break;
default:
cout<<"\nINVALID STATE ";
a=0;
}
CurrentStateDisplay(a);
}
};
//derived State 2 from the base class State
class State2ublic State
{
public:
void ChangeState()
{
switch(GetdataEvent())
{
case event2:a=Event2_Handler();
break;
case event3:a=Event3_Handler();
break;
default:
cout<<"\nINVALID STATE ";
a=0;
}
CurrentStateDisplay(a);
}
};
//main function
int main(int argc, char* argv[])
{
char ch='y';
State *ptr;
State s;
State1 s1;
State2 s2;
ptr=&s;
while(ch=='y')
{
a=1;
switch(ptr->GetdataState())
{
case state1tr=&s1;
ptr->ChangeState();
break;
case state2tr=&s2;
ptr->ChangeState();
break;
default:cout<<"\nINVALID STATE";
}
cout<<"\nTO CONTINUE PRESS y ";
cin>>ch;
}
return 0;
}
can i have the simpler way to call the member function of the base
class.(i.e by any mathematical relation)
I have a serious problem .i have a base class having 15 member function
and there r 4 classes derived from this base class .the situation is
like if certain condion is met then the particular function get
executed.I have a example following with two classes
// fsmvir.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream.h>
#include "statemachineconst.h"
int a;
class State
{
protected:
int currState;
int currEvent;
public:
int GetdataState() ;
// display the current state from the user
int GetdataEvent() ;
// get the current event from the user
void CurrentStateDisplay(int) ;
int Event1_Handler()
{
return state1;
}
int Event2_Handler()
{
return state2;
}
int Event3_Handler()
{
return state1;
}
virtual void ChangeState()
{
}
};
int State::GetdataState()
{
cout<<"\nEnter the state ";
cin>>currState;
return currState;
}
int State::GetdataEvent()
{
cout<<"\nEnter the event ";
cin>>currEvent;
return currEvent;
}
void State::CurrentStateDisplay(int b)
{
if(b==0)
cout<<"\nERROR";
else
cout<<"\nYOU R IN STATE "<<a;
}
//derived State 1 from the base class State
class State1ublic State
{
public:
void ChangeState()
{
switch(GetdataEvent())//certain condition
{
case event1:a=Event1_Handler();
break;
case event2:a=Event2_Handler();
break;
default:
cout<<"\nINVALID STATE ";
a=0;
}
CurrentStateDisplay(a);
}
};
//derived State 2 from the base class State
class State2ublic State
{
public:
void ChangeState()
{
switch(GetdataEvent())
{
case event2:a=Event2_Handler();
break;
case event3:a=Event3_Handler();
break;
default:
cout<<"\nINVALID STATE ";
a=0;
}
CurrentStateDisplay(a);
}
};
//main function
int main(int argc, char* argv[])
{
char ch='y';
State *ptr;
State s;
State1 s1;
State2 s2;
ptr=&s;
while(ch=='y')
{
a=1;
switch(ptr->GetdataState())
{
case state1tr=&s1;
ptr->ChangeState();
break;
case state2tr=&s2;
ptr->ChangeState();
break;
default:cout<<"\nINVALID STATE";
}
cout<<"\nTO CONTINUE PRESS y ";
cin>>ch;
}
return 0;
}
can i have the simpler way to call the member function of the base
class.(i.e by any mathematical relation)