Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Step 1: Let\'s have some fun! In this lab, we are going to create an abstract Pi

ID: 3701457 • Letter: S

Question

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!

Create the UML class diagram to show the inheritance relationship between the Parent class and the Child classes.

Open Visio and create a new diagram using the UML Class template.

Save the diagram as Week 6—Abstract Parent Class With Pirates.

Drag a class shape to your work area.

Change the class name to Pirate.

Add the following attribute and make it protected so that the Child classes have direct access to them.

name as a string (remember to make it protected by using a hashtag/pound symbol)

Add the following methods, and make them virtual so that the Child classes can override them.

speak() that returns a string

toString() that returns a string

We have a problem, though! How does a Pirate object speak? We have to know what kind of Pirate object it is so that we can tell it how to speak. In other words, a CaptainPirate is going to speak much differently than a ZombiePirate. Because we do not know how a general Pirate object speaks, we have to make the method abstract. In Visio, we make the method abstract by italicizing it (make the whole line italics).

It is time to do some inheritance! Drag another class shape to your work area, and place it below the Pirate class.

Change the name of the new class to CaptainPirate.

Add the following attribute, and make it private because we do not expect to create Child classes off of the CaptainPirate class. Notice that we do not need to add the name attribute because we inherit it from the Parent class!

pet as a string

Add the following methods to override the parent's methods and to allow the child objects to behave differently than the parent objects.

speak() that returns a string

toString() that returns a string

Let's do some more inheritance! Drag another class shape to your work area and place it below the Pirate class and to the right of the CaptainPirate class.

Change the name of the new class to ZombiePirate.

Add the following attribute and make it private because we do not expect to create Child classes off of the ZombiePirate class. Remember that we automatically get the name attribute because we inherit it from the Parent class.

brainHunger as a short

Add the following methods to override the parent's methods and to allow the child objects to behave differently than the parent objects.

speak() that returns a string

toString() that returns a string

We have our three classes on our Visio diagram. We need to make them related!

Drag an inheritance arrow off the template and drop it on your diagram.

Drag the tail of the arrow to the Child class (CaptainPirate) and the head of the arrow to the Parent class (Pirate). This inheritance arrow states that the CaptainPirate inherits from the Pirate class.

Drag an inheritance arrow from the template, and connect the ZombiePirate class to the Pirate class. Remember that the arrowhead should point to the Parent class, which is the Pirate class.

Step 2 Create a C++ project, and call it Week 6—Abstract 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 Captain Pirate 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 Zombie Pirate 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 : 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;

Explanation / Answer

here is your files : ----------->>>>>>>

Pirate.h : --------->>>>>>>>

#ifndef _PIRATE_H
#define _PIRATE_H
#include<iostream>

using namespace std;

class Pirate{
protected:
  string name;
  
public:
  Pirate();
  Pirate(string);
  virtual string speak() = 0;
  string getName()const;
  string toString();
};

#endif

Pirate.cpp : -------->>>>>>>

#ifndef _PIRATE_H
#include "Pirate.h"
#endif

string Pirate::toString(){
return "Name : "+name;
}

Pirate::Pirate(){
name = "";
}

Pirate::Pirate(string n){
name = n;
}

string Pirate::getName()const{
return name;
}

CaptainPirate.h : -------->>>>>>

#ifndef _PIRATE_H
#include "Pirate.cpp"
#endif
#ifndef _CAPTAIN_PIRATE_H
#define _CAPTAIN_PIRATE_H

class CaptainPirate:public Pirate{
string pet;

public:
  CaptainPirate(string name,string pet):Pirate(name){
   this->pet = pet;
  }
  CaptainPirate();
  string speak();
  string getPet()const;
  string toString();
};

#endif

CaptainPirate.cpp : ----->>>>>>>>>>

#ifndef _PIRATE_H
#include "CaptainPirate.h"
#endif

CaptainPirate::CaptainPirate(){
name = "";
pet = "";
}
string CaptainPirate::getPet()const{
return pet;
}
string CaptainPirate::speak(){
return "Yaaarrr! it be a "+pet+"! Yaaarrr ScallyWags! swab that poop deck!";
}

string CaptainPirate::toString(){
return Pirate::toString()+", pet: "+pet;
}

ZombiePirate.h : ------->>>>>>>

#ifndef _PIRATE_H
#include "Pirate.cpp"
#endif
#ifndef _ZOMBIE_PIRATE_H
#define _ZOMBIE_PIRATE_H

class ZombiePirate:public Pirate{
short brainHunger;

public:
  ZombiePirate(string name,short hunger):Pirate(name){
   this->brainHunger = hunger;
  }
  ZombiePirate();
  string speak();
  short getBrainHunger()const;
  string toString();
};

#endif

ZombiePirate.cpp : --------------->>>>>>>

#ifndef _PIRATE_H
#include "ZombiePirate.h"
#endif

ZombiePirate::ZombiePirate(){
name = "";
brainHunger = 0;
}
short ZombiePirate::getBrainHunger()const{
return brainHunger;
}

string ZombiePirate::speak(){
string s1 = "";
switch(brainHunger){
  case 0:s1 = "Yum, I just ate a brain";break;
  case 1:s1 = "I'm getting a little hungry.... Are there any brain out there?";break;
  case 2:
  case 3:s1 = "I'm getting very hungry! I need a brain to eat";break;
  case 4:
  case 5:s1 = "BRAINS!!!! GIVE ME BRAINS NOW!!! I NEED TO EAT BRAINS NOW!!!";break;
  default:s1 = "Error, something went wrong";break;  
}

return s1;
}

string ZombiePirate::toString(){
return Pirate::toString()+", brainHunger : "+to_string(brainHunger);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote