[Cs3] Question About States and Inheritance

Mikhail Nesterenko mikhail at cs.kent.edu
Sat Mar 1 10:27:14 EST 2014


Mikhail Nesterenko wrote:
> > 
> > 
> > For this lab, Lab 7, you asked us to only show one example of an
> > incorrect transition. This is simple to do since we're hard-coding
> > the program and there's no user input. But if we did have user input
> > and had to account for every possible transition being called for
> > every possible state (that's 6 transitions and 5 states, gives me 30
> > methods) what would the easiest way of coding this?
> > 
> > 
> > The way I understand it, you would need to fill out methods like so:
> > 
> > 
> > ProcessState* New::instance(){};
> > void New::admit(Process *p){};
> > void New::dispatch(Process *p){} //does nothing;
> > void New::interrupt(Process *p){} //does nothing;
> > void New::startWait(Process *p){} //does nothing;
> > void New::endWait(Process *p){} //does nothing;
> > void New::exit(Process *p){} //does nothing;
> > 
> > 
> 
> Now that you mention it, this is a perfect case for abstract function
> with a default body (to be defined in the abstract class State):
> 
>   // abstract class
>   class State{
>      virtual void dispatch(Process*p) = 0;
>   };
> 
> 
>   void State::dispatch(Process* p){}
> 
> 

Actually, that might not work. The pure virtual function (as shown
above) requires the concrete class to provide implementation, the
default body is there in case the concrete class wants to use.

In this particular case, it is simpler to just provide regular virtual
function: this amounts to default implementation but can be overridden:


    // abstract class
    class State{
        virtual void dispatch(Process* p){};
    };

I recoded Zork example this way on the course's website. Please take a
look.

Thanks,
--
Mikhail


More information about the cs3 mailing list