Constructor, Destructor:

, by Prashant Gunjal


1. 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.

code :


#include<iostream.h>
#include<conio.h>

class weather
{
private:
      int day_of_month,hightemp,lowtemp,amount_rain,amount_snow;
public:
weather()
{
day_of_month=99;
hightemp=999;
lowtemp=-999;
amount_rain=amount_snow=0;
}
void overwrite();
void display();
~weather()
{
cout<<"\tDistructed......";
}
};

void weather::overwrite()
{
cout<<"\nEnter day => ";
cin>>day_of_month;
cout<<"\nEnter High temp => ";cin>>hightemp;
cout<<"\nEnter Low tmep  => ";cin>>lowtemp;
cout<<"\nEnter amount of rain => ";cin>>amount_rain;
cout<<"\nEnter amount of snow fall=> ";cin>>amount_snow;
}
void weather::display()
{
cout<<"\n"<<day_of_month<<"\t"<<hightemp<<"\t\t"<<lowtemp;
cout<<"\t\t"<<amount_rain<<"\t\t"<<amount_snow;
}

void main()
{
weather obj[5];
clrscr();
for(int i=0;i<2;i++)
{
obj[i].overwrite();
}
cout<<"Your records = \n";
cout<<"\nday\tHigh temp\tLow tmep\train fall\tsnow fall ";
for(i=0;i<2;i++)
obj[i].display();
getch();
}
/*
o/p

Enter day => 1                                                                
                                                                               
Enter High temp => 213                                                        
                                                                               
Enter Low tmep  => 342                                                        
                                                                               
Enter amount of rain => 43                                                    
                                                                               
Enter amount of snow fall=> 2                                                  
                                                                               
Enter day => 3                                                                
                                                                               
Enter High temp => 343                                                        
                                                                               
Enter Low tmep  => 32                                                          
                                                                               
Enter amount of rain => 34                                                    
                                                                               
Enter amount of snow fall=> 37                                                
Your records =

day     High temp       Low tmep        rain fall       snow fall
1       213             342             43              2
3       343             32              34              37      Distructed......
Distructed......        Distructed......        Distructed......
Distructed......
*/

0 comments: