声明一个Tree(树)类,有成员ages(树龄),成员函数grow(int years)用以对ages 加上years,showage( )用以显示tree对象的ages值。在主函数中定义Tree类对

//声明一个Tree(树)类,有成员ages(树龄),
//成员函数grow(int years)用以对ages 加上years
//,showage()用以显示tree对象的ages值。
//在主函数中定义Tree类对象,并调用成员函数(学生自行指定实参数




#include<iostream>
using namespace std;
class Tree
{
public:
int grow(int years);
void showage();
private:
int ages;
};
int Tree::grow(int years)
{
cout << "输入树的树龄:" << endl;
cin >> ages;


ages = ages + years;
return ages;


}
void Tree::showage()
{
cout << "该树的年龄是:" << ages << endl;
}
int main()
{
Tree ages, years;
ages.grow(5);
ages.showage();

return 0;
}

猜你喜欢

转载自blog.csdn.net/wyongkang/article/details/80721506