Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

This lab practices with inheritance and function overriding by having you create characters from the game Among Us. We provide a very basic gameplay in

This lab practices with inheritance and function overriding by having you create characters from the game "Among Us".

We provide a very basic gameplay in main.cc, and you do not need to edit this file (although you can edit it if you want! for example, add new colors or change the drawing!) main.cc's output isn't tested, but we do need your program to compile -- so make sure it compiles when you submit. Here's how it looks when you run the program:

When you run your code in Replit, please click on Output from the dock on the bottom left panel to launch the Output window to display and draw your Astronaut characters, if it isn't automatically opened for you.

You will create an Astronaut base class that stores the name and the color. This will be inherited by a Crewmate derived class which has some crewmate-specific functionality to do tasks and get/set whether it is alive, and also an Impostor derived class which has functionality to kill a Crewmate.

Game Overview

Knowledge of the real game is not important for implementing these classes. Here's how our version works:

Impostors and Crewmates look alike but have different goals. Both have names and colors, but while Crewmates can do tasks, Impostors try to kill Crewmates. If the Impostors kill all the Crewmates they win, but if the Crewmates complete a total number of tasks first, the Crewmates win instead.

Note that crewmates can continue to do tasks after they are not alive.

The Astronaut class

Create an Astronaut class in astronaut.h and implement the functions in astronaut.cc.

The Astronaut class should have a constructor that takes a std::string which is the astronaut's name, and a graphics::Color which is the astronaut's main color. Store these in member variables.

Astronaut needs a const GetName function which returns its name, a std::string.

The Astronaut class needs a const accessor function (getter) for the graphics::Color called GetColor.

Finally, the Astronaut class needs a const function, GetIconFilename, which returns a std::string filename of the icon to draw. Astronaut::GetIconFilename should return "astronaut.bmp".

Create Crewmate class which inherits from Astronaut

Create a Crewmate class in crewmate.h and implement its functions in crewmate.cc. The Crewmate must inherit from Astronaut.

Crewmate has two private member variables, a boolean to track whether it is alive (default true), and a integer to track the task count (default 0).

Non-default constructor: Since Crewmate inherits from Astronaut, the Crewmate constructors must initialize the base class, Astronaut.Crewmate must have a constructor which takes a std::string name and a graphics::Color and calls the base class constructor. Remember to set the two private member variables to the defaults mentioned above.

Default constructor: In addition, Crewmate must also have a default constructor that takes no arguments, which calls the base class constructor and initializes the name to "no name", and the color to black, represented by RGB (0, 0, 0), which is created by constructing a graphics::Color object like so: graphics::Color(0, 0, 0). Remember to set the two private member variables to the defaults mentioned above.

Crewmate should have a const getter, GetIsAlive, and a setter called SetIsAlive.

Crewmate should have a const getter for the task count called GetTaskCount.

It should have a function called DoTask which takes a std::string parameter, the name of the task to do. When DoTask is called, Crewmate should increment its member variable for task count and also std::cout its name followed by " is doing " and then the task name, i.e. for a Crewmate named robot and a task called "gardening":

robot is doing gardening

Crewmate also overrides two functions from Astronaut, GetColor and GetIconFilename. Note that if you write the function declarations in crewmate.h for these overridden functions, and place the implementations in crewmate.cc.

For both of these functions, if the Crewmate is alive you can return the result from the base class, i.e. Astronaut::GetColor() or Astronaut::GetIconFilename() respectively. Note: it's important not to forget the Astronaut:: prefix to avoid recursing infinitely!

But if the Crewmate is dead, you will need to return a different result. For the filename, return "ghost.bmp" when the crewmate is dead. For the color, return the original color shifted 1/2 of the way to white. Here's how you can shift the color, assuming that you have a graphics::Color color which is the original color:

graphics::Color shifted((color.Red() + 256) / 2,                        (color.Green() + 256) / 2,                        (color.Blue() + 256) / 2);

Create Impostor class which inherits from Astronaut

Create an Impostor class in impostor.h and implement its functions in impostor.cc. The Impostor must inherit from Astronaut.

Non-default constructor: Since Impostor inherits from Astronaut, the Impostor constructors must initialize the base class, Astronaut. It must have a constructor which takes a std::string name and a graphics::Color and calls the base class constructor.

Default constructor: Impostor must also have a default constructor that takes no arguments. (For this you may pass any arguments you like to the non-default constructor.)

The Impostor class has one function, a Kill function which is const and which takes a single parameter, a reference to a Crewmate, i.e. a Crewmate&. When the Kill function is called the Impostor class should print the impostor's name and who they killed, i.e. for an impostor named "pie" and a crewmate named "robot" it would std::cout:

pie killed robot

Ensure the Impostor changes Crewmate when Kill is called

Impostor::Kill function should call Crewmate::SetIsAlive with false on the crewmate parameter.image

imposter.h:

#include "astronaut.h"
#include "crewmate.h"
// These header guards are necessary to keep your class from being defined
// multiple times when it is included from multiple files.
#ifndef IMPOSTOR_H
#define IMPOSTOR_H
// ========================= YOUR CODE HERE =========================
// Define the Impostor class here, which inherits from the Astronaut
// base class. Refer to the README for instructions.
// ===================================================================
#endif // IMPOSTOR_H

impostor.cc:

#include "impostor.h"
#include
#include "crewmate.h"
// ========================= YOUR CODE HERE =========================
// This implementation file is where you should implement the member
// functions declared in the header, only if you didn't implement
// them inline in the header.
//
// Remember to specify the name of the class with :: in this format:
// MyClassName::MyFunction() {
// ...
// }
// to tell the compiler that each function belongs to the Impostor class.

crewmate.h:

#include "astronaut.h"
// These header guards are necessary to keep your class from being defined
// multiple times when it is included from multiple files.
#ifndef CREWMATE_H
#define CREWMATE_H
// ========================= YOUR CODE HERE =========================
// Define the Crewmate class here, which inherits from the Astronaut
// base class. Refer to the README for instructions.
// ===================================================================
#endif // CREWMATE_H

crewmate.cc:

#include "crewmate.h"
#include
// ========================= YOUR CODE HERE =========================
// This implementation file is where you should implement the member
// functions declared in the header, only if you didn't implement
// them inline in the header.
//
// Remember to specify the name of the class with :: in this format:
// MyClassName::MyFunction() {
// ...
// }
// to tell the compiler that each function belongs to the Crewmate class.

astronote.h:

image

astronaut.cc:

image

main.ccimageimageimageimage

How many Impostors? (1, 2 or 3): 1 Enter impostor name: pie Enter color, valid colors are red, orange, yellow, green, teal, blue, pink, or purple: red How many Crewmates? (between 1 and 9): 3 Enter crewmate name: robot Enter color, valid colors are red, orange, yellow, green, teal, blue, pink, or purple: pink Enter crewmate name: paul Enter color, valid colors are red, orange, yellow, green, teal, blue, pink, or purple: teal Enter crewmate name: derrick Enter color, valid colors are red, orange, yellow, green, teal, blue, pink, or purple: blue How many total tasks must be done by the crewmates to win? 3 Which astronaut do you want to use (or "quit" to quit)? robot What task do you want to do? eat cake robot is doing eat cake Which astronaut do you want to use (or "quit" to quit)? pie Who do you want to kill? derrick pie killed derrick Which astronaut do you want to use (or "quit" to quit)? paul What task do you want to do? grade midterms paul is doing grade midterms Which astronaut do you want to use (or "quit" to quit)? derrick What task do you want to do? explain recursion derrick is doing explain recursion Crewmates have completed 3 tasks, and win. XImage 1 tasks. derrick 1 tasks paul 1 tasks. robot pie X X

Step by Step Solution

3.42 Rating (158 Votes )

There are 3 Steps involved in it

Step: 1

Lets start by implementing the Astronaut class followed by the Crewmate and Impostor classes Heres the astronauth file cpp ifndef ASTRONAUTH define AS... 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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Fundamental Financial Accounting Concepts

Authors: Thomas Edmonds, Christopher Edmonds

9th edition

9781259296802, 9781259296758, 78025907, 1259296806, 9781259296765, 978-0078025907

More Books

Students explore these related Programming questions