列表

详情


CPP38. 设计立方体类

描述

采用封装的思想设计一个立方体类(Cube),成员变量有:长(length)、宽(width)、高(height),都为 int 类型;
成员方法有:获取表面积的方法(getArea)、获取体积的方法(getVolume)。

输入描述

键盘输入立方体的长(length)、宽(width)、高(height),都为 int 类型,范围[1, 100]

输出描述

输出立方体的长(length)、宽(width)、高(height)、面积、体积,中间使用空格隔开。
例如:3 4 5 60

示例1

输入:

3
4
5

输出:

3 4 5 94 60

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-12-13

#include <iostream>
using namespace std;

class Cube {

    private:
int length;
int width;
int height;
    public:
void setLength(int l){
    length = l;
    }
void setWidth(int w){
    width = w;
}
void setHeight(int h){
    height = h;
}
int getLength(){
    return length;
}
int getWidth(){
    return width;
}
int getHeight(){
    return 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++ 解法, 执行用时: 2ms, 内存消耗: 388KB, 提交时间: 2021-12-13

#include <iostream>
using namespace std;

class Cube {

    // write your code here......
        private:
        int length;
        int width;
        int height;
    public:
        int getArea() {
            return 2 * (length * width + length * height + width * height);
        }
        int getVolume() {
            return this -> length * this -> width * this -> height;
        }
        void setLength(int length) {
            this -> length = length;
        }
        void setWidth(int width) {
            this -> width = width;
        }
        void setHeight(int height) {
            this -> height = height;
        }
        int getLength() {
            return this -> length;
        }
        int getWidth() {
            return this -> width;
        }
        int getHeight() {
             return this -> 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++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2022-07-23

#include <iostream>
using namespace std;

class Cube {

    // write your code here......
    public:
    int m_length;
    int m_width;
    int m_height;
    
    void setLength(int length)
    {
        this->m_length = length;
    }
    void setWidth(int width)
    {
        this->m_width = width;
    }
    void setHeight(int heigth)
    {
        this->m_height = heigth;
    }
    int getLength()
    {
        return m_length;
    }
    int getWidth()
    {
        return m_width;
    }
    int getHeight()
    {
        return m_height;
    }
    int getArea()
    {
        return 2 * (m_height * m_width + m_width * m_length +m_length * m_height);
    }
    int getVolume()
    {
        return m_length * m_height * m_width;
    }
};

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++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-12-19

#include <iostream>
using namespace std;

class Cube {

    // write your code here......
    private:
        int length, width, height;
    public:
        Cube(){}
        void setLength(int l){
            length=l;
        }
        void setWidth(int w){
            width=w;
        }
        void setHeight(int h){
            height=h;
        }
        int getLength(){
            return length;
        }
        int getWidth(){
            return width;
        }
        int getHeight(){
            return 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++ 解法, 执行用时: 2ms, 内存消耗: 392KB, 提交时间: 2021-12-18

#include <iostream>
using namespace std;

class Cube {
    
    // write your code here......
    int length;
    int width;
    int height;
public:
    void setLength(int length){
        this->length=length;
    }
    void setWidth(int width){
        this->width=width;
    }
    void setHeight(int height){
        this->height=height;
    }
    int getLength(){
        return length;
    }
    int getWidth(){
        return width;
    }
    int getHeight(){
        return 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;
}

上一题