Advertisement
Guest User

Untitled

a guest
Aug 27th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. // This program implements a Rectangle class.
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. // Rectangle class declaration
  6. class Rectangle
  7. {
  8. public:
  9. double length;
  10. double width;
  11. double stories;
  12. void setLength(double);
  13. void setWidth(double);
  14. void setHeight(double);
  15. double getLength();
  16. double getWidth();
  17. double getHeight();
  18. double getArea();
  19. };
  20.  
  21. // Member function implementation section
  22.  
  23. /********************************************************************
  24. * Rectangle::setLength *
  25. * This function sets the value of the member variable length. *
  26. * If the argument passed to the function is zero or greater, it is *
  27. * copied into length. If it is negative, 1.0 is assigned to length.*
  28. ********************************************************************/
  29. void Rectangle::setLength(double len)
  30. {
  31. if (len >= 0)
  32. length = len;
  33. else
  34. { length = 1.0;
  35. cout << "Invalid length. Using a default value of 1.\n";
  36. }
  37. }
  38.  
  39. /********************************************************************
  40. * Rectangle::setWidth *
  41. * This function sets the value of the member variable width. *
  42. * If the argument passed to the function is zero or greater, it is *
  43. * copied into width. If it is negative, 1.0 is assigned to width. *
  44. ********************************************************************/
  45. void Rectangle::setWidth(double w)
  46. {
  47. if (w >= 0)
  48. width = w;
  49. else
  50. { width = 1.0;
  51. cout << "Invalid width. Using a default value of 1.\n";
  52. }
  53. }
  54.  
  55. /********************************************************************
  56. * Rectangle::setHeight *
  57. * This function sets the value of the member variable height. *
  58. * If the argument passed to the function is zero or greater, it is *
  59. * copied into width. If it is negative, 1.0 is assigned to width. *
  60. ********************************************************************/
  61. void Rectangle::setHeight(double h)
  62. {
  63. if (h >= 0)
  64. stories = h;
  65. else
  66. { stories < 1.0;
  67. cout << "Invalid height. Using a default value of 1.\n";
  68. }
  69. }
  70.  
  71. /**************************************************************
  72. * Rectangle::getLength *
  73. * This function returns the value in member variable length. *
  74. **************************************************************/
  75. double Rectangle::getLength()
  76. {
  77. return length;
  78. }
  79.  
  80. /**************************************************************
  81. * Rectangle::getWidth *
  82. * This function returns the value in member variable width. *
  83. **************************************************************/
  84. double Rectangle::getWidth()
  85. {
  86. return width;
  87. }
  88.  
  89. /**************************************************************
  90. * Rectangle::getHeigth *
  91. * This function returns the value in member variable Height. *
  92. **************************************************************/
  93. double Rectangle::getHeight()
  94. {
  95. return stories;
  96. }
  97. /*******************************************************************
  98. * Rectangle::getArea *
  99. * This function calculates and returns the area of the rectangle. *
  100. *******************************************************************/
  101. double Rectangle::getArea()
  102. {
  103. return length * width * stories;
  104. }
  105.  
  106. /*************************************************************
  107. * main *
  108. *************************************************************/
  109. int main()
  110. {
  111. Rectangle box; // Declare a Rectangle object
  112. double boxLength, boxWidth, boxHeight;
  113.  
  114. //Get box length and width
  115. cout << "This will calculate the square footage area of your house.\n";
  116. cout << "What is the length? ";
  117. cin >> boxLength;
  118. cout << "What is the width? ";
  119. cin >> boxWidth;
  120. cout << "How many stories? ";
  121. cin >> boxHeight;
  122.  
  123. // Call member functions to set box dimensions
  124. box.setLength(boxLength);
  125. box.setWidth(boxWidth);
  126. box.setHeight(boxHeight);
  127.  
  128. // Call member functions to get box information to display
  129. cout << "\nHere is your house's data:\n";
  130. cout << "Square Footage Area of your home is : " << box.getArea() << endl;
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement