关于C++的问题,高手进!

来源:百度知道 编辑:UC知道 时间:2024/06/19 17:11:50
1、声明并实现一个矩形类,有长宽两个属性,用成员函数计算矩形的面积
2、声明一个tree(树)类,有成员ages(树龄),成员函数grow(int years)对ages加上years,age()显示tree对象的ages的值

class rectangle
{
private:
float width;
float height;
public:
rectangle(float w,float h):width(w),height(h){}//构造函数
float area(){return width*height;}
};

2、、、、、、、、、、、、、、、、、、、
class tree
{
private :
int ages;
public:
int grow(int years){return ages+years;}
int age(){return ages;}
};

class rectangle
{
private:
float width;
float height;
public:
float area(){return width*height;}
};

class tree
{
private :
int ages;
public:
int grow(int years){return ages+years;}
int get_ages(){return ages;}
};
//////////////////////////
晕。忘构造函数了。见烟花飘飘

1、
class rectangle
{
private:
float width; //宽
float height;//长
public:
rectangle(float w,float h):width(w),height(h){}//构造函数
float area(){return width*height;} //计算面积的成员函数
}; <