宣告一個Tree(樹)類,有成員ages(樹齡),成員函式grow(int years)用以對ages 加上years,showage( )用以顯示tree物件的ages值。在主函式中定義Tree類對
//成員函式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;
}