构造函数与析构函数

时间:2024-03-26 19:05:45
#include "iostream" using namespace std; class Line { public: void setLength(int len); int getLength(); Line(); ~Line(); private: int length; } ; Line :: Line() { cout << "创建了一个对象" << endl; } Line :: ~Line() { cout << "删除了一个对象" << endl; } void Line :: setLength(int len) //注意:前面需要类型 { length = len; } int Line :: getLength() { return length; } int main(void) { Line line; line.setLength(7); cout << "length is " << line.getLength() << endl; }