Posted on 2012-09-24 10:47
盛勝 閱讀(303)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
vc++深入詳解
#include<iostream.h>
class point
{
public:
int x;
int y;
point()
{
x=0;
y=0;
}
point(int a,int b)
{
x=a;
y=b;
}
void output()
{
cout<<x<<endl<<y<<endl;
}
void input(int x,int y)
{
this->x=x;
this->y=y;
}
}
void main()
{
point pt(5,5);
pt.input(10,10);
pt.output();
}
輸出結(jié)果為10
10
如果把this指針去掉則為5
5
在使用構(gòu)造函數(shù)時(shí)注意是否需要釋放內(nèi)存。~構(gòu)造函數(shù)名()利用析構(gòu)函數(shù)解決內(nèi)存泄漏問(wèn)題。