Question
(please help me coding this C++. it can be in the simplest form. thank you) Scenario/Summary The purpose of this lab is to practice using
(please help me coding this C++. it can be in the simplest form. thank you)
Scenario/Summary
The purpose of this lab is to practice using an abstract parent class. Let's have some fun creating Pirate classes!
Deliverables
The deliverables for this lab are
Word document that contains the project code and screenshots.
The zipped project folder
Lab Steps
Step 1
Let's have some fun! In this lab, we are going to create an abstract Pirate class. Then, we are going to create CaptainPirate and ZombiePirate child classes!
First, here are the UML diagrams for this program.
Step 2
Create a C++ project, and call it Week 6 Lab_YourLastName. 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.
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
The toString() method returns the a string with the pirates name such as:
return Name is + name + ;
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 toString() method should be similar to the one for CaptainPirate except add information on the brainHunger setting. Note: to add the brainHunger to the string, since it is an int type, you have to convert it to a string using to_string function which is part of the
The speak() method should return a string statement based on how hungry the ZombiePirate has become (reference the brainHunger). For example, feel free to use this code or come up with your own:
stringZombiePirate::speak(void)
{
// say something based on the hunger level
switch(brainHunger)
{
case0:
return"Yum. I just ate a brain!";
break;
case1:
return"I'm getting a little hungry... Are there any brains out there?";
break;
case2:
case3:
return"I'm getting very hungry!! I need a brain to eat!!";
break;
case4:
case5:
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.
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. The loop should go through the array and call the speak() method and then the toString() methods. 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 manyand morphmeans change into.
Finally, we need to clean up our memory because we used the new keyword. Remember that the new keyword 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 4
Create a Microsoft Word document called Week 6 Lab_YourLastName. At the top, put your information, including your name, course, Week 6 Lab, and the date.
Paste the code from each file into the Word document; there should be 7 files. Then run your application and paste the screenshot (use Alt+Print Screen) into the Word document.
Submit the Word document.
Sample output (yours may look different!):
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started