Question

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 eat <insert name here> if I were you"

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

Then this is the second part after writing the code above:

Add unit tests to ensure 100% code coverage of your Food class

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

#include <fstream>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <vector>
using namespace std;

class Food
{
private :
    //Declaring instance variables
    string name;
    bool hasBeenEaten;
    bool isSpoiled;
public :  
//Setter method
void setName(string name)
{
    this->name=name;
   }
     
   //Parameterized constructor
Food(string name)
{
   this->hasBeenEaten=false;
   this->isSpoiled=false;
   this->name=name;
   }
   void eat()
   {
       if(hasBeenEaten==false && isSpoiled==false)
       {
           cout<<"you eat the "<<name<<", yum!"<<endl;
           hasBeenEaten=true;
       }
       else if(isSpoiled==true)
       {
           cout<<"I wouldn't eat "<<name<<" if I were you"<<endl;
       }
       else if(hasBeenEaten==true)
       {
           cout<<"you already ate the "<<name<<endl;
       }
   }
};

int main() {
   //Declaring variables
Food f("Noodles");
f.eat();
f.eat();
   return 0;
}

_________________________

Output:

__________________________Thank You

Add a comment
Know the answer?
Add Answer to:
IN C++ PLEASE Write a class Food that has a string attribute for name and a...
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
  • 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...

  • Assume that you have a String "name" attribute in a Java class definition.  Write a public method...

    Assume that you have a String "name" attribute in a Java class definition.  Write a public method to return this "name" attribute. public String getName() { return name;         } 1.) Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”.​ 2.) The method getValueFromFile is public and returns an int. It accepts no arguments. The method is capable of throwing an IOException and a FileNotFoundException. Write the header for this method.

  • Use your Food class, utilities code, and sample data from Lab 1 to complete the following...

    Use your Food class, utilities code, and sample data from Lab 1 to complete the following Tasks. def average_calories(foods):     """     -------------------------------------------------------     Determines the average calories in a list of foods.     foods is unchanged.     Use: avg = average_calories(foods)     -------------------------------------------------------     Parameters:         foods - a list of Food objects (list of Food)     Returns:         avg - average calories in all Food objects of foods (int)     -------------------------------------------------------     """ your code here is 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...

  • 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...

  • In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following...

    In Java, Write a class encapsulating a restaurant,which inherits from Store. A restaurant has the following additional attributes: how many people are served every year and the average price per person. code the constructor, accessors, mutators, toString and equals method of the new subclass; also code a method returning the average taxes per year. You also need to include a client class to test your code for both the parent class and the subclass. Code for Store below(Super class aka...

  • 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 •...

  • 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...

  • Language is Python 3.6 Function name (7): class writing Description: Write a class called "Burrito". A...

    Language is Python 3.6 Function name (7): class writing Description: Write a class called "Burrito". A Burrito should have the following attributes (instance variables): meat, to_go, rice, beans, extra meat (default: False), guacamole (default: False), cheese (default: False), pico (default: False), corn (default: False), Add a method called "get_cost" to the Burrito class. It should accept zero arguments (except for "self, of course) and it will return a float. Here's how the cost should be computed: - The base cost...

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