肇砖oj题,仅供参考,不怕被钟sir封号三周你使劲抄
Description
定义一个Tree(树)类,有成员ages(树龄),成员函数 grow(int years) 对 ages 加上 years,age() 显示 Tree 对象的 ages 的值。
下面的程序不完整,请编程完善:
Input
#include <iostream> using namespace std; class Tree { 你写的代码放在这里 }; int main() { Tree t; t.setages( 3 ); t.age(); t.grow( 20 ); t.age(); return 0; }
Output
见输出样例
Sample Input
NULL
Sample Output
3
23
#include <iostream>
using namespace std;
class Tree {
public:
void grow(int years);
void age();
void setages(int n);
int ages;
};
void Tree::setages(int n){
ages=n;
}
void Tree::grow(int years){
ages=ages+years;
}
void Tree::age(){
cout<<ages<<endl;
}
int main() {
Tree t;
t.setages(3);
t.age();
t.grow(20);
t.age();
return 0;
}