How do I make derived classes
Ok, I am a bit confused. How do I make a derived class from this:
Code:
#include <iostream>
class Enemy
{
public:
int enemyHealth;
int attack();
};
int Enemy::attack()
{
int hit;
srand(time(NULL));
hit = rand() % 6 + 5;
return hit;
}
I think it is something like:
Code:
class Joker : public Enemy
{
// I know that everything thats public in Enemy is in Joker automatically.
};
Also, how do i change the method, or do I have to write a new one for this class?
Re: How do I make derived classes
Quote:
Also, how do i change the method, or do I have to write a new one for this class?
method for what ?.
if you want to change the base class method (ie: member function) then make it virtual function which can be override in derived class.
Re: How do I make derived classes
I think it would be a good idea to pick up an introductory book on C++.
All your questions regarding classes, inheritance and so on will be explained in any C++ book.
Re: How do I make derived classes
Unrelated, but you don't want to call srand in your attack method. Typically, that should only be called once.
Re: How do I make derived classes
Quote:
Originally Posted by
hypheni
method for what ?.
if you want to change the base class method (ie: member function) then make it virtual function which can be override in derived class.
Ok. I figured out the first question I asked. Now I am onto the method.
I want to change:
[C++]
int Enemy::attack()
{
int hit;
srand(time(NULL));
hit = rand() % 6 + 5;
return hit;
}
[/C++]
which is the base class's method. Into this (for one of the derived classes):
[C++]
int Enemy::attack()
{
int hit;
srand(time(NULL));
hit = rand() % 9 + 3; /* The change is with the integers */
return hit;
}
[/C++]
Do I have to rewrite the method (if so, how) or what? How do I change those two numbers for the derived class, but still having them "6" and "5" for the base class?
I want to have a base enemy class, then many different enemies that do different amounts of damage as derived classes (please do not give me a different way to do it) if you are wondering what my circumstances are.
Re: How do I make derived classes
Put the word virtual in front of the function declaration in the base class definition.
Rewrite the function in the derived class however you want it. Make sure it has the same name, return type and argument list.
Move srand out of the function.
Use code tags when posting code.
Re: How do I make derived classes
Quote:
Originally Posted by
prattcmp
...(please do not give me a different way to do it)
Well, this is very limiting. And discouraging, too.
Re: How do I make derived classes
Quote:
Originally Posted by
GCDEF
Move srand out of the function.
Use code tags when posting code.
I am used to another forum that uses that tag. Where do I move srand to so that it still generates a random number every time?
Re: How do I make derived classes
Quote:
Originally Posted by
prattcmp
I am used to another forum that uses that tag. Where do I move srand to so that it still generates a random number every time?
Beginning of your program.
Re: How do I make derived classes
Quote:
Originally Posted by
GCDEF
Beginning of your program.
I moved it from here:
Code:
#include <iostream>
class Enemy
{
public:
int enemyHealth;
int attack();
};
int Enemy::attack()
{
int hit;
srand(time(NULL));
hit = rand() % 6 + 5;
return hit;
}
class Drexar : public Enemy
{
public:
int specialAttack;
};
to here:
Code:
#include <iostream>
srand(time(NULL));
class Enemy
{
public:
int enemyHealth;
int attack();
};
int Enemy::attack()
{
int hit;
hit = rand() % 6 + 5;
return hit;
}
class Drexar : public Enemy
{
public:
int specialAttack;
};
and I get this error:
Expected constructor, destructor, or type conversion before "(" token.
Re: How do I make derived classes
Quote:
Originally Posted by
prattcmp
I moved it from here:
[code removed]
and I get this error:
Expected constructor, destructor, or type conversion before "(" token.
C++ isn't python or lua, you can't just place a random function call anywhere in the file, code has to go inside of functions.
Any program that uses rand() should look like this:
Code:
int main()
{
srand(time(NULL));
...
}
srand is only ever called once, and it makes sense to do it at the beginning of your program's execution.