求c++定义日期类TDate
2010-12-29
日,你肯定是在考试!太牛叉了,我就说,怎么会搜索到和试题一模一样的题目,原来是这样!哈哈,我刚刚做了...... 就这样交了
#include <iostream.h>
class TDate{
private:
int month,day,year;
public:
TDate()
{
month=1;
day=2;
year=2010;
}
TDate(int m,int d,int y)
{
month=m;
day=d;
year=y;
}
~TDate()
{
cout<<"destructing..."<<endl;
}
friend void print1(TDate &x);
void SetDate(int y,int m=1,int d=7)
{
year=y;
month=m;
day=d;
}
};
void print1(TDate &x)
{
cout<<"month="<<x.month<<" day="<<x.day<<" year="<<x.year<<endl;
}
void main()
{
int y,m,d;
TDate a(12,10,2009);
TDate b;
print1(b);
print1(a);
cout<<"请输入日期 ";
cin>>y>>m>>d;
b.SetDate(y);
print1(b);
}
class TDate {
private:
int year;
int month;
int day;
public:
TDate():year(2010),month(12),day(29) {}
TDate(int x, int y):year(x),month(y),day(29) {}
TDate(int x, int y, int z):year(x),month(y),day(z) {}
void display()
{
cout <<year<<"-"<<month<<"-"<<day<<endl;
}
int getYear()
{return year;}
int getMonth()
{return month;}
int getDay()
{return day;}
};
void main ()
{
TDate a(2011, 11);
a.display();
cout<<a.getDay()<<endl;
}
using namespace std;
class Tdate{
private:
int year;
int month;
int day;
public:
Tdate(){
year=-1;
month=-1;
day=-1;
}
Tdate(int y,int m){
year=y;
month=m;
day=-1;
}
Tdate(int y,int m,int d){
year=y;
month=m;
day=d;
}
void display(){
cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
}
int getYear(){
return year;
}
int getMonth(){
return month;
}
int getDay(){
return day;
}
};
int main(){
Tdate t(2010,12,26);
t.display();
return 0;
}