C++構造函數與析構函數的使用方法
析構函數(destructor) 與構造函數相反,當對象脫離其作用域時(shí)(例如對象所在的函數已調用完畢),系統自動(dòng)執行析構函數。
#include
class animal
{
public:
animal()
{
cout<<"hello"<
~animal()
{
cout<<"析構函數"<
void animal1();
};
void animal::animal1 () //構造函數
{
int box[3],i,sum=0; //sun記得賦初值
cout<<"請輸入三個(gè)數"<
{
cin>>box[i];
sum=box[i]+sum;
}
cout<
int main()
{
animal sh;
sh.animal1 ();
return 0;
}
評論