Question

Create a class named weather report that holds a daily weather report with data
members day_of_month,hightemp,lowtemp,amount_rain and amount_snow. The
constructor initializes the fields with default values: 99 for day_of_month, 999 for
hightemp,-999 for low emp and 0 for amount_rain and amount_snow. Include a
function that prompts the user and sets values for each field so that you can override the
default values. Write a program that creates a monthly report.

Hey guys I am new to C++ and I want to make the program that does this. Could you tell me how I should go about it, the skeleton of the program and a few hints as to how I should go about it?
Last edited on
Bit by bit.

Start by making the class.
1
2
3
4
5
6
7
8
9
class your_class_name
{
private:
   // private methods/variables
public:
   your_class_name(); // constructor
   ~your_class_name(); // destructor
   // public methods/variables
};
Create a weatherReport class.
Give it data members day_of_month,hightemp,lowtemp,amount_rain and amount_snow.
Give it a constructor that sets default values.
Give it a function to get values from user for the data members.
How do I make the constructor set default values? I tried doing that, but I couldnt! How do I access the variables declared in my class?
http://www.learncpp.com/cpp-tutorial/101-constructor-initialization-lists/

If you're working inside of your class, access them as normal. For outside of the class, you need setter/getter functions (also known as accessor functions).
Here is a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

class dataStore
{
  public:
  int x;
 dataStore();
};

dataStore::dataStore()
{
  x = 7; // Set x to default value
}


int main()
{
  dataStore a;
  std::cout << a.x << std::endl;
  return 0;
}


What if I make int x private in the dataStore class? Could you give me an example of the setter/getter functions please? Thanks a lot! Appreciate the help :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class SomeClass()
{
private:
   int x;
public:
   int Getx();
   void Setx(int n);
};

int SomeClass::Getx()
{
   return x;
}

void SomeClass::Setx(int n)
{
   x = n;
}
Thank you, I'll try it out :)
Topic archived. No new replies allowed.