Class例程之3析构函数
时间:2010-03-07 来源:hnrainll
#include <iostream>
#include <string>
using namespace std;
class CStudent
{
public:
CStudent();
CStudent(char *name,int age);
~CStudent();
void init(char *name,int age);
void output();
private:
char m_name[20];
int m_age;
};
CStudent::CStudent()
{
strcpy(m_name,"TOM");
m_age = 0;
}
CStudent::CStudent(char *name,int age)
{
strcpy(m_name,name);
m_age = age;
}
CStudent::~CStudent()
{
cout<<"The object is deconstructing!"<<endl;
}
void CStudent::init(char *name,int age)
{
strcpy(m_name,name);
m_age = age;
}
void CStudent::output()
{
cout<< m_name <<" " <<m_age<<endl;
}
int main()
{
CStudent stu1;
CStudent *p = &stu1;
p->init("Peg",22);
(*p).output();
return 0 ;
}
明白,析构函数的特性,用法。
#include <string>
using namespace std;
class CStudent
{
public:
CStudent();
CStudent(char *name,int age);
~CStudent();
void init(char *name,int age);
void output();
private:
char m_name[20];
int m_age;
};
CStudent::CStudent()
{
strcpy(m_name,"TOM");
m_age = 0;
}
CStudent::CStudent(char *name,int age)
{
strcpy(m_name,name);
m_age = age;
}
CStudent::~CStudent()
{
cout<<"The object is deconstructing!"<<endl;
}
void CStudent::init(char *name,int age)
{
strcpy(m_name,name);
m_age = age;
}
void CStudent::output()
{
cout<< m_name <<" " <<m_age<<endl;
}
int main()
{
CStudent stu1;
CStudent *p = &stu1;
p->init("Peg",22);
(*p).output();
return 0 ;
}
明白,析构函数的特性,用法。
相关阅读 更多 +