In: Computer Science
For this assignment you are expected to submit the following two files:
1. ????ℎ??????ℎ??????. ℎ
2. ????ℎ??????ℎ??????. ???
You are being asked to write a class for simple weather data
storage. Each instance of the class will hold data for
exactly one month (30 days). Each day’s weather will be classified
as either rainy (‘R’), cloudy (‘C’), or sunny (‘S’).
To achieve this, your class will contain a character array of
length 30. The class will provide three functions to
return the number of rainy, cloudy, and sunny days in that
month.
Name your class "MonthlyWeatherData"; separate declaration from
the implementation (i.e. Header and CPP files).
The class has the following members:
1. monthName, a private member of type string
2. arrMonth, a private member array of type char of size 30.
3. Non‐default constructor which receives two parameters, a string
and a char array. The constructor will copy the
array’s content from the parameter array to the member variable
arrMonth.
4. destructor. Leave the destructor empty.
5. getMonthName, returns the value of the variable monthName
6. numberOfRainyDays, this method returns the number of days in the
month designated with the letter R
7. numberOfSunnyDays, this method returns the number of days in the
month designated with the letter S
8. numberOfCloudyDays, this method returns the number of days in
the month designated with the letter C
9. toVector, this method returns a copy of the array arrMonth in
the form of a vector pointer. The method’s return
type is vector*
******Note:******
Use the provided test class to test your implementation. Don not
modify this code and do not submit it with
your solution. Your submission should only contain the header and
CPP files of the class.
******************
// ***
// Do not modify this file
// ***
#include <iostream>
#include "MonthlyWeatherData.h"
using namespace std;
// *** Do not modify this file *****
void printStats(const MonthlyWeatherData *monthData) {
cout << "Status for the month of: " <<
monthData->getMonthName() << endl;
cout << " Cloudy Days: " <<
monthData->numberOfCloudyDays() << endl;
cout << " Rainy Days: " <<
monthData->numberOfRainyDays() << endl;
cout << " Sunny Days: " <<
monthData->numberOfSunnyDays() << endl;
}
int main() {
// *** Do not modify this file *****
char arr0[] = { 'S', 'S', 'S', 'S', 'S', 'C', 'C',
'C', 'S', 'C', 'R', 'S',
'R', 'R', 'S',
'S', 'R', 'S', 'S', 'R', 'C', 'C', 'R', 'C', 'C',
'R', 'R', 'R',
'C', 'C', 'C', 'S', 'S', 'R', 'C', 'R' };
char arr1[] = { 'R', 'R', 'C', 'R', 'R', 'C', 'C',
'R', 'C', 'S', 'S', 'S',
'C', 'S', 'S',
'R', 'R', 'C', 'S', 'C', 'C', 'C', 'S', 'S', 'C',
'C', 'C', 'S',
'S', 'C', 'C', 'C', 'C', 'C', 'C', 'C' };
char arr2[] = { 'C', 'R', 'C', 'C', 'C', 'C', 'C',
'R', 'S', 'C', 'R', 'R',
'S', 'S', 'S',
'R', 'R', 'S', 'S', 'S', 'S', 'C', 'S', 'R', 'S',
'S', 'S', 'R',
'C', 'R', 'S', 'S', 'R', 'R', 'S', 'S' };
char arr3[] = { 'C', 'C', 'S', 'S', 'R', 'R', 'C',
'C', 'C', 'S', 'R', 'S',
'C', 'C', 'C',
'C', 'R', 'C', 'R', 'R', 'R', 'R', 'C', 'C', 'R',
'R', 'S', 'R',
'C', 'C', 'C', 'S', 'R', 'C', 'R', 'R' };
MonthlyWeatherData *one = new
MonthlyWeatherData("January", arr0);
MonthlyWeatherData *two = new
MonthlyWeatherData("February", arr1);
MonthlyWeatherData *three = new
MonthlyWeatherData("March", arr2);
MonthlyWeatherData *four = new
MonthlyWeatherData("April", arr3);
printStats(one);
printStats(two);
printStats(three);
printStats(four);
vector<char> *vec = one->toVector();
cout << "Deallocating MonthlyWeatherData instances" << endl;
delete one;
delete two;
delete three;
delete four;
cout << "Weather data for January using vector" << endl;
for (int i = 0; i < 30; ++i)
cout << (*vec)[i] << "
";
cout << endl;
delete vec;
return 0;
}
Both required files code are attached below along with the output.
1.MonthlyWeatherData.h
#include<iostream>
#include<vector>
using namespace std;
class MonthlyWeatherData {
string monthName;
char arrMonth[30];
public :
MonthlyWeatherData(string m,char *arr){
monthName=m;
for(int i=0; i<30;i++) {
arrMonth[i]=arr[i];
}
}
~MonthlyWeatherData(){
}
string getMonthName ();
int numberOfCloudyDays ();
int numberOfRainyDays ();
int numberOfSunnyDays ();
vector<char> *toVector ();
};
2.MonthlyWeatherData.cpp
// ***
// Do not modify this file
// ***
#include <iostream>
using namespace std;
#include "MonthlyWeatherData.h"
// *** Do not modify this file *****
string MonthlyWeatherData ::getMonthName(){
return monthName;
}
int MonthlyWeatherData ::numberOfCloudyDays() {
int ans=0;
for(int i=0; i< 30; i++) {
if(arrMonth[i]=='C') {
ans++;
}
}
return ans;
}
int MonthlyWeatherData ::numberOfRainyDays() {
int ans=0;
for(int i=0; i< 30; i++) {
if(arrMonth[i]=='R') {
ans++;
}
}
return ans;
}
int MonthlyWeatherData ::numberOfSunnyDays() {
int ans=0;
for(int i=0; i< 30; i++) {
if(arrMonth[i]=='S') {
ans++;
}
}
return ans;
}
vector<char>* MonthlyWeatherData ::toVector(){
vector<char>* temp=new vector<char>();
for(int i=0; i< 30;i++) {
temp->push_back(arrMonth[i]);
}
return temp;
}
void printStats( MonthlyWeatherData *monthData) {
cout << "Status for the month of: " <<
monthData->getMonthName() << endl;
cout << " Cloudy Days: " <<
monthData->numberOfCloudyDays() << endl;
cout << " Rainy Days: " <<
monthData->numberOfRainyDays() << endl;
cout << " Sunny Days: " <<
monthData->numberOfSunnyDays() << endl;
}
int main() {
// *** Do not modify this file *****
char arr0[] = { 'S', 'S', 'S', 'S', 'S', 'C', 'C', 'C',
'S', 'C', 'R', 'S',
'R', 'R', 'S', 'S', 'R', 'S', 'S', 'R', 'C', 'C', 'R', 'C',
'C',
'R', 'R', 'R', 'C', 'C', 'C', 'S', 'S', 'R', 'C', 'R' };
char arr1[] = { 'R', 'R', 'C', 'R', 'R', 'C', 'C', 'R', 'C', 'S',
'S', 'S',
'C', 'S', 'S', 'R', 'R', 'C', 'S', 'C', 'C', 'C', 'S', 'S',
'C',
'C', 'C', 'S', 'S', 'C', 'C', 'C', 'C', 'C', 'C', 'C' };
char arr2[] = { 'C', 'R', 'C', 'C', 'C', 'C', 'C', 'R', 'S', 'C',
'R', 'R',
'S', 'S', 'S', 'R', 'R', 'S', 'S', 'S', 'S', 'C', 'S', 'R',
'S',
'S', 'S', 'R', 'C', 'R', 'S', 'S', 'R', 'R', 'S', 'S' };
char arr3[] = { 'C', 'C', 'S', 'S', 'R', 'R', 'C', 'C', 'C', 'S',
'R', 'S',
'C', 'C', 'C', 'C', 'R', 'C', 'R', 'R', 'R', 'R', 'C', 'C',
'R',
'R', 'S', 'R', 'C', 'C', 'C', 'S', 'R', 'C', 'R', 'R'
};
MonthlyWeatherData *one = new
MonthlyWeatherData("January", arr0);
MonthlyWeatherData *two = new MonthlyWeatherData("February",
arr1);
MonthlyWeatherData *three = new MonthlyWeatherData("March",
arr2);
MonthlyWeatherData *four = new MonthlyWeatherData("April",
arr3);
printStats(one);
printStats(two);
printStats(three);
printStats(four);
std::vector<char> *vec = one->toVector();
cout << "Deallocating MonthlyWeatherData instances" << endl;
delete one;
delete two;
delete three;
delete four;
cout << "Weather data for January using vector" << endl;
for (int i = 0; i < 30; ++i)
cout << (*vec)[i] << " ";
cout << endl;
delete vec;
return 0;
}
//output