定义一个Tree树类有成员ages树龄int有构造函数Treeint years成员函数void Growint years对ages 加上years 成员函数void ShowAge 显示tree对象的ages的值。在main函数中创建一棵松树对象Pine树龄10岁。再生长5年后显示其树龄。写一个c++代码
#include
class Tree { private: int ages; public: Tree(int years) { ages = years; } void Grow(int years) { ages += years; } void ShowAge() { cout << "The age of the tree is " << ages << " years." << endl; } };
int main() { Tree Pine(10); Pine.Grow(5); Pine.ShowAge(); return 0; }
原文地址: https://www.cveoy.top/t/topic/cIwT 著作权归作者所有。请勿转载和采集!