Thread: inheritance question

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    36

    inheritance question

    Say you have a base enemy class, but you want to have 100 different enemies... is there an efficient method to create 100 enemies (say from a base class), and provide them all with some unique value or function without having to code in all 100 enemy classes?

    Maybe I should ask, what's a good way to do this without so much maintenance? Or would I have to create all 100 enemy classes with a unique value in my code?

  2. #2
    spaghetticode
    Guest
    Sounds like a bad design rather than a question of programming technique. Are you sure the differences require 100 different classes?

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Read them from a data file.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    Quote Originally Posted by dennis.cpp View Post
    Sounds like a bad design rather than a question of programming technique. Are you sure the differences require 100 different classes?
    yes, 100 enemies each with a unique value. So would it be more efficient if I made them read from a data file? And is there any other method for this?

    Edit: For clarity, let's say each enemy will have a different amount of health points, attack stat, or defense stat.
    Last edited by Darkroman; 09-21-2012 at 10:53 AM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Darkroman View Post
    yes, 100 enemies each with a unique value.
    You're saying Yes, but then your description to me immediately says No.

    You don't use inherritance when the values are different, you use inherritance when the behaviour is different!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    Quote Originally Posted by iMalc View Post
    You're saying Yes, but then your description to me immediately says No.

    You don't use inherritance when the values are different, you use inherritance when the behaviour is different!
    OK, awesome! So should I just call new instances of the class with the constructor giving different values to stats on program loading time instead?

  7. #7
    spaghetticode
    Guest
    Darkroman, you seem to have a strong misconception of object orientation. Try and explain in your own words what you think a class is and what you think an instance of this class is. Helping you apparently has to start there.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Darkroman View Post
    OK, awesome! So should I just call new instances of the class with the constructor giving different values to stats on program loading time instead?
    This is ok if only the values differ and not the functions as mentioned in your 1st post.If so,maybe overloading the functions or let them abstract in the base class is ok.

    But iMalc's post is critical to understand

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    Quote Originally Posted by dennis.cpp View Post
    Darkroman, you seem to have a strong misconception of object orientation. Try and explain in your own words what you think a class is and what you think an instance of this class is. Helping you apparently has to start there.
    A class is like the main category of something (polygon). An instance is an object from the main category (from polygon you can make an instance of it for square, rectangle, hexagon, etc). That's the best way I can really describe them.

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Darkroman View Post
    A class is like the main category of something (polygon). An instance is an object from the main category (from polygon you can make an instance of it for square, rectangle, hexagon, etc). That's the best way I can really describe them.
    Not really.Let's say we have the base class Polygon,and tho child classes,the Triangle and the Square.So now i create two Squares objects and three Triangle objects.Now the 1st and 2nd squares are instances of the class Square.I want you to tell me what is true for the 1sr,2nd and 3rd triangle

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Darkroman View Post
    A class is like the main category of something (polygon). An instance is an object from the main category (from polygon you can make an instance of it for square, rectangle, hexagon, etc). That's the best way I can really describe them.
    What you are describing is a "class hierarchy", not the difference between a class and an object.

    A class is a description of the attributes of a type of thing. An object is an actual instance of the thing. So a class is a type, like int, whereas an object is an actual instance of a type, like an int variable called i. Some real-world examples:

    Code:
    Class                 Object
    --------------------  ------------------------------
    Human                 You
    Tree                  A specific tree
    Concept of a square   An actual instance of a square
    Concept of a car      An actual car
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about inheritance
    By dayalsoap in forum C++ Programming
    Replies: 11
    Last Post: 10-06-2011, 10:22 AM
  2. Inheritance question
    By alexy in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2008, 03:46 PM
  3. Inheritance question.
    By antex in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2006, 09:59 AM
  4. Different question about inheritance
    By Marcos in forum C++ Programming
    Replies: 5
    Last Post: 03-02-2006, 03:52 PM
  5. Inheritance question
    By CompiledMonkey in forum C++ Programming
    Replies: 5
    Last Post: 12-19-2003, 01:20 PM