熊猫英雄归来:C++问题~~

来源:百度文库 编辑:查人人中国名人网 时间:2024/04/30 00:51:08
#ifndef TDate1_H
#define TDate1_H
class TDate1 {
public:
TDate1(int y=1990, int m=1,int d=1);
~TDate1();
void Print();
private:
int year,month,day;
};
#endif

TDate1::TDate1(int y, int m,int d)
{ year=y; month=m; day=d;
cout<<"Constructor called."<<endl;
}

TDate1::~TDate1()
{ cout<<"Destructor called."<<endl;
}

void TDate1 :: Print()
{ cout<<year<<"."<<month<<"."<<day<<endl;
}

void main()
{ TDate1 mDay, nDay(2003,3,15),*pd;
pd=new TDate1(2004,3,8);
mDay.Print();
nDay.Print();
pd->Print();
}

运行结果:
Constructor called.
Constructor called.
Constructor called.
1990.1.1
2003.3.15
2004.3.8
Destructor called.
Destructor called.

为什么只析构两个麻烦解答一下。谢谢~~

pd=new TDate1(2004,3,8);

这个没有delete,自然没有析构

void main()
{ TDate1 mDay, nDay(2003,3,15),*pd;
pd=new TDate1(2004,3,8);
mDay.Print();
nDay.Print();
pd->Print();
delete pd;
}