1.
#include <iostream>
using namespace std;
class TAdd
{
public:
TAdd(int a,int b)
{
x=a;y=b;
cout<<"constructor."<<endl;
cout<<x<<","<<y<<endl;
}
~TAdd()
{
cout<<"destructor."<<endl;
cout<<x<<","<<y<<endl;
}
int add(){return x+y;}
private:
int x,y;
};
int main()
{
TAdd p1(3,4);
cout<<"x+y="<<p1.add()<<endl;
TAdd p2(30,40);
cout<<"x+y="<<p2.add()<<endl;
return 0;
}
2.
#include <iostream>
using namespace std;
class Date
{
public:
Date()
{
cout<<"Input Date:";
cin>>y>>m>>d;
}
int beLeapYear()
{
return ((y%4==0&&y%100!=0)||(y%400==0));
}
void addOneDay()
{
d++;
if((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&(d==32))
{
d=1;
m++;
if(m>12)
{
m=1;
y++;
}
}
if(beLeapYear())
{
if(m==2&&d==28)
{
d=1;
m++;
}
}
if(!beLeapYear())
{
if(m==2&&d==29)
{
d=1;
m++;
}
}
else if((m==4)||(m==6)||(m==9)||(m==11)&&(d==31))
{
m++;
d=1;
}
}
void showDate()
{
cout<<y<<"-"<<m<<"-"<<d<<endl;
}
private:
int y,m,d;
};
int main()
{
Date d;
d.showDate();
d.addOneDay();
d.showDate();
return 0;
}
2-1
#include <iostream>
#include <string>
using namespace std;
class