rectangle class

DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: rectangle class

  1. #1
    Join Date
    Apr 2012

    rectangle class

    Got real head scratcher here, I have no idea where to begin any help would be greatly appreciated..

    Make a class rectangle. The class should have data members length and width of type float, each of which defaults to 1. The class should have member functions that calculate the perimeter() and the area() of the rectangle. It should also have separate set() and get() functions for both the length and width (set_length(), get_length(), set_width(), get_width()).

    The set_length() and set_width() functions should require the user to enter length or width data values greater than zero and less than or equal to 20.0. These functions should examine the data entered by the user, and accept the data only if it is in the specified range (0 < data <= 20.0). If the data is not in this range, the user should be reminded of the allowed data range and prompted to enter the data again. This cycle should repeat until the user enters a correct data value. This can be accomplished by using a do while loop in each set() function.

    The get() functions should be declared using the const keyword to ensure that these functions can not change private member data of the rectangle class.

    Add a draw() function to your rectangle class that draws a solid rectangle of the correct length and width. Since the length and width in are floating point numbers, truncate the length and width values for the purpose of drawing the rectangle (for example, if length = 6.3 and width = 11.5, draw a rectangle that is 6 by 11 characters in size). Use any character you like to draw the rectangle. Here is an example of a rectangle drawn with + signs:


    Write a short demonstration program that uses the rectangle class you have created. The program should create a rectangle object, then prompt the user to enter new values for the length and width. Use the set() functions and the userís entries to change the private data of the rectangle object you created. Finally, call the draw() function to display the resulting rectangle.

    I dont understand. :(

  2. #2
    Join Date
    Jan 2018
    Obviously it's over 5 years to late to ask what you don't understand. However, for info and for others reading this thread consider

    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    class rectangle {
    	rectangle() {}
    	float perimeter() const
    		return 2.0f * (length + width);
    	float area() const
    		return length * width;
    	void set_length()
    		length = get_data("Please enter length: "s);
    	float get_length() const
    		return length;
    	void set_width()
    		width = get_data("Please enter width: "s);
    	float get_width() const
    		return width;
    	void draw() const
    		const char sym = '+';
    		for (int r = 0; r < int(width); ++r)
    			cout << setw(int(length)) << setfill(sym) << sym << endl;
    	float length = 1.0;
    	float width = 1.0;
    	float get_data(const string& prmp)
    		const float max_size = 20.0;
    		float val = 0.0;
    		while ((cout << prmp && !(cin >> val)) || (val < 0.0 || val > max_size))
    			if (!cin.good()) {
    				cout << "Invalid input - not a number" << endl;
    				cin.ignore(1000, '\n');
    			} else
    				cout << "Incorrect number. needs to be >= 0 and <= " << max_size << endl;
    		return val;
    int main()
    	rectangle rect;
    	cout << "The perimeter is " << rect.perimeter() << " and the area is " << rect.area() << endl << endl;

Similar Threads

  1. Replies: 27
    Last Post: 02-11-2016, 06:29 AM
  2. please help with class not found issue
    By meomike2000 in forum Java
    Replies: 6
    Last Post: 06-13-2010, 08:02 AM
  3. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 01:15 PM
  4. Replies: 3
    Last Post: 07-21-2006, 07:16 AM
  5. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 08:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center