Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

So I am having trouble understanding abstract class and polymorphism for a coding lab. I get that the class needs to be abstract so others

So I am having trouble understanding abstract class and polymorphism for a coding lab. I get that the class needs to be abstract so others can call and modify the class, but Im at a loss on the process.

Create a C++ project, and call it Week 6Abstract Parent Class. Now, let's realize the UML class diagram (take the UML class diagram to code).

Create a Pirate class using a separate header file and implementation file.Review your UML class diagram for the attributes and behaviors.

The speak() method should be abstract because we do not know how a Pirate speaks until we know what kind of Pirate the object is. To make the method abstract, set the virtual method to 0 like this. virtual string speak() = 0; // pure virtual method -- abstract method

Create a CaptainPirate class using a separate header file and implementation file.

The CaptainPirate class needs to inherit from the Pirate class

The speak() method should return a statement that you would expect from a CaptainPirate like this. return "Yaaarrr! It be a " + pet + "! Yaarrr Scallywags! Swab that poop deck!";

The toString() method should return the Pirate toString() method plus a little more. For example: return Pirate::toString() + ", pet: " + pet;

Create a ZombiePirate class using a separate header and implementation file.

The ZombiePirate class needs to inherit from the Pirate class.

The speak() method should return a statement based on how hungry the ZombiePirate has become (reference the brainHunger). For example:

string ZombiePirate::speak(void) { // say something based on the hunger level switch (brainHunger) { case 0: return "Yum. I just ate a brain!"; break; case 1: return "I'm getting a little hungry... Are there any brains out there?"; break; case 2: case 3: return "I'm getting very hungry!! I need a brain to eat!!"; break; case 4: case 5: return "BRAINS!!!! GIVE ME BRAINS NOW!!! I NEED TO EAT BRAINS NOW!!!"; break; default: return "Error. Something went wrong."; break; } }

Step 3

Let's test our classes and use polymorphism. Add a Source.cpp to your project.

Create a main method for your application.

In the main method, check for memory leaks using this code.

// check for memory leaks #if defined(DEBUG) | defined(_DEBUG) _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif

Create an array of Pirate pointers like this. Pirate* pirates[3];

At each pointer reference, create a new child object (CaptainPirates and ZombiePirates). For example: pirates[0] = new CaptainPirate( "Jack", "Parrot" );

Create a for loop, and make the three pirate objects speak. Notice that the CaptainPirate objects return a different statement than the ZombiePirate objects even though they are all located in the Pirate array! How does this happen? Polymorphism! The Parent morphs (changes into) the many different Children and behaves like the Children! Remember that polymeans many and morph means change into.

Now, create a method that displays the Pirate object's information. Plus, the method should determine which child is being held and then show the specific child information. You can determine the child information by using a dynamic_cast. CaptainPirate* cp = dynamic_cast( ptrPirate ); // NULL if it does not work if( cp != NULL ) { cout << "Pet: " << cp->getPet( ) << endl; }

Finally, we need to clean up our memory because we used the new keyword. Remember that the newkeyword creates the object on the memory heap, and we are responsible for cleaning up the memory heap. Use a for loop to delete the three Pirate objects that we created.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions