Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This program implements a Rectangle class.
- #include <iostream>
- using namespace std;
- // Rectangle class declaration
- class Rectangle
- {
- public:
- double length;
- double width;
- double stories;
- void setLength(double);
- void setWidth(double);
- void setHeight(double);
- double getLength();
- double getWidth();
- double getHeight();
- double getArea();
- };
- // Member function implementation section
- /********************************************************************
- * Rectangle::setLength *
- * This function sets the value of the member variable length. *
- * If the argument passed to the function is zero or greater, it is *
- * copied into length. If it is negative, 1.0 is assigned to length.*
- ********************************************************************/
- void Rectangle::setLength(double len)
- {
- if (len >= 0)
- length = len;
- else
- { length = 1.0;
- cout << "Invalid length. Using a default value of 1.\n";
- }
- }
- /********************************************************************
- * Rectangle::setWidth *
- * This function sets the value of the member variable width. *
- * If the argument passed to the function is zero or greater, it is *
- * copied into width. If it is negative, 1.0 is assigned to width. *
- ********************************************************************/
- void Rectangle::setWidth(double w)
- {
- if (w >= 0)
- width = w;
- else
- { width = 1.0;
- cout << "Invalid width. Using a default value of 1.\n";
- }
- }
- /********************************************************************
- * Rectangle::setHeight *
- * This function sets the value of the member variable height. *
- * If the argument passed to the function is zero or greater, it is *
- * copied into width. If it is negative, 1.0 is assigned to width. *
- ********************************************************************/
- void Rectangle::setHeight(double h)
- {
- if (h >= 0)
- stories = h;
- else
- { stories < 1.0;
- cout << "Invalid height. Using a default value of 1.\n";
- }
- }
- /**************************************************************
- * Rectangle::getLength *
- * This function returns the value in member variable length. *
- **************************************************************/
- double Rectangle::getLength()
- {
- return length;
- }
- /**************************************************************
- * Rectangle::getWidth *
- * This function returns the value in member variable width. *
- **************************************************************/
- double Rectangle::getWidth()
- {
- return width;
- }
- /**************************************************************
- * Rectangle::getHeigth *
- * This function returns the value in member variable Height. *
- **************************************************************/
- double Rectangle::getHeight()
- {
- return stories;
- }
- /*******************************************************************
- * Rectangle::getArea *
- * This function calculates and returns the area of the rectangle. *
- *******************************************************************/
- double Rectangle::getArea()
- {
- return length * width * stories;
- }
- /*************************************************************
- * main *
- *************************************************************/
- int main()
- {
- Rectangle box; // Declare a Rectangle object
- double boxLength, boxWidth, boxHeight;
- //Get box length and width
- cout << "This will calculate the square footage area of your house.\n";
- cout << "What is the length? ";
- cin >> boxLength;
- cout << "What is the width? ";
- cin >> boxWidth;
- cout << "How many stories? ";
- cin >> boxHeight;
- // Call member functions to set box dimensions
- box.setLength(boxLength);
- box.setWidth(boxWidth);
- box.setHeight(boxHeight);
- // Call member functions to get box information to display
- cout << "\nHere is your house's data:\n";
- cout << "Square Footage Area of your home is : " << box.getArea() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement