-
April 6th, 2011, 06:13 AM
#1
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?
-
April 6th, 2011, 06:40 AM
#2
Re: How do I make derived classes
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.
◄◄ hypheni ►►
-
April 6th, 2011, 02:49 PM
#3
Re: How do I make derived classes
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.
-
April 6th, 2011, 02:53 PM
#4
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.
-
April 6th, 2011, 03:34 PM
#5
Re: How do I make derived classes
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?
-
April 6th, 2011, 03:36 PM
#6
Re: How do I make derived classes
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.
-
April 6th, 2011, 02:59 PM
#7
Re: How do I make derived classes
Originally Posted by prattcmp
...(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:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
April 6th, 2011, 07:15 AM
#8
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.
-
April 6th, 2011, 07:18 AM
#9
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|