song 發問時間: 電腦與網際網路程式設計 · 1 0 年前

請問這個要我們寫什麼(c++)

Create a class Rectangular_Solid with attributes length, width, and height, each of which defaults to 1.0. Provide member functions that calculate the perimeter, the area , and the volume of the rectangular solid. Also, provide set and get functions for the length, width, and height attributes. The set functions should verify that length, width, and height are each floating-point numbers larger than 0.0 and less than 100.0. Write a main function to test each member function of the Rectangle object. (Note: the default values of length, width, and height can be initialized by the constructor of Rectangular_Solid.)

請問這個要我們寫什麼程式?

1 個解答

評分
  • 1 0 年前
    最佳解答

    翻譯...

    建立一個擁有length(長), width(寬), height(高)的Rectangle_Solid類別,

    這三個屬性預設值皆為1.0

    提供計算長方體周長,表面積,體積的成員函式

    並且提供設定和取得length, width, height值的成員函式

    設定屬性的函式必須檢查length, width, height是否大於0.0和小於100.0

    在main函式裡測試此類別物件的每個成員函式

    (註:length, width, height的預設值可由Rectangle_Solid的建構函式初始化)

    以下順帶附上程式碼

    #include <iostream>

    #include "Rectangle_Solid.h"

    using namespace std;

    class Rectangle_Solid {

    public:

    Rectangle_Solid(float length = 1.0, float width = 1.0, float height = 1.0);

    float perimeter();

    float area();

    float volumn();

    void setlength(float);

    void setwidth(float);

    void setheight(float);

    float getlength();

    float getwidth();

    float getheight();

    private:

    float _length, _width, _height;

    };

    Rectangle_Solid::Rectangle_Solid\

    (float length, float width, float height):_length(length), _width(width), _height(height) {

    }

    float Rectangle_Solid::perimeter() {

    return (4 * (_length + _width + _height));

    }

    float Rectangle_Solid::area() {

    return (2 * (_length * _width + _length * _height + _width * _height));

    }

    float Rectangle_Solid::volumn() {

    return (_length * _width * _height);

    }

    void Rectangle_Solid::setheight(float height) {

    if (height>0.0 && height<100.0)

    _height = height;

    else

    cerr<<"Input error!"<<endl;

    }

    void Rectangle_Solid::setwidth(float width) {

    if (width>0.0 && width<100.0)

    _width = width;

    else

    cerr<<"Input error!"<<endl;

    }

    void Rectangle_Solid::setlength(float length) {

    if (length>0.0 && length<100.0)

    _length = length;

    else

    cerr<<"Input error!"<<endl;

    }

    float Rectangle_Solid::getlength() {

    return _length;

    }

    float Rectangle_Solid::getwidth() {

    return _width;

    }

    float Rectangle_Solid::getheight() {

    return _height;

    }

    其他部分自己加吧

    main函式部分應該你自己測試就可以了吧,很簡單的

    參考資料: 自己
還有問題?馬上發問,尋求解答。