编写矩形类.cpp 808 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4. class Rectangle
  5. {
  6. public:
  7. Rectangle();
  8. Rectangle(float length, float width);
  9. ~Rectangle();
  10. float getArea();
  11. float getPerimeter();
  12. private:
  13. float mLength;
  14. float mWidth;
  15. };
  16. Rectangle::Rectangle() : mLength(0), mWidth(0)
  17. {
  18. }
  19. Rectangle::Rectangle(float length, float width) : mLength(length), mWidth(width)
  20. {
  21. }
  22. Rectangle::~Rectangle()
  23. {
  24. }
  25. float Rectangle::getArea()
  26. {
  27. return mLength * mWidth;
  28. }
  29. float Rectangle::getPerimeter()
  30. {
  31. return 2 * (mLength + mWidth);
  32. }
  33. int main()
  34. {
  35. float m, n;
  36. cout << "Input the Length and Width: ";
  37. cin >> m >> n;
  38. Rectangle r1(m, n);
  39. cout << "The Area is: " << r1.getArea() << endl;
  40. cout << "The Perimeter: " << r1.getPerimeter() << endl;
  41. return 0;
  42. }