88.1k views
2 votes
Define the tomorrow class's getweather() accessor that gets data member weather and the gethumidity() accessor that gets data member humidity. Ex: if the input is sunny 83. 0, then the output is: tomorrow: sunny humidity: 83. 0%

1 Answer

5 votes

Answer:

(Note: I'm a C++ guy, so I'll assume that this is to be done in C++. If not, convert it into your respective programming language).

#include <iostream>

#include <string>

using namespace std;

class tomorrow {

private:

string weather;

double humidity;

public:

// Constructor

tomorrow(string w, double h) {

weather = w;

humidity = h;

}

// Accessors

string getweather() const {

return weather;

}

double gethumidity() const {

return humidity;

}

};

int main() {

// Example usage

tomorrow t("sunny", 83.0);

cout << "tomorrow: " << t.getweather() << " humidity: " << t.gethumidity() << "%" << endl;

return 0;

}

Step-by-step explanation:

The 'tomorrow' class has private data members 'weather' and 'humidity'. The constructor will initialize these data members with values passed as parameters.

The 'getweather()' and 'gethumidity()' accessors are defined as 'const' member functions of the class (which means they cannot modify the object's data members). These functions will return the values of the 'weather' and 'humidity' data members respectively.

In the 'main()' function, an instance of the 'tomorrow' class is created with the values "sunny" and 83.0 passed to the constructor. The 'getweather()' and 'gethumidity()' accessors are called on this object to print out the weather and humidity values (in the desired format).

User AJD
by
9.2k points