Problem Description
定义一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years, age()显示Tree对象的ages的值。
下面的程序不完整,请编程完善:
#include <iostream>
using namespace std
;
class Tree{
};
int main()
{
Tree t
;
t
.setages(3);
t
.age();
t
.grow(20);
t
.age();
return 0;
}
Sample Output
3
23
解题代码
#include <iostream>
using namespace std
;
class Tree{
int ages
;
public:
void setages(int age
){this->ages
= age
;}
void age(){cout
<< this->ages
<< endl
;};
void grow(int years
){this->ages
+= years
;}
};
int main()
{
Tree t
;
t
.setages(3);
t
.age();
t
.grow(20);
t
.age();
return 0;
}
转载请注明原文地址: https://lol.8miu.com/read-7094.html