Question:
Rectangle Class Drawing C++?
TheBigBossCJ
2010-04-03 03:44:24 UTC
Create 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 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). Truncation can be achieved by converting the floating point value to an integer value. 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. Use the functions perimeter() and area() to display their results. Finally, call the draw() function to display the resulting rectangle.
============
Code
#include
using namespace std;

class rectangle
{
private:
double length;
double width;
public:
void theLen(double length);
void theWid(double width);
double calculateArea ();
double calculatePerimeter();

};
void rectangle::theLen(double L)
{
length=L;
}
void rectangle::theWid(double W)
{
width=W;
}
double rectangle::calculateArea()
{
double A;
A=length*width;
return A;
}
double rectangle::calculatePerimeter()
{
double P;
P=(length+width)*2;
return P;
}

int main()
{
double length, Width;
rectangle objectX;

cout << "Enter The Length Of The Rectangle: ";
cin >> length;
objectX.theLen(length);

cout << "Enter The Width Of Rectangle: ";
cin >> Width;
objectX.theWid(Width);

cout <<"The area of the rectangle is : "<< objectX.calculateArea() << endl;
cout <<"The Perimeter is: " <
fflush(stdin);
cin.get();
return 0 ;

}
Problem is I cant figure out haw draw the rectangle nor get in truncate floating paoint value to an int???
Four answers:
cja
2010-04-03 05:20:28 UTC
You missed this part:

"The class should have data members length and width of type float, each of which defaults to 1."



For that, you need a default constructor:



    rectangle() : length(1), width(1) { }



You also skipped the part about validating the user input and limiting the dimensions of the rectangle. The problem statement tells you to prompt for input in rectangle's set_length and set_width operations. You're prompting in main, and not validating the input. I don't think the prompting and dimension limiting should be in the rectangle class. Nothing about that class requires the dimensions to be limited. If you have to do it for the assignment, though, I guess you have to. And if you do, I'd recommend adding minSide and maxSide attributes, defaulting to 0 and 20, and settable by a full constructor and set( ) operations.



You missed this part as well:

"The get() functions should be declared using the const keyword ..."



e.g. :

double rectangle::get_width() const {

    return width;

}



Your draw operation should look something like this:



void rectangle::draw() {

   size_t y = static_cast(width);

   size_t x = static_cast(length);

   char symbol = '+';



   for (size_t i = 0; i < y; i++) {

      cout << setw(x+1) << setfill(symbol) << '\n';

   }



   // Note: symbol could be an attribute, set

   // to '+' in the default constructor, settable

   // by the full constructor, and also with

   // a set( ) operation, if you want.

}
ashleigh
2016-05-01 04:38:15 UTC
If you wish to become a professional in doing symbol then Realistic Pencil Portrait Mastery from here https://tr.im/eiLeo may be the manual that you need.

The center of the Realistic Pencil Portrait Mastery eBook is 3 secrets that fully guaranteed to provide you with a opportunity to attain the amount of a specialist of the art. They're emphasizing picking up mastery of depiction of most facial characteristics; realizing actually the most small tourist attractions in most facial characteristic; and understanding how to see what is invisible to others.

The guide substance will stop you busy for weeks if you do not brain pulling celebrities.  The face functions photos offer you sense of the difference of face features.  The photos are very large and very clean – great to bring in other words.
?
2016-04-14 01:47:26 UTC
// TestCPPConsole.cpp : Defines the entry point for the console application. // #include "stdafx.h" #define MAX_LENGTH 21 #define MAX_WIDTH 79 class Rectangle { private: char m_cUseThisChar; int m_iHeight; int m_iWidth; public: Rectangle( char cUseThisChar, int iHeight, int iWidth ) { m_cUseThisChar = cUseThisChar; m_iHeight = iHeight; m_iWidth = iWidth; }; void DrawRectangle( char * szRectangle ) { char * szTemp = ( char * ) malloc( ( m_iWidth + 2 ) * sizeof( char ) ); try { szRectangle[ 0 ] = 0; memset( szTemp, 0, m_iWidth + 2 ); memset( szTemp, m_cUseThisChar, m_iWidth ); szTemp[ m_iWidth ] = '\n'; for( int iCnt = 1; iCnt <= m_iHeight; iCnt++ ) strcat( szRectangle, szTemp ); } catch(...) { strcpy( szRectangle, "Error" ); } if( szTemp != NULL ) { free( szTemp ); szTemp = NULL; } }; }; int _tmain(int argc, _TCHAR* argv[]) { char cUseThisChar; int iHeight, iWidth; printf( "Enter the character, and length and width (eg. x %d %d) ):\n", MAX_LENGTH, MAX_WIDTH ); scanf( "%c %d %d", &cUseThisChar, &iHeight, &iWidth ); if( iHeight > MAX_LENGTH ) { printf( "You have entered an illegal value for the length. It must be between 1 and %d.", MAX_LENGTH ); getch(); return 1; } if( iWidth > MAX_WIDTH ) { printf( "You have entered an illegal value for the width. It must be between 1 and %d.", MAX_WIDTH ); getch(); return 1; } Rectangle rect( cUseThisChar, iHeight, iWidth ); char * szBuffer = ( char * ) malloc( ( ( ( iWidth + 2 ) * iHeight ) + 10 ) * sizeof( char ) ); rect.DrawRectangle( szBuffer ); printf( szBuffer ); free( szBuffer ); szBuffer = NULL; getch(); return 0; } ======================================... Disclaimers: 1. Content of stdafx.h file is: //------------------------------------... #pragma once #define WIN32_LEAN_AND_MEAN #include #include #include #include #include #include //------------------------------------... 2. Error handling must be improved 3. Due to deprecation, functions like getch(), strcpy() etc will get warnings. Use other library methods if you are picky.
Joe_Young
2010-04-03 04:04:37 UTC
Learn it from Microsoft



http://search.microsoft.com/Results.aspx?q=Rectangle+Class+Drawing+C%2B%2B&mkt=en-US&FORM=QBME1&l=1&refradio=0&qsc0=0


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...