谭浩强c++第三版课后习题------第八章(类和对象的特性)第6题

#include 

using namespace std;

class Box
{
public:
    void set_value();//设置长,宽,高的成员函数 

    double set_volume();//体积不能用void ,因为体积输出是double类型

    void display();//显示体积的成员函数

private:
    double length;
    double width;
    double height;
};
void Box::set_value()//调用设置长,宽,高的成员函数
{
    cin>>length;
    cin>>width;
    cin>>height;
}
double Box::set_volume()//调用设置体积的成员函数
{
    return length*width*height;
}
void Box::display()//调用显示体积的成员函数
{
   cout<<set_volume()<<endl;
}
int main()
{
    Box b1,b2,b3;//设置三个对象,可以显示三个长方体的体积
    cout<<"Input the first box data:"<<endl;
    b1.set_value();
    cout<<"Output the first box'volume:"<<endl;
    b1.display();
    cout<<"Input the second box data:"<<endl;
    b2.set_value();
    cout<<"Output the second box'volume:"<<endl;
    b2.display();
    cout<<"Input the third  box data:"<<endl;
    b3.set_value();
    cout<<"Output the third box'volume:"<<endl;
    b3.display();
    return 0;
}
/*Input the first box data:
1 2 3
Output the first box'volume:
6
Input the second box data:
4 5 6
Output the second box'volume:
120
Input the third  box data:
6 7 8
Output the third box'volume:
336

Process returned 0 (0x0)   execution time : 17.288 s
Press any key to continue.
*/

你可能感兴趣的:(谭浩强c++第三版课后习题------第八章(类和对象的特性)第6题)