Answer to Question #279601 in C++ for Shivang

Question #279601

In the farming Game, we define a class named Poultry. A poultry has five attributes: name, num, weight, Maxweight and price. Their names include "Chickens", "Rabbits", and "Pigs". representing three types of animals in a farm. In Poultry, "num" means the number of a specific animal that can be sold, and "weight" means the weight of a young animal. In particular, the weight of an animal can increase if it eats food by a member function "eatFood" defined in the class. Once the weight reaches Maxweight, it will cause the animal of the specific type become mature and old enough to be sold, and its price is defined by the variable "price" and the num will also be increased by 1. It is worth mentioning that only one young animal for each type is available in the farm for feeding. For example, after the young rabbit becomes mature enough to be sold, another young rabbit will come out and begin to be fed. In a poultry, "name"-Chickens, "num"-2, "weight"=1. "Maxweight"-7, "price"-30, mean that in your farm.







1
Expert's answer
2021-12-14T14:47:40-0500
#include <iostream>

enum class Name{Chickens, Rabbits, Pigs};
class Poultry{
    Name m_name;
    unsigned m_num = 0;
    unsigned m_weight = 0;
    unsigned const m_maxweight;
    unsigned m_price;
public:
    Poultry(const Name& name, const unsigned& maxweight, const unsigned& price):
    m_name(name), m_maxweight(maxweight), m_price(price){}
    void eatFood()
    {
        ++m_weight;
        if(m_weight >= m_maxweight){
            m_weight = 0;
            ++m_num;
        }
    }
    void eatFood(unsigned n)
    {
        m_weight+=n;
        if(m_weight >= m_maxweight){
            m_num += m_weight / m_maxweight;
            m_weight %= m_maxweight;
        }
    }
    [[nodiscard]] unsigned getPrice() const{return m_price;}
    [[nodiscard]] unsigned getNum() const{return m_num;}
    [[nodiscard]] unsigned getCost() const{return m_num*m_price;}
};

int main()
{
    Poultry chickens(Name::Chickens, 5, 30);
    chickens.eatFood(31);
    std::cout<<chickens.getPrice()<<" * "<<chickens.getNum()<<" = "<<chickens.getCost();
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog