CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Mar 2011
    Posts
    9

    Question 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?

  2. #2
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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 ►►

  3. #3
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    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.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  5. #5
    Join Date
    Mar 2011
    Posts
    9

    Re: How do I make derived classes

    Quote Originally Posted by hypheni View Post
    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    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.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Unhappy Re: How do I make derived classes

    Quote Originally Posted by prattcmp View Post
    ...(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...

  8. #8
    Join Date
    Mar 2011
    Posts
    9

    Re: How do I make derived classes

    Quote Originally Posted by GCDEF View Post

    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?

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How do I make derived classes

    Quote Originally Posted by prattcmp View Post
    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.

  10. #10
    Join Date
    Mar 2011
    Posts
    9

    Re: How do I make derived classes

    Quote Originally Posted by GCDEF View Post
    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.

  11. #11
    Join Date
    Aug 2008
    Posts
    902

    Re: How do I make derived classes

    Quote Originally Posted by prattcmp View Post
    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.

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
  •  





Click Here to Expand Forum to Full Width

Featured