题解 | #设计立方体类#
设计立方体类
https://www.nowcoder.com/practice/0f02d35dcd564f0a87865d604eccbe18
#include <iostream> using namespace std; class Cube { // 设计一个立方体类(Cube) //定义立方体类的属性 private: int length; int width; int height; //定义立方体类的方法 public: void setLength(int len){ this->length = len; //方法功能:给类的属性赋值 } void setWidth(int wid){ this->width = wid; } void setHeight(int hei){ this->height = hei; } int getLength(){ return this->length; } int getWidth(){ return this->width; } int getHeight(){ return this->height; } int getArea(){ return 2*(length*width + length*height + width*height); } int getVolume(){ return length*width*height; } }; int main() { int length, width, height; cin >> length; //输入立方体类的长宽高 cin >> width; cin >> height; Cube c; c.setLength(length); //给类的属性赋值 c.setWidth(width); c.setHeight(height); cout << c.getLength() << " " //输出类的方法的返回值 << c.getWidth() << " " << c.getHeight() << " " << c.getArea() << " " << c.getVolume() << endl; return 0; }#你的秋招进展怎么样了##我的求职思考##零基础学习C++#