   Design Question
  What would be a good design in this case:1) Bird is able to fly and hop2) Not all birds are able to fly3) Not all birds are able to hopSo create an instance of each birds and add them to a single vector andhave them perform fly and/or hop if possible. If I have a Bird base classwith virtual hop() and virtual fly(), then its derived will have to implementthem as well and some of them will have do nothing functions then. How canwe get around this?   
Quote:Original post by ConcentrateWhat would be a good design in this case:1) Bird is able to fly and hop2) Not all birds are able to fly3) Not all birds are able to hopSo create an instance of each birds and add them to a single vector andhave them perform fly and/or hop if possible. If I have a Bird base classwith virtual hop() and virtual fly(), then its derived will have to implementthem as well and some of them will have do nothing functions then. How canwe get around this?You have a few general solutions of which you already thought of one.First, use composition. This is entirely doable using data-based design where you'd have internal variables like so:bool IsFlyable;bool IsHoppable;From here you either check if a bird is hoppable or flyable before attempting to make them fly or hop OR you have your Fly and Hop functions perform the check. This feature basically emulates built-in polymorphism.Second, the built-in polymorphic way you just suggested where the derived birds do nothing when you try to make them Hop or Fly.Third, have 4 vectors for each type of bird (flyable and hoppable, flyable, hoppable, neither). When you want birds that can fly to Fly you perform that action on each bird in that vector. Otherwise you just update all 4 vectors in parallel for all other uses.Each solution has pros and cons associated with it, but if you don't ever expect to have more actions than Fly and Hop then the third solution is probably going to have the fastest execution time since you only need to perform a check for Flyability or Hoppability at bird creation time. Likewise, the polymorphic way would probably be the best method if you expect to add or remove actions frequently. Hope this helps.   ;  Anything that works.That said, there's a few options that come to mind pretty quickly:1. Plain old inheritance - nice and straight forward.2. Function objects that define the behaviors - more flexible if you have a lot of behaviors, or if they aren't grouped in sane manners.3. Interpret/script commands rather than call methods directly - useful if you want a lot of different actions, or need to change things at runtime.4. Component based entities (define hopping/flying) then aggregate - if you want to keep things really separate/modular/plugable.5. Fix the requirements/look at it differently - really? some birds can't fly or hop? maybe they shouldn't be birds then.In general, tend towards the simplest solution (1 or 5). You're almost certainly going to have to refactor the thing anyways. And good design helps with maintainability and flexibility; both of which are meaningless if it doesn't work.And of course 'it depends on your requirements' applies as normal.;  I basically agree with Telastyn's response more than my own.   