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?
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.
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.
...(please do not give me a different way to do it)
Well, this is very limiting. And discouraging, too.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
#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.
Bookmarks