Kodewick
Start learning →
Basic←→0 / 60

Plant care card

#041BasicClassesStrings4 min

The flower shop puts a little card in every pot saying how often to water it. The Plant constructor gets a name n and a number of days d. Fill in the constructor: store them in this plant's name and days. card() is already written.

Examples
plant_card("Fern", 3)→'Fern: water every 3 days'
plant_card("Cactus", 14)→'Cactus: water every 14 days'
The functionstring plant_card(string name, int days)
main.cpp
#include <string>
using namespace std;

class Plant {
public:
    string name;
    int days;

    Plant(string n, int d) {
        name = "?";
        days = 0;
        // your code here: store n and d in name and days (you can replace the two lines above)
    }

    string card() const {
        return name + ": water every " + to_string(days) + " days";
    }
};

string plant_card(string name, int days) {
    Plant p(name, days);
    return p.card();
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.