fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class rectangle {
  5. private:
  6. float Width;
  7. float Length;
  8.  
  9. public:
  10. //Contstructor
  11. rectangle(float w=2, float l=2);
  12.  
  13. float GetWidth();
  14. void SetWidth(float w);
  15.  
  16. float GetLength();
  17. void SetLength(float l);
  18.  
  19. float Area();
  20. float Perimeter();
  21. };
  22.  
  23.  
  24. rectangle::rectangle(float w, float l):Width(w), Length (l) {}
  25.  
  26.  
  27. //width
  28. float rectangle::GetWidth()
  29. {
  30. cout << "Enter a width: ";
  31. cin >> Width;
  32. while (Width > 20.0 || Width <= 0)
  33. {
  34. cout << "Please enter a width between 0 and 20.";
  35. cin >> Width;
  36. }
  37. return Width;
  38. }
  39. void rectangle::SetWidth(float w)
  40. {
  41. Width = w;
  42. };
  43.  
  44.  
  45. //Length
  46. float rectangle::GetLength()
  47. {
  48. cout << "Enter a length: ";
  49. cin >> Length; cout << endl;
  50. while (Length > 20.0 || Length <= 0)
  51. {
  52. cout << "Please enter a length between 0 and 20." << endl;
  53. cin >> Length; cout << endl;
  54. }
  55. return Length;
  56. }
  57. void rectangle::SetLength(float l)
  58. {
  59. Length = l;
  60. };
  61.  
  62.  
  63. float rectangle::Area()
  64. {
  65. return Width * Length;
  66. };
  67. float rectangle::Perimeter()
  68. {
  69. return (Width * 2) + (Length * 2);
  70. };
  71.  
  72. int main(){
  73. rectangle one;
  74. cout << "Length: " << one.GetLength() << "\nWidth: " << one.GetWidth() << "\nArea: " << one.Area() << "\nPerimeter: " << one.Perimeter() << endl;
  75. system("pause");
  76. return 0;
  77. };
Success #stdin #stdout #stderr 0s 3304KB
stdin
Standard input is empty
stdout
Enter a width: Enter a length: 
Length: 2
Width: 2
Area: 4
Perimeter: 8
stderr
sh: pause: not found