function in the class have addresses

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nik

    function in the class have addresses

    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 "statemachineco nst.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 CurrentStateDis play(int) ;

    int Event1_Handler( )
    {
    return state1;
    }
    int Event2_Handler( )
    {
    return state2;
    }
    int Event3_Handler( )
    {
    return state1;
    }
    virtual void ChangeState()
    {
    }
    };

    int State::GetdataS tate()
    {
    cout<<"\nEnter the state ";
    cin>>currState;
    return currState;
    }

    int State::GetdataE vent()
    {
    cout<<"\nEnter the event ";
    cin>>currEvent;
    return currEvent;
    }

    void State::CurrentS tateDisplay(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(GetdataE vent())//certain condition
    {
    case event1:a=Event1 _Handler();
    break;
    case event2:a=Event2 _Handler();
    break;
    default:
    cout<<"\nINVALI D STATE ";
    a=0;
    }
    CurrentStateDis play(a);
    }
    };
    //derived State 2 from the base class State

    class State2:public State
    {
    public:
    void ChangeState()
    {
    switch(GetdataE vent())
    {
    case event2:a=Event2 _Handler();
    break;
    case event3:a=Event3 _Handler();
    break;
    default:
    cout<<"\nINVALI D STATE ";
    a=0;
    }
    CurrentStateDis play(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)

  • Gernot Frisch

    #2
    Re: function in the class have addresses

    [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



    Comment

    Working...