Am i doing virtual functions right?

Pages: 12
to be honest i would rather just focus on classes now. so are there any good examples of how classes should look?
Is this a good example?

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
class Character
{
    public:
        Character(){}
};

class Enemy
{
    private:
        int health;
        string name;

    public:
        Enemy(int HLTH, string NME);
        virtual void Attack(Character& Target);
};

class Bat : public Enemy
{
    public:
        Bat(int HLTH, string NME):
            Enemy(HLTH, NME){}
};

class Skeleton : public Enemy
{
    private:
        int NumOfArrows;

    public:
        Skeleton(int HLTH, string NME):
        Enemy(HLTH, NME){}
        void Attack(Character& Target);

};

Enemy::Enemy(int HLTH, string NME)
{
    health = HLTH;
    name = NME;
}

void Skeleton::Attack(Character& Target)
{
    cout << "Skeletor Shot you" << endl;
}

void Enemy::Attack(Character& Target)
{
    cout << "Base Class attack" << endl;
}
to be honest i would rather just focus on classes now. so are there any good examples of how classes should look?

I'd need to shop around before I could point you at something that illustrates the principles clearly. It might be worth posting a thread just about this? (Asking for a pointer to a well-designed, open source, OO game which isn't that complicated.)

But I did (just) find this:

OGRE – Open Source 3D Graphics Engine
http://www.ogre3d.org/

You could download the source and have a look at it:
http://www.ogre3d.org/download/source

It's rather involved, but you could look at some of the samples and follow though to the class they use. Plenty of documentation is provided, too.

The same search also found this:

An Introduction to Object-Oriented Programming for C++ Game Developers
http://www.peachpit.com/articles/article.aspx?p=482334

Note that the approach I sketched out in my earlier mail is a pretty common way of teaching "object think". Classes are not really about how they look, but how they behave.

Is this a good example?

It looks ok as a starting point, but it's not really detailed enough to be good or bad.

As the classes are extended, what will be good (or bad) will depend on what they need to do. And there are, of course, usually a number of equally valid solutions. You just need the select one approach and use it consistently.

The Character / Enemy / Bat / Skeleton hierachy does, however, raise these questions:

1. when you say Character, you mean a good character? Are enemies bad characters? Do characters represent a player. Etc.

2. do characters also have health and name, as well as enemies? If so, name and health should be moved to a common base class

3. do enemies ever fall out and fight each other? in that case, the attack method should be altered to take a suitable base class. e.g. Fighter (or Combatant?) so enemies can fight other enemies as well as characters

4. Do enemies have all their own way of attacking, or do they share things in common? If the former, Attack should be pure virtual

5. You also need (e.g.) AcceptHit (this is just for illustrative purposes; need to work out how weapons, etc.)

1
2
3
4
5
6
7
8
void Skeleton::Attack(Fighter& other)
{
    while(isFighting() && other.isFighting())
    {
        AcceptHit(other);
        other.AcceptHit(*this);
    }
}


5. etc

Andy

PS Found OGRE by googling ""well designed " oo "open source" project c++ game"...

What is the best designed open source game engine to take example? [closed]
http://gamedev.stackexchange.com/questions/5277/what-is-the-best-designed-open-source-game-engine-to-take-example

The Ogre3D rendering library is pretty close to engine code and from all accounts I've heard is very well designed and very OO.

http://www.ogre3d.org/


PPS Note that member variables should really be initialized using the constructor initializer list:

1
2
3
Enemy::Enemy(int HLTH, string NME) : health(HLTH), name(NME)
{
}


Not a bit deal when the variables are built-in types like int, double. But when they're classes, your using the default contructors plus operator= if you use the constructor body approach; but only the initializing constructor if you use the init list. (And it's best to be consistent...)
Last edited on
Ok i found one from Ogre:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class DemoApp : public OIS::KeyListener
{
public:
	DemoApp();
	~DemoApp();

	void startDemo();
	void setupDemoScene();
        void setShutdown(bool flag) { m_bShutdown = flag; }
	
	bool keyPressed(const OIS::KeyEvent &keyEventRef);
	bool keyReleased(const OIS::KeyEvent &keyEventRef);

private:
	void runDemo();

	Ogre::SceneNode*			m_pCubeNode;
	Ogre::Entity*				m_pCubeEntity;

	bool						m_bShutdown;
};
Last edited on
Holy crap heres an even bigger one 0.o

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
class BitArray
{
private:
/** The number of bits in this array
	*/
	unsigned long arraysize;
	/** The number of unsigned longs for storing at least arraysize bits
	*/
	unsigned long bitlongs;
	/** The array of unsigned longs containing the bits
	*/
	unsigned long *bits;
public:
/** Constructors.
    */
	BitArray(unsigned long newsize);
	BitArray(const BitArray& b);
	
	BitArray(unsigned long newsize, bool setclear);
	BitArray(unsigned long newsize, unsigned long *newbits);
	
    /** Destructor.
    */
	~BitArray();
	
	BitArray& operator =(const BitArray& b);
	BitArray operator ~(void);
	BitArray& operator ^=(const BitArray& b);
	BitArray& operator &=(const BitArray& b);
	BitArray& operator |=(const BitArray& b);
	BitArray operator ^(const BitArray& b);
	BitArray operator &(const BitArray& b);
	BitArray operator |(const BitArray& b);
	
	/** Test to see if a single bit is set.
	*/
	inline bool bitSet(unsigned long index) const
	{
		return bits[(index>>5)] >> (index & 0x0000001f) & 0x00000001;
	}
	/** Clear all bits in this array.
	*/
	inline void clear(void)
	{
		fillBitArray(0x00000000);
	}
	/** Clear a single bit.
	*/
	inline void clearBit(unsigned long index)
	{
		bits[index >> 5] &= ~(0x00000001 << (index & 0x0000001f));
	}
	/** fill with a 32-bit pattern.
	*/
	inline void fillBitArray(unsigned long pattern)
	{
		for (unsigned long i=0; i < bitlongs; bits[i++]=pattern); 
	}

	/** flip a single bit.
	*/
	inline void flipBit(unsigned long index)
	{
		if (bitSet(index))
			clearBit(index);
		else
			setBit(index);
	};

	/** Returns index of next set bit in array (wraps around)
    */
	inline long getNextSet(unsigned long index)
	{
		unsigned long i;
		for (i=index+1;i<arraysize;i++) if (bitSet(i)) return i;
		for (i=0;i<index-1;i++) if (bitSet(i)) return i;
		return -1;
	}
	
	/** Returns index of previous set bit in array (wraps around)
    */
	inline long getPreviousSet(unsigned long index)
	{
		unsigned long i;
		if (index != 0)
		{
			for (i=index-1;i>0;i--) if (bitSet(i)) return i;
			if (bitSet(0)) return 0;
		}
		for (i=arraysize-1;i>index;i--) if (bitSet(i)) return i;
		return -1;
	}
	
	/** Set all bits in this array.
	*/
	inline void set(void)
	{
		fillBitArray(0xffffffff);
	}
	
	/** Set a single bit.
	*/
	inline void setBit(unsigned long index)
	{
		bits[index >> 5] |= 0x00000001 << (index & 0x0000001f);
	}
	
	/** return the number of bits in this bit array..
	*/
	inline unsigned long size(void)
	{
		return arraysize;
	}
};
bump
What are you bumping about, in particular?

Andy
Well you said to find some classes and look them over so i did. Now they have inline functions inside the class can you put any kind of functions in the class? are these classes what you were talking about?
Well you said to find some classes and look them over so i did.

Well, you asked for some good examples.

I found the Ogre project by googling for good examples of OO for a games program, as that appeared to be your interest.

I can't really comment on it in general terms as I have not studied the code myself. But if you have specific questions about what they're doing, I might be able to help.

Now they have inline functions inside the class

All class methods defined within the class body are automatically inline. They are just making that clear by using the keyword, even though it's optional.

are these classes what you were talking about?

I wasn't talking about any specific class. I was just pointing you at a project which appears to have a good reputation with regards to OO design.

Andy
Last edited on
Topic archived. No new replies allowed.
Pages: 12