Question

FlyPigeonFly - But she can't take off!!

Asked by: IanCool

Was given an object oriented programming question in C++. I've gotta admit that i'm not the best when it comes to programming. I've attempted the question and found myself stuck. Here's the question:

FlyPigeonFly is a game rather like Simon says. It is played on a character-based console by very young children who are just learning to read.
It is an excellent exercise in quick verbal decision making and vocabulary building as well as reading skills.

FlyPigeonFly prints a motion verb ( fly, run, swim, crawl, walk, or roll ), waits for a second, then
prints the name of an entity and repeats the verb (for example: fly ..1s.. pigeon fly!.. ).
The player has ½ second to type y for yes or n for no. (case insensitive)

- If the answer is correct, the player scores, if incorrect, or if there was no answer within the ½ second, the player looses a point. When correct, all the motions the entity can perform are printed.
- When wrong, Ka..BOOM! is printed instead.

The default number of questions per player is 20, but the number can be changed by passing a different
number on the command line when starting the program.
For example: > FlyPigeonFly 10 will ask only 10 questions per player.
At the end of the run the players score (and the score of previous players if there were any) is printed, the
question: Another player (Y/N):  is asked. The game exits if n is typed.

A typical output is reproduced below:
Player 1 starting.
You must answer with the letter y for YES, or n for NO.
Press [Enter] when ready to start:
1  fly pigeon fly!.. y
- I walk - I fly
2  swim Wheelbarrow swim.. n
- I roll
3  crawl ball crawl!..
Ka...BOOM!
4  fly plane fly!.. y
- I roll - I fly.
5  run boat run!.. n
- I roll.
6  walk engine walk!..
Ka..BOOM!
7  roll stone roll!.. y
- I roll.
. . .
16- crawl time crawl!.. y
- I crawl - I fly.
17- swim goose swim!.. n
Ka..BOOM!
18- run lizard run!.. y
- I run - I crawl.
19- run nose run.. y
- I run
20- fly goose fly!.. y
- fly - I walk  I run  I swim.
Player 1 *** score: 16 ***
Well done! Start another player (Y or N)? y
Player 2 starting
You must answer with the letter y for YES, or n for NO.
Press [Enter] when ready to start:
1  crawl giraffe crawl!.. y
Ka..BOOM!
2  roll river roll!.. n
- I run
3  swim crocodile swim!.. y
- I crawl - I walk - I run - I swim
. . .

Design Requirements:
To achieve a simple, easily maintainable object-oriented implementation, FlyPigeonFly uses inheritance, polymorphism and the STL. The minimum class requirement is given here:
* Each individual motion, ( fly, run, swim, crawl, walk, or roll ) is a singleton instance of a concrete class ( FlyMotion, RunMotion, SwimMotion, CrawlMotion, WalkMotion, RollMotion ) that
inherits from the Motion abstract class.
* Motion is a pure abstract class (also called an interface) with no data and the signature of two abstract function members:

// return a C-style string representing the concrete motion
virtual char * toString() const = 0;
// return true if key is found within the motion actors
virtual bool contains(const char * key) const = 0;

Motion has a stream insertion operator that prints - I  then calls the toString() abstract- member function.

* The 6 concrete motion instances each contain a set of actors (C-style strings, const char *) that can perform the motion. These classes implement the Motion interface. Their toString() implementation prints the verb corresponding to the concrete motion and a trailing space, for example: roll , or swim . They must implement the singleton design pattern, so only one instance of a concrete motion can exist at one time
* A main (driver) controls the overall program logic and the interaction with the user. It maintains and activates a collection of 6 Motion pointers (which may, if you wish your program to be as OO as possible, be within another class).
The lists of actors that perform each concrete motion may be either read from file or hard-coded within the driver as you prefer.

-------------------------------------------------------------------------------------------------------------------

The attached code is what i've done so far. I've tried building it in CodeBlock and i've got these errors:

error: cannot allocate an object of type `Motion'
error: because the following virtual functions are abstract:
error: virtual bool Motion::contains(const char*) const
error: virtual char* Motion::toString() const

The first and second error refer to this portion of the code: Located in Motion.cxx

single = new Motion();
----------------------------------------------------------
The third and forth errors refer to these respectively: Located in Motion.h

public:
      virtual char * toString() const = 0;
      virtual bool contains(const char * key) const = 0;

I'm lost. Can anyone help me out? I have less then 3 days left to complete this task.

Thanks a lot in advance. :)

//Motion.h
#ifndef MOTION_H
#define MOTION_H 
#include <iostream>
#include <string> 
using namespace std; 
class Motion
{
    friend ostream &operator << (ostream &out, const Motion &m); 
private:
    static bool instanceFlag;
    static Motion *single; 

public:
    // return a C-style string representing the concrete motion
    virtual char * toString() const = 0;
    // return true if key is found within the motion actors
    virtual bool contains(const char * key) const = 0; 

    static Motion* getInstance();
    inline virtual ~Motion()
    {
        instanceFlag = false;
    }
};
#endif 
//-----------------------------------------------------------
//Motion.cxx
#include <iostream>
#include <sstream>
#include <string>
#include "Motion.h" 
ostream &operator << (ostream &out, const Motion &m)
{
    out << "- I " << m.toString();
    return out;
} 
bool Motion::instanceFlag = false;
Motion* Motion::single = NULL;
Motion* Motion::getInstance()
{
    if(! instanceFlag)
    {
        single = new Motion();
        instanceFlag = true;
        return single;
    }
    else
    {
        return single;
    }
} 
//-----------------------------------------------------------
//FlyMotion.h
#ifndef FLYMOTION_H
#define FLYMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class FlyMotion : public Motion
{
    public:
    static FlyMotion* instance();
    virtual char * toString() const;
    virtual bool contains(const char * key) const;
    //~FlyMotion(){}; 
    private:
    static FlyMotion *inst;
    set <string> actors;
    set<char>::iterator act; 
    FlyMotion();
    //FlyMotion(const FlyMotion&);
}; 

#endif
//-----------------------------------------------------------
//FlyMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "FlyMotion.h" 
using namespace std; 
FlyMotion* FlyMotion::inst = 0; 
FlyMotion::FlyMotion()
{
    inst = 0;
    actors.insert("pigeon");
    actors.insert("plane");
    actors.insert("time");
    actors.insert("goose");
} 
FlyMotion* FlyMotion::instance()
{
    if(inst == 0)
    {
        inst = new FlyMotion;
    }
    return inst;
} 
char* FlyMotion::toString() const
{
    ostringstream outStr; 
    outStr << Motion::toString() << "fly " << flush;
    return outStr.str();
} 
bool FlyMotion::contains(const char *key) const
{
    act=actors.find(key);
    return act!=actors.end();
}
//-----------------------------------------------------------
//mainDRV.cxx
#include <iostream>
#include <set> 
#include "Motion.h"
#include "FlyMotion.h"
#include "RunMotion.h"
#include "SwimMotion.h"
#include "CrawlMotion.h"
#include "WalkMotion.h"
#include "RollMotion.h" 
//const int size = 6; 
int main()
{
    FlyMotion *test1;
    test1 = FlyMotion::getInstance();
    if(test1->contains("pigeon"))
    {
        cout << "Test" << endl;
    } 
    cout << "Press Enter to continue..." << flush;
    cin.get();
    return 0;
}
//-----------------------------------------------------------

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-03 at 07:42:40ID24867454
Tags

C++

Topics

C++ Builder

,

Microsoft Visual C++.Net

,

Microsoft Visual C++

Participating Experts
1
Points
500
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. STL maps
    I just started using STL, and I have instantiated a map. When I try to insert something into the map, I receive an 'Unhandled exception in ????.exe: 0xC0000005: Access Violation'. What am I doing wrong. I also get all kinds of warnings of the form "...Identifier ...
  2. STL and TREE
    Is there predefined TREE Data Structure in STL as List Stack etc. we can use directly in our programs??? I haven't found it... Or should I make it by myself ... Would you please tell me how you use STL to build a TREE? Thanks alot! Regards Yinan
  3. STL deallocate()
    i have a problem in testing STL deallocate() function . ptr=allocate( size,0); memset(ptr,14,size); deallocate(ptr); this should deallocate memory simmilar to delete and free function. i want to test whther the memory is deallocated properly or not. so i fiiled the memory...
  4. She DROPPED it.
    I have a friend who recently dropped her computer. It is now "broken," and here's a description of the symptoms: It will NOT boot up. Pressing the power button only turns on the light within the power button and the STR (save to RAM?) LED on the motherboard. It ...
  5. VBA code to find "Young_*_051205.xls" (FileS…
    I have an Excel macro that needs to determine if a file with the naming pattern "Young_*_051205.xls" exists in the current directory. I currently have the following but am having the corresponding problems: 1. Using the buit-in FileSearch method of Application wor...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ZoppoPosted on 2009-11-03 at 08:52:35ID: 25730924

Hi IanCool,

the problem simply is that it's not possible to instantiate an object for an abstract class (a class which has at least one undeclared virtual function).

Therefor it's not possible to implement the 'Motion::getInstance' as you did.

IMO you have to implement a '...:getInstance' for each Motion-derived class because it has to know for which class it has to instantiate an object or i.e. implement 'Motion::getInstance' as a template method in a way it instantiates the derived class needed.

BTW: IMO you don't need the flag 'instanceFlag' since you can simply check if the instance is NULL ...

ZOPPO

 

by: IanCoolPosted on 2009-11-03 at 18:45:30ID: 25735965

I've moved getInstance into the child class FlyMotion and did some minor alterations. But i'm getting this error now:
|error: cannot call member function `FlyMotion* FlyMotion::getInstance()' without object|

It refers to the line: Located in mainDRV.cxx
test1 = FlyMotion::getInstance();

//Motion.h
#ifndef MOTION_H
#define MOTION_H 
#include <iostream>
#include <string> 
using namespace std; 
class Motion
{
    friend ostream &operator << (ostream &out, const Motion &m); 
private:
    static bool instanceFlag;
    static Motion *single;  
public:
    // return a C-style string representing the concrete motion
    virtual char * toString() const = 0;
    // return true if key is found within the motion actors
    virtual bool contains(const char * key) const = 0;  
    static Motion* getInstance();
    inline virtual ~Motion()
    {
        instanceFlag = false;
    }
};
#endif 
//-----------------------------------------------------------
//Motion.cxx
#include <iostream>
#include <sstream>
#include <string>
#include "Motion.h" 
ostream &operator << (ostream &out, const Motion &m)
{
    out << "- I " << m.toString();
    return out;
} 
//-----------------------------------------------------------
//FlyMotion.h
#ifndef FLYMOTION_H
#define FLYMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class FlyMotion : public Motion
{
    public:
    static FlyMotion* instance();
    virtual char * toString() const;
    virtual bool contains(const char * key) const;
    FlyMotion* getInstance();
    //~FlyMotion(){}; 
    private:
    static FlyMotion *inst;
    static bool instanceFlag;
    set <string> actors;
    set<char>::iterator act;
    static FlyMotion *fly; 
    FlyMotion();
    //FlyMotion(const FlyMotion&);
};  
#endif
//-----------------------------------------------------------
//FlyMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "FlyMotion.h" 
using namespace std; 
FlyMotion* FlyMotion::inst = 0; 
FlyMotion::FlyMotion()
{
    inst = 0;
    actors.insert("pigeon");
    actors.insert("plane");
    actors.insert("time");
    actors.insert("goose");
} 
FlyMotion* FlyMotion::instance()
{
    if(inst == 0)
    {
        inst = new FlyMotion;
    }
    return inst;
} 
char* FlyMotion::toString() const
{
    ostringstream outStr; 
    outStr << Motion::toString() << "fly " << flush;
    return outStr.str();
} 
bool FlyMotion::contains(const char *key) const
{
    act=actors.find(key);
    return act!=actors.end();
} 
bool FlyMotion::instanceFlag = false;
FlyMotion* FlyMotion::single = NULL;
FlyMotion* FlyMotion::getInstance()
{
    if(! instanceFlag)
    {
        fly = new FlyMotion();
        instanceFlag = true;
        return fly;
    }
    else
    {
        return fly;
    }
}
//-----------------------------------------------------------
//mainDRV.cxx
#include <iostream>
#include <set> 
#include "Motion.h"
#include "FlyMotion.h"
#include "RunMotion.h"
#include "SwimMotion.h"
#include "CrawlMotion.h"
#include "WalkMotion.h"
#include "RollMotion.h" 
//const int size = 6; 
int main()
{
    FlyMotion *test1;
    test1 = FlyMotion::getInstance();
    if(test1->contains("pigeon"))
    {
        cout << "Test" << endl;
    } 
    cout << "Press Enter to continue..." << flush;
    cin.get();
    return 0;
}
//-----------------------------------------------------------

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:

Select allOpen in new window

 

by: ZoppoPosted on 2009-11-03 at 23:48:35ID: 25737206

Hi again,

you'll have to declare the 'FlyMotion::getInstance' as a static (class-)function like this:

> class FlyMotion ...
> {
>  ...
>  static FlyMotion* getInstance();
> ...
> };

ZOPPO

 

by: IanCoolPosted on 2009-11-04 at 04:16:57ID: 25738535

Hi there, I've managed to get it compiled and working. Now i have 24 hours to complete the entire assignment. What i understand now is that i have to implement the algorithm in mainDRV.cxx.

Was wondering if anyone can show me how to go about doing this. I've attached a partially working program code below. It now prints out "Test" if it has successfully determined that a certain noun is compatible with the particular child class.

Basically, all 6 child classes FlyMotion, CrawlMotion, RunMotion, WalkMotion, RollMotion, and SwimMotion codings should be identical.


Thank you lots in advance,
IanCool

//mainDRV.cxx
#include <iostream>
#include <set> 
#include "Motion.h"
#include "FlyMotion.h"
#include "RunMotion.h"
#include "SwimMotion.h"
#include "CrawlMotion.h"
#include "WalkMotion.h"
#include "RollMotion.h" 
const int size = 6; 
int main()
{
    cout << "How many questions would you like to answer?" << endl;
    cout << "Player 1 starting." << endl;
    cout << "You must answer with the letter y for YES, or n for NO." << endl;
    cout << "Press [Enter] when ready to start:" << endl; 
    Motion *test1;
    test1 = FlyMotion::getInstance();
    if(test1->contains("plane "))
    {
        cout << "Test" << endl;
    } 
    cout << "Press Enter to continue..." << flush;
    cin.get();
    return 0;
}
//----------------------------------------------------------------
//Motion.cxx
#include <iostream>
#include <sstream>
#include <string>
#include "Motion.h" 
ostream &operator << (ostream &out, const Motion &m)
{
    out << "- I " << m.toString();
    return out;
} 
//----------------------------------------------------------------
//Motion.h
#ifndef MOTION_H
#define MOTION_H 
#include <iostream>
#include <string> 
using namespace std; 
class Motion
{
    friend ostream &operator << (ostream &out, const Motion &m); 
public:
    Motion(){}; 
    // return a C-style string representing the concrete motion
    virtual const char * toString() const = 0;
    // return true if key is found within the motion actors
    virtual const bool contains(const string &key) const = 0;
    static Motion* getInstance();
    virtual ~Motion(){};
};
#endif 
//----------------------------------------------------------------
FlyMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "FlyMotion.h" 
using namespace std; 
bool FlyMotion::instanceFlag = false;
FlyMotion* FlyMotion::singleton = NULL; 
FlyMotion::FlyMotion()
{
    actors.insert("pigeon ");
    actors.insert("plane ");
    actors.insert("time ");
    actors.insert("goose ");
} 
FlyMotion* FlyMotion::getInstance()
{
    if(singleton == 0)
    {
        singleton = new FlyMotion();
        instanceFlag = true;
        return singleton;
    }
    return singleton;
} 
const char* FlyMotion::toString() const
{
    return "fly ";
} 

const bool FlyMotion::contains(const string &key) const
{
    set<string>::const_iterator act=actors.find(key);
    return act!=actors.end();
} 
//----------------------------------------------------------------
//FlyMotion.h
#ifndef FLYMOTION_H
#define FLYMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class FlyMotion : public Motion
{
    public:
        static FlyMotion* getInstance();
        const char *toString() const;
        const bool contains(const string &key) const;
        ~FlyMotion(){instanceFlag = false;} 
    private:
        FlyMotion(); 
        static FlyMotion *singleton;
        static bool instanceFlag;
        set <string> actors;
}; 

#endif 
//----------------------------------------------------------------
//CrawlMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "CrawlMotion.h" 
using namespace std; 
bool CrawlMotion::instanceFlag = false;
CrawlMotion* CrawlMotion::singleton = NULL; 
CrawlMotion::CrawlMotion()
{
    actors.insert("lizard");
    actors.insert("spider");
    actors.insert("crocodile");
} 
CrawlMotion* CrawlMotion::getInstance()
{
    if(singleton == 0)
    {
        singleton = new CrawlMotion();
        instanceFlag = true;
        return singleton;
    }
    return singleton;
} 
const char* CrawlMotion::toString() const
{
   // ostringstream outStr; 
   // outStr << Motion::toString() << "fly " << flush;
    return "crawl ";//outStr.str().c_str();
} 

const bool CrawlMotion::contains(const string &key) const
{
    set<string>::const_iterator act=actors.find(key);
    return act!=actors.end();
} 
//----------------------------------------------------------------
//CrawlMotion.h
#ifndef CRAWLMOTION_H
#define CRAWLMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class CrawlMotion : public Motion
{
    public:
        static CrawlMotion* getInstance();
        const char *toString() const;
        const bool contains(const string &key) const;
        ~CrawlMotion(){instanceFlag = false;} 
    private:
        CrawlMotion(); 
        static CrawlMotion *singleton;
        static bool instanceFlag;
        set <string> actors;
};
#endif 
//----------------------------------------------------------------
//RollMotion.cxx
#include <iostream>
#include <sstream>
#include <string>
#include "RollMotion.h" 
using namespace std; 
bool RollMotion::instanceFlag = false;
RollMotion* RollMotion::singleton = NULL; 
RollMotion::RollMotion()
{
    actors.insert("wheelbarrow");
    actors.insert("ball");
    actors.insert("plane");
    actors.insert("boat");
    actors.insert("stone");
    actors.insert("tire");
} 
RollMotion* RollMotion::getInstance()
{
    if(singleton == 0)
    {
        singleton = new RollMotion();
        instanceFlag = true;
        return singleton;
    }
    return singleton;
} 
const char* RollMotion::toString() const
{
    return "roll ";
} 

const bool RollMotion::contains(const string &key) const
{
    set<string>::const_iterator act=actors.find(key);
    return act!=actors.end();
} 
//----------------------------------------------------------------
//RollMotion.h
#ifndef ROLLMOTION_H
#define ROLLMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class RollMotion : public Motion
{
    public:
        static RollMotion* getInstance();
        const char *toString() const;
        const bool contains(const string &key) const;
        ~RollMotion(){instanceFlag = false;} 
    private:
        RollMotion(); 
        static RollMotion *singleton;
        static bool instanceFlag;
        set <string> actors;
};
#endif 
//----------------------------------------------------------------
//RunMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "RunMotion.h" 
using namespace std; 
bool RunMotion::instanceFlag = false;
RunMotion* RunMotion::singleton = NULL; 
RunMotion::RunMotion()
{
    actors.insert("engine");
    actors.insert("goose");
    actors.insert("lizard");
    actors.insert("nose");
    actors.insert("giraffe");
    actors.insert("river");
    actors.insert("crocodile");
    actors.insert("spider");
    actors.insert("chicken");
} 
RunMotion* RunMotion::getInstance()
{
    if(singleton == 0)
    {
        singleton = new RunMotion();
        instanceFlag = true;
        return singleton;
    }
    return singleton;
} 
const char* RunMotion::toString() const
{
    return "run ";
} 

const bool RunMotion::contains(const string &key) const
{
    set<string>::const_iterator act=actors.find(key);
    return act!=actors.end();
} 
//----------------------------------------------------------------
//RunMotion.h
#ifndef RUNMOTION_H
#define RUNMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class RunMotion : public Motion
{
    public:
        static RunMotion* getInstance();
        const char *toString() const;
        const bool contains(const string &key) const;
        ~RunMotion(){instanceFlag = false;} 
    private:
        RunMotion(); 
        static RunMotion *singleton;
        static bool instanceFlag;
        set <string> actors;
};
#endif 
//----------------------------------------------------------------
//SwimMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "SwimMotion.h" 
using namespace std; 
bool SwimMotion::instanceFlag = false;
SwimMotion* SwimMotion::singleton = NULL; 
SwimMotion::SwimMotion()
{
    actors.insert("goose");
    actors.insert("crocodile");
    actors.insert("whale");
} 
SwimMotion* SwimMotion::getInstance()
{
    if(singleton == 0)
    {
        singleton = new SwimMotion();
        instanceFlag = true;
        return singleton;
    }
    return singleton;
} 
const char* SwimMotion::toString() const
{
    return "swim ";
} 

const bool SwimMotion::contains(const string &key) const
{
    set<string>::const_iterator act=actors.find(key);
    return act!=actors.end();
} 
//----------------------------------------------------------------
//SwimMotion.h
#ifndef SWIMMOTION_H
#define SWIMMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class SwimMotion : public Motion
{
    public:
        static SwimMotion* getInstance();
        const char *toString() const;
        const bool contains(const string &key) const;
        ~SwimMotion(){instanceFlag = false;} 
    private:
        SwimMotion(); 
        static SwimMotion *singleton;
        static bool instanceFlag;
        set <string> actors;
};
#endif 
//----------------------------------------------------------------
//WalkMotion.cxx
#include <iostream>
#include <sstream>
#include <string> 
#include "WalkMotion.h" 
using namespace std; 
bool WalkMotion::instanceFlag = false;
WalkMotion* WalkMotion::singleton = NULL; 
WalkMotion::WalkMotion()
{
    actors.insert("pigeon");
    actors.insert("goose");
    actors.insert("giraffe");
    actors.insert("chicken");
} 
WalkMotion* WalkMotion::getInstance()
{
    if(singleton == 0)
    {
        singleton = new WalkMotion();
        instanceFlag = true;
        return singleton;
    }
    return singleton;
} 
const char* WalkMotion::toString() const
{
    return "walk ";
} 

const bool WalkMotion::contains(const string &key) const
{
    set<string>::const_iterator act=actors.find(key);
    return act!=actors.end();
} 
//----------------------------------------------------------------
//WalkMotion.h
#ifndef WALKMOTION_H
#define WALKMOTION_H 
#include <iostream>
#include <string>
#include <set> 
using namespace std; 
#include "Motion.h" 
class WalkMotion : public Motion
{
    public:
        static WalkMotion* getInstance();
        const char *toString() const;
        const bool contains(const string &key) const;
        ~WalkMotion(){instanceFlag = false;} 
    private:
        WalkMotion(); 
        static WalkMotion *singleton;
        static bool instanceFlag;
        set <string> actors;
};
#endif 
//----------------------------------------------------------------

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:

Select allOpen in new window

 

by: ZoppoPosted on 2009-11-04 at 04:34:59ID: 25738647

Well, now you have to generate instanced for all derived motions, put them into a container [which is mentioned as 'driver': It maintains and activates a collection of 6 Motion pointers (which may, if you wish your program to be as OO as possible, be within another class).]

With this you just need to implement a functionality which finds all Motions which fit to a noun and check if the wanted one is part of these ...

 

by: IanCoolPosted on 2009-11-05 at 22:08:38ID: 25756932

I've basically gotten everything written out properly. It's working except for the 0.5sec timer. Can you give me a templete example so that i can utilize it easily?

 

by: IanCoolPosted on 2009-11-05 at 22:09:49ID: 25756940

I need the the program to print out "KA...BOOM" if nothing is done within 0.5sec and move on to the next question.

 

by: ZoppoPosted on 2009-11-06 at 02:03:24ID: 25757871

Hi,

well, I'm not sure about to implement this issue.  - the problem is you can't use any function which waits for keyboard input if you want to break the workflow within a given time.

IMO you could either implement the keyboard input using a polling mechanism (i.e. doing a loop which repeatedly checks for input but breaks after a given time span) or implement i.e. a thread which anyhow signals the main thread about a time overrun - hard to say which is the better strategy for you app, but the easier one may be the polling.

For this you'll have to get the initial time, then do a loop for at maximum 0,5 seconds which checks if any input happened.

To get info about current time you can include <clock.h> and use 'clock()'. If you gonna implement polling you need to write a loop to check input stream instead of waiting until the user entered something ...

ZOPPO

ZOPPO

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...