Question

Need help asap with basic c++ question Write a class Food that has a string attribute...

Need help asap with basic c++ question

Write a class Food that has a string attribute for name and a Boolean value for hasBeenEaten and a Boolean value for isSpoiled, default both Booleans to false

add a method spoil() which sets isSpoiled to true

Add a method eat()

If hasBeenEaten is false,

if isSpoiled is false, return a string that says "you eat the <insert name here>, yum!", then set hasBeenEaten to true

if isSpoiled is true, return a string that says "I wouldn't eat <insert name here> if I were you"

if hasBeenEaten is true, return "you already ate the <insert name here>"

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The source code of the class is given below:

class Food
{
//member variable
string name;
bool hasBeenEaten = false;
bool isSpoiled = false;
public:
  
//functio to set the value of isSpoiled
void spoil()
{
isSpoiled = true;
}
  
//funciton to return the status of food eaten or not
string eat()
{
if(hasBeenEaten == false)
{
if(isSpoiled==false)
{
hasBeenEaten = true;
return "you eat the " + name + " , yum!";
}
else
{
return "I wouldn't eat " + name + " if I were you";
}
}
else
{
return "you already ate the " + name;
}
}
};

Add a comment
Know the answer?
Add Answer to:
Need help asap with basic c++ question Write a class Food that has a string attribute...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ PLEASE Write a class Food that has a string attribute for name and a...

    IN C++ PLEASE Write a class Food that has a string attribute for name and a Boolean value for hasBeenEaten and a Boolean value for isSpoiled, default both Booleans to false add a method spoil() which sets isSpoiled to true Add a method eat() If hasBeenEaten is false, if isSpoiled is false, return a string that says "you eat the <insert name here>, yum!", then set hasBeenEaten to true if isSpoiled is true, return a string that says "I wouldn't...

  • Python Classes - Write a class to determine the price of a salad and its ingredients....

    Python Classes - Write a class to determine the price of a salad and its ingredients. Write a Salad class. Your class will have two attributes: • name, which should be a string containing the name of the person who ordered the salad. • ingredients, which should be a set of strings where each string is an ingredient in the salad. __init__() The Salad class should have an __init__() method with four parameters, in the following order: • self •...

  • Create a Python class named Phonebook with a single attribute called entries. Begin by including a...

    Create a Python class named Phonebook with a single attribute called entries. Begin by including a constructor that initializes entries to be an empty dictionary. Next add a method called add_entry that takes a string representing a person’s name and an integer representing the person’s phone number and that adds an entry to the Phonebook object’s dictionary in which the key is the name and the value is the number. For example: >>> book = Phonebook() >>> book.add_entry('Turing', 6173538919) Add...

  • Write a class StringsAndThings. The class has one instance variable of type String and a constructor...

    Write a class StringsAndThings. The class has one instance variable of type String and a constructor with a parameter to initialize the instance variable. The parameter could contain any characters, including letters, digits, spaces, special characters (+, -, $, @,...), and punctuation marks. Write methods: countNonLetters - that will count how many of the characters in the string are not letters. You can use Character's isLetter method. moreVowels - which returns true if the String parameter has more vowels than...

  • I need help asap, Python language 2) Write a class to represent a class to represent...

    I need help asap, Python language 2) Write a class to represent a class to represent a Car and its passengers. Write a constructor that takes one additional parameter a number, the total seats in the car. The constructor should save this as a datamember and also initialize an additional datamember, a list, to keep track of the passengers in the car. Write method addRider that takes an additional parameter a string of the passenger to be added to the...

  • 1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store...

    1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...

  • Please help with a source code for C++ and also need screenshot of output: Step 1:...

    Please help with a source code for C++ and also need screenshot of output: Step 1: Create a Glasses class using a separate header file and implementation file. Add the following attributes. Color (string data type) Prescription (float data type) Create a default constructor that sets default attributes. Color should be set to unknown because it is not given. Prescription should be set to 0.0 because it is not given. Create a parameterized constructor that sets the attributes to the...

  • Assume Doctor Class is Following: class Doctor { private String fullName; private String registryNumber; private String...

    Assume Doctor Class is Following: class Doctor { private String fullName; private String registryNumber; private String specialty;    public Doctor(String fullName, String registryNumber, String specialty) { this.fullName = fullName; this.registryNumber = registryNumber; this.specialty = specialty; }    public String getName() { return fullName; }    public String getRegistryNumber() { return registryNumber; }    public String getSpecialty() { return specialty; }    public void setName(String fullName) { this.fullName = fullName; }    public boolean equals(Doctor other) { if(registryNumber == other.registryNumber) return...

  • \\ this is the code i need help to get this part working the rest works...

    \\ this is the code i need help to get this part working the rest works but this and im not sure what is missing or how to make it work. this is what they asking for to do and there is two errors the ones shown in the pictures this is all the information I have from zybooks\\ Q3. (54 Points) Complete a public class to represent a Movie as described below. (a-e 4 pts each) (f-h 8 pts...

  • import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly;...

    import java.util.*; public class Movie {    private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers;    // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; }    // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers];    for(int i=0;i<castMembers.length;i++)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT