C/C++ >> Question about C++ Classes
Posted by KaGez on 13:23:00 03-09-2002
I looked through some tutorials, but I didn't understand most of them, because they were building up on another tutorial, and I dun wanna read 100 other tuts to get 1 simple thing
Anyways, here's my Q:
What are those "virtual" things in C++ classes? And, what can/can't I do with them? just tell me everything about them you know!
Thx in advance
[addsig]
Posted by SilentStrike on 22:57:00 03-09-2002
Geeky OO definition, they let you program to an interface rather than an implementation.
Robert's counter, they make your program slower.

Let's say your programming a game. You make an AbstractPlayer interface for anything that can compete in the game.

Code:
class AbstractPlayer {
public:
virtual Action getInput()=0; // the AbstractPlayer class know how to do this, but subclasses will
void takeDamage();
void getPowerup(PowerUp* p);
protected:
int health;
list<PowerUp*> powerUpList;
};

// AbstractPlayer::takeDamage() and AbstractPlayer::getPowerup() implemented, but NOT AbstractPlayer::getInput(). Note that we cannot actually create AbstractPlayers


This class can provide some functionality, namely handling powerups and taking damage, but it still doesn't actually know how to play the game. There are going to be two different kinds of players, a ComputerPlayer (performs getInput() with artificial intelligence), and a HumanPlayer(performs getInput() by checking the keyboard for input).

Code:
class ComputerPlayer: public AbstractPlayer {
virtual Action getInput();
};

Action ComputerPlayer::getInput() {
// do some wonderful AI crap and return your action
}

class HumanPlayer : public AbstractPlayer {
virtual Action getInput();
};

// implement HumanPlayer::getInput()


Ok, now you have your interface (AbstractPlayer), and two subclasses, HumanPlayer, and ComputerPlayer. Now you actually want to make something that drives the game.

Code:
class GameRunner {
public:
void runGame();
void addCompPlayer();
void addHumanPlayer();
private:
list<AbstractPlayer*> playerList;
};

void GameRunner::addCompPlayer() {
playerList.push_back(new ComputerPlayer);
}

void GameRunner::addHumanPlayer() {
playerList.push_back(new HumanPlayer);
}

void GameRunner::runGame() {
// do stuff
for (list<AbstractPlayer*>::iterator i = playerList.begin(); i != playerList.end(); i++) {Action a = (*i)->getInput();
switch (a) {
case FIRED_WEAPON:
// handle it
break;
case MOVE_RIGHT:
// handle it
break;
// more cases
};
}
}

notice how we don't care what type of player the currentPlayer is.. we just care that we can get input from it. It would be possible to not change this function at all, and add network play (theoritically, at least, in practice, your gonna have to do some other stuff like syncronization) by adding a NetworkPlayer subclass of AbstractPlayer and an addNetworkPlayer option to the GameRunner (trivial), but the runGame function can stay the same.


In the practical sense, they are like function pointers in C, as they decide which function is called at runtime. Evertying you can do with virtual funcs in C++, you could do with function pointers in C, but its a hell of a lot prettier in C++.

[ This Message was edited by: SilentStrike on 2002-03-09 22:58 ]
Posted by robost86 on 14:20:00 03-10-2002


He's right... my opinion is this is way too complicated, big and slow. Might be useful in some cases though, but my opinion is that C is high-level enough for most tasks
Posted by KaGez on 08:57:00 03-11-2002
SilentStrike:
Thx a lot, it helped me very much

robert:
Why the hell should I struggle around with all those low-level things, if it is much easier with a high-level lang? Speed is no argument anymore these days.
[addsig]
Posted by fsvara on 11:05:00 03-11-2002
but kde is soo sloow, it lollipops!
Posted by SilentStrike on 03:10:00 03-12-2002
Speed is good.. and there is still a need for low level code... but it doesn't belong everywhere IMO.
Posted by KaGez on 05:35:00 03-12-2002
fsvara:
I was talking about the speed diff between asm C/C++ .... you always have to compare it with something that doesn't belong to the topic ...
[addsig]
Posted by robost86 on 06:35:00 03-12-2002
SilentStrike: Of course low-level code shouldn't be used everywhere. Even I know that
Posted by KaGez on 09:23:00 03-12-2002
I just meant that the speed boost you get when coding in asm isn't worth that much of extra work. I mean, it'd only do _maximum_ 1/2 FPS in a 3D game, but probably the 10x of code you have to write.
[addsig]
Posted by fsvara on 13:13:00 03-12-2002
uh, put nicely, i just wanted to point out how arguments can be a "two edged sword"...

take it anyway you like...
Posted by Yjo on 18:29:00 04-06-2002
A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.