Description :Whenever
the object of the class is created,the constructor is
called.Constructor is responsible for assigning initial values for all
its data members.It has the same name as its class.Whenever an object is
no longer needed ,it has to be deleted from the memory,then we write a
destructor for it.It takes the same name of their class preceded by a
tilde (~)
Code :
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class weather
{
int day,month;
float ltemp,htemp;
float rain_amt,snow_amt;
public:
weather();
void getdetail(int);
void putdetail();
};
weather :: weather()
{
day = 99;
ltemp = -999;
htemp = 999;
rain_amt = 0;
snow_amt = 0;
}
void getdata(int *m1,int *d1)
{
cout<<”\n ENTER THE MONTH NUMBER: “;
cin>>*m1;
cout<<”\n ENTER THE DAY NUMBER: “;
cin>>*d1;
return ;
}
void weather :: getdetail(int d)
{
day=d;
cout<<”\n HIGH TEMPERATURE: “;
cin>>htemp;
cout<<”\n LOW TEMPERATURE: “;
cin>>ltemp;
cout<<”\n AMOUNT OF RAIN: “;
cin>>rain_amt;
cout<<”\n AMOUNT OF SNOW: “;
cin >> snow_amt;
return ;
}
void weather :: putdetail()
{
cout<<”\t”<<day;
cout<<”\t “<<htemp;
cout<<”\t\t”<<ltemp;
cout<<”\t\t “<<rain_amt;
cout<<”\t\t “<<snow_amt;
return ;
}
const m=12,d=31;
main()
{
weather w[m][d];
int ch,day,month;
clrscr();
cout<<”\n\n\t\t\t\t\t\t\t\t\t\t..*..*..*..* WEATHER REPORT *..*..*..*..”;
do
{
cout<<”\n\n MENU: “;
cout<<”\n\n\t 1. INSERT “;
cout<<”\n\n\t 2. DISPLAY “;
cout<<”\n\n\t 3. EXIT “;
cout<<”\n\n Enter your choice: “;
cin>>ch ;
switch(ch)
{
case 1 :
getdata(&month,&day);
w[month][day].getdetail(day);
break;
case 2:
cout<<”\n\nENTER THE MONTH: “;
cin>>month ;
cout<<”\n\n———————————————————————”;
cout<<”\n\tDAY \t\tTEMPERATURE\t\t RAIN \t\t SNOW”;
cout<<”\n\n———————————————————————”;
cout<<”\n\t\t HIGH \t LOW”;
cout<<”\n———————————————————————”;
for(int i=1 ; i<=31 ; i++)
{
cout<< “\n” <<i;
w[month][i].putdetail();
}
break;
}
}while(ch!=3);
return 0;
}
Code :
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
class weather
{
int day,month;
float ltemp,htemp;
float rain_amt,snow_amt;
public:
weather();
void getdetail(int);
void putdetail();
};
weather :: weather()
{
day = 99;
ltemp = -999;
htemp = 999;
rain_amt = 0;
snow_amt = 0;
}
void getdata(int *m1,int *d1)
{
cout<<”\n ENTER THE MONTH NUMBER: “;
cin>>*m1;
cout<<”\n ENTER THE DAY NUMBER: “;
cin>>*d1;
return ;
}
void weather :: getdetail(int d)
{
day=d;
cout<<”\n HIGH TEMPERATURE: “;
cin>>htemp;
cout<<”\n LOW TEMPERATURE: “;
cin>>ltemp;
cout<<”\n AMOUNT OF RAIN: “;
cin>>rain_amt;
cout<<”\n AMOUNT OF SNOW: “;
cin >> snow_amt;
return ;
}
void weather :: putdetail()
{
cout<<”\t”<<day;
cout<<”\t “<<htemp;
cout<<”\t\t”<<ltemp;
cout<<”\t\t “<<rain_amt;
cout<<”\t\t “<<snow_amt;
return ;
}
const m=12,d=31;
main()
{
weather w[m][d];
int ch,day,month;
clrscr();
cout<<”\n\n\t\t\t\t\t\t\t\t\t\t..*..*..*..* WEATHER REPORT *..*..*..*..”;
do
{
cout<<”\n\n MENU: “;
cout<<”\n\n\t 1. INSERT “;
cout<<”\n\n\t 2. DISPLAY “;
cout<<”\n\n\t 3. EXIT “;
cout<<”\n\n Enter your choice: “;
cin>>ch ;
switch(ch)
{
case 1 :
getdata(&month,&day);
w[month][day].getdetail(day);
break;
case 2:
cout<<”\n\nENTER THE MONTH: “;
cin>>month ;
cout<<”\n\n———————————————————————”;
cout<<”\n\tDAY \t\tTEMPERATURE\t\t RAIN \t\t SNOW”;
cout<<”\n\n———————————————————————”;
cout<<”\n\t\t HIGH \t LOW”;
cout<<”\n———————————————————————”;
for(int i=1 ; i<=31 ; i++)
{
cout<< “\n” <<i;
w[month][i].putdetail();
}
break;
}
}while(ch!=3);
return 0;
}
No comments:
Post a Comment