C++面向对象入门--简单的实现立方体类

#include <iostream>

using namespace std;

class Cube {
    //属性
private:
    //长
    double length;
    //高
    double height;
    //宽
    double width;

    //行为
public:
    //设置长
    void setLength(double length) {
        this->length = length;
    }

    //获取长
    double getLength() {
        return length;
    }

    //设置高
    void setHeight(double height) {
        this->height = height;
    }

    //获取高
    double getHeight() {
        return height;
    }

    //设置宽
    void setWidth(double width) {
        this->width = width;
    }

    //获取宽
    double getWidth() {
        return width;
    }

    /**
     * 计算表面积
     * surface n.表面;外观;表层;v.浮出水面;使成平面;adj.表面的
     * surface/surfacial area 表面积
     * @return
     */
    double calculateSurfaceArea() {
        return length * (width + height) * 2 + width * height * 2;
    }

    /**
    * 计算立方体的体积
    * volume n.体积;量;音量;adj.大量的
    * @return
    */
    double calculateVolume() {
        return length * width * height;
    }

    /**
     * 比较体积是否相等
     * @param c 欲比较的立方体Cube对象
     * @return  比较结果
     */
    bool equalsVolume(Cube& c) {
        return calculateVolume() == c.calculateVolume();
    }
};

bool equalsVolume(Cube& c1,Cube& c2) {
    return c1.calculateVolume() == c2.calculateVolume();
}

int main() {
    Cube c1;
    c1.setLength(2.5);
    c1.setWidth(7.5);
    c1.setHeight(4);
    cout << "Cube c1's volume is " << c1.calculateVolume() << endl;
    cout << "Cube c1's surface area is " << c1.calculateSurfaceArea() << endl;

    Cube c2;
    c2.setLength(5);
    c2.setWidth(3);
    c2.setHeight(5);

    cout << "Is the volume of Cube c1 is equals to the volume of Cube c2 ?" << (c1.equalsVolume(c2) ? "yes" : "no") << endl;
    cout << "Is the volume of Cube c1 is equals to the volume of Cube c2 ?" << (equalsVolume(c1, c2) ? "yes" : "no") << endl;

    system("pause");
    return 0;
}

 

上一篇:图形学入门 - 变换(一)


下一篇:unity里如何隐藏物体实现延时显示