数据结构1 C++类的编写的注意事项

int main(){
Line line(10.0);
cout<<" "<<line.getLength()<<endl;
line.setLength(6.0);
cout<<" "<<line.getLength()<<endl;
return 0;
};
注意主函数中需先声明line,类型为Line
getLength setLength前一定要加line.!!!

class Line
{
public:
//成员函数的声明
void setLength(double len);
double getLength(void);
//含参的构造函数的声明
Line(double len);
private:
double length;
};
定义类的最后分号不能忘
Line::Line(double len) {
cout << "object is being created,length=" << len << endl;
length = len;
}
void Line::setLength(double len){
length=len;
}
double Line::getLength(void){
return length;
}

Line::不能忘
 

猜你喜欢

转载自www.cnblogs.com/wwqdata/p/11483426.html