C++析构函数
程序员文章站
2024-03-26 11:19:47
...
#include <iostream>
using namespace std;
class Line
{
public:
void setLength(double len);
double getLength(void);
// 构造函数,在每次创建新的对象时执行
Line();
// 析构函数,在每次删除所创建的饿对象时执行
~Line();
private:
double length;
};
Line::Line(void)
{
cout << "object is created" << endl;
}
Line::~Line(void)
{
cout << "object is delted" << endl;
}
void Line::setLength(double len)
{
length = len;
}
double Line::getLength(void)
{
return length;
}
int main()
{
Line line;
line.setLength(6.0);
cout << "Length of line:" << line.getLength() << endl;
system("pause");
return 0;
}
上一篇: C++类和对象知识点总结(复习)
下一篇: C++类析构函数