String Insertion Operation & Abstract Classes

Hey, ive got most of my program working just a few glitches that i cant seem to get past.
the program has a PURE ABSTRACT CLASS FILE...called 'motion.h' with no data and has two signatures
there is a concrete class that has to implement 'motion.h' and its own 'concreteclass.h' interface
and the main driver file that controls the logic and interface.

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

so far the code is like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

#ifndef MOTION_H_INCLUDED
#define MOTION_H_INCLUDED

using namespace std;

class Motion
{
    public:

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


    private:

};

#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
#include <set>
#include <iostream>
#include "Motion.h"

using namespace std;

class FlyMotion : public Motion
{
    public:
    static FlyMotion* getInstance();
    ~FlyMotion(){

        instanceFlag = false;
    }

   bool contains(const char *)const;

    const char * toString()const;

    void method(const char *fly[]);

   

    private:
    set<const char *> actors;

    static bool instanceFlag;
    static FlyMotion *single;
    FlyMotion()
    {

    }


};


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
#include "FlyMotion.h"
#include <iostream>



using namespace std;

bool FlyMotion::instanceFlag = false;
FlyMotion* FlyMotion::single = NULL;
FlyMotion* FlyMotion::getInstance()
{
    if(! instanceFlag)
    {
        single = new FlyMotion();
        instanceFlag = true;
        return single;
    }
    else
    {
        return single;
    }
}
bool FlyMotion::contains(const char *act)const
{
    return actors.count(act);
}
const char * FlyMotion::toString()const
{

    cout << "FLY";
}

void FlyMotion::method(const char *fly[])
{
    actors.insert(fly,fly + (sizeof(*fly)/sizeof(char)));
}



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
#include <iostream>
#include "FlyMotion.h"
#include "Motion.h"
#include <time.h>
#include <stdlib.h>

using namespace std;

int main()
{
    srand (time(NULL));
    const char *fly[] = {"Goose","Plane","Pigeon","Time"};
    const char *motn[] = {"Fly","Crawl","Swim","Run","Roll","Walk"};
    const char *all[] = {"Goose","Plane","Pigeon","Time","Lizard","Turtle","Shark","Tiger","Stone","Ball","Boat","Nose","Snail","Engine"};

    int num = rand() % 13 + 0;
    const char *strall = all[num];
    int numm = rand() % 5 + 0;
    const char *strmotn = motn[numm];

    FlyMotion *f = FlyMotion::getInstance();

    f->method(fly);
    
    return 0;
}



ok so as u can see...we're using singleton class instances...and set...
the program's requirements are that
User is asked a random question ..such as Crawl Lizard Crawl
if user replies Y n it is the correct answer..we are supposed to print every other motion that 'lizard' can do such as
- I Crawl -I run
6 classes are to be used...to keep it simple i've only posted one..FlyMotion...
I'm not sure what your exact problem is, but I do see one major problem with your code:

1
2
3
4
void FlyMotion::method(const char *fly[])
{
    actors.insert(fly,fly + (sizeof(*fly)/sizeof(char)));
}


This is not going to work at all. You cannot determine the size of a dynamic array in this way.

Also,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool FlyMotion::instanceFlag = false;
FlyMotion* FlyMotion::single = NULL;
FlyMotion* FlyMotion::getInstance()
{
    if(! instanceFlag)
    {
        single = new FlyMotion();
        instanceFlag = true;
        return single;
    }
    else
    {
        return single;
    }
}


is more complex than it needs to be. Why can you not just check if single is NULL? If it is, allocate a new FlyMotion
and assign it to single. You don't need the instanceFlag.

Topic archived. No new replies allowed.