function in the class have addresses

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 State1:public 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 State2:public 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 state1:ptr=&s1;
ptr->ChangeState();
break;
case state2:ptr=&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)
 
G

Gernot Frisch

[snip]
//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 state1:ptr=&s1;
ptr->ChangeState();
break;
case state2:ptr=&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)

Keep you code as short as possible.

Do you want:

class A
{
void foo() {printf("FooA");}
};

class B : public A
{
void foo() {printf("FooB";}
}

void bar(A* pA)
{
}


// them try:
int main()
{
B b;
bar( (A*) &b);
}

// you don't need the (A*) cast explicitly, though
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top