【问题描述】定义并实现长方体类,其属性有长、宽、高。分别构造2个长方体,第一个使用默认构造函数初始化,然后使用输入的长、宽、高设置长方体。第二个长方体使用输入的长、宽、高构造长方体。
【输入形式】长方体的长、宽、高
【输出形式】长方体的表面积和体积
【样例输入】
第一个长方体的长、宽、高:1 2 3
第二个长方体的长、宽、高:4 5 6
【样例输出】
第一个长方体的表面积为:22
第一个长方体的体检为:6
第二个长方体的表面积为:148
第二个长方体的体检为:120
#include <iostream>
using namespace std;
class Rectangle {
public:
Rectangle(){}
Rectangle(int length, int width, int height): m_length(length), m_width(width), m_height(height){}
int v() { return m_length* m_height* m_width; }
int s() { return (m_length * m_width + m_width * m_height + m_length * m_height) * 2; }
public:
int m_length;
int m_width;
int m_height;
};
int main()
{
int length, width, height;
cout << "first one" << endl;
cout << "length = ";
cin >> length;
cout << "width = ";
cin >> width;
cout << "height = ";
cin >> height;
Rectangle a;
a.m_height = height;
a.m_width = width;
a.m_length = length;
cout << "s = " << a.s() << endl;
cout << "v = " << a.v() << endl;
//*****************************************
cout << "second one" << endl;
cout << "length = ";
cin >> length;
cout << "width = ";
cin >> width;
cout << "height = ";
cin >> height;
Rectangle b(length, width, height);
cout << "s = " << a.s() << endl;
cout << "v = " << a.v() << endl;
system("pause");
return 0;
}