Friday, 15 January 2010

(Beginner) C++ regarding inheritance -


I am trying to work out a problem in C ++ about heritage and thinking that it is correct How should I go about doing it in a manner.

I have 3 class sets: - Annie (which is the member's data: intense, int heath and int level) - Wolf - beer

the way my legacy was established Goes: The Wolf is an enemy and the bear is an enemy; In other words, both the Wolf and the Bear are the heirs to the enemy.

I want to set my program so that when I create a new enemy:

  enemy anemi;  

Then the enemy :: enemy () will randomly decide whether the enemy is a wolf or bear in the manufacturer

How would one have an attitude of this problem? I know that I have to generate a random number in the enemy :: emmy () constructor, and based on the random number result, it will turn the enemy into a bear or wolf. But I can not see how to move my head "sheep" (enemy) into a wolf or bear.

Any help would be really appreciated thanks!

You have 2 problems now:

First of all, polymorphism (see above it ) Based on context or indication, there is no value, therefore, when you make your anime (which bears or "wolf" can be "randomly"), then you can not have a value type variable in it, because the type is always the same Will not done.

  Animei My_nanyame = (bear or wolf); // this is impossible. Basically, the bear and the wolf are not the same type, and not the same size, so they can not fit within the memory of an anonymous size.  

You want something with these lines:

  anonymous> my_ennemy = new bears (); / / Or some Accingbear rear; Anonymous * my_ennemy = & amp; Some exporting; // or even std :: unique_ptr & lt; Anonymity & gt; My_ennemy (new bear ());  

On the contrary, you have to decide whether or not to make a bear or wolf out of the animation maker.

You can use a free function like this:

  Anonymous * make_ennemy () {// Some random ways to choose. Maybe with a steady seed. If (iSaire) returns new bears (); Second New Wolf () returned; }  

No comments:

Post a Comment