using namespace std;
class Box {
\x09float length, width, height;
public:
\x09Box(float l, float w, float h);
\x09float GetVolume() const;
};
Box::Box(float l = 1, float w = 1, float h = 1)
\x09: length(l), width(w), height(h) {}
float Box::GetVolume() const {
\x09return height * width * length;
}
int main()
{
\x09Box b1, b2(2, 3, 4);
\x09float v1, v2;
\x09v1 = b1.GetVolume();
\x09v2 = b2.GetVolume();
\x09if (v1>v2)
\x09\x09cout << v1 << " " << v2 << endl;
\x09else
\x09\x09cout << v2 << " " << v1 << endl;
\x09return 0;
}Box b1, b2(2, 3, 4);
这行有错哎~error C2512: 'Box' : no appropriate default constructor available
怎么改呢???
#include <iostream>
using namespace std;
class Box {
\x09float length, width, height;
public:
\x09Box();
\x09Box(float l, float w, float h);
\x09float GetVolume() const;
};
Box::Box() : length(1), width(1), height(1) {}
Box::Box(float l, float w, float h)
: length(l), width(w), height(h) {}
float Box::GetVolume() const {
\x09return height * width * length;
}
int main()
{
\x09Box b1, b2(2, 3, 4);
\x09float v1, v2;
\x09v1 = b1.GetVolume();
\x09v2 = b2.GetVolume();
\x09if (v1>v2)
\x09\x09cout << v1 << " " << v2 << endl;
\x09else
\x09\x09cout << v2 << " " << v1 << endl;
\x09return 0;
}