Tutorial :Rectangle class



Question:

Hi everyone Im very new to c++ and may be in over my head on this problem im trying to solve. A good visual explanation and solution to my errors or even better a revised source code is all that I ask of. Thanks to everyone who invest there interest into my question.

Heres the problem: Design a class named rectangle to represent a rectangle. The class must contain:

  • Two double data fields named width and height that specify the width and height of the rectangle.
  • A no-arg constructor that creates a default rectangle with width 1 and height 1.
  • A constructor that creates a rectangle with the specified width and height
  • The accessor and mutator functions for all data fields
  • The function named get Area() that returns the area of this rectangle
  • A function named getPerimeter() that returns the peremter.

Draw the UML diagram for the class. Implement the class. Write a test progranm that creates two rectangle obejects. Assign width 4 and height 40 to the first object and width 3.5 and height 35.9 to the second. Display the properties of both objects and find their areas and perimeters.

Heres what I have so far:

#include <iostream>  using namespace std;    class Rectangle  {      public:          double height;    public:    double width;    Rectangle()    {        width = 4;            }      rectangle(double newArea)      double height;    height()    (        height = 40        {            {            area = height* width;            }        double getArea()    {      return Area;    }      bool isOn()    {      return on;    }        double getPerimeter()    {      return Perimeter;    }      void setPerimeter(double radius)      cout << "The area of the  Rectangle"    << rectangle1.area<<"is"<<rectangle1.getArea()<< endl;   cout<<"The area of  the Rectangle"   <<rectangle.area2.area<<"is"<<rectangle2.getArea()<<endl;         return 0;  }  


Solution:1

If you'd like to learn whats going on here, check this out

#include <iostream>   using namespace std;     class Rectangle{   private:        // In nearly all cases you want to keep your member variables private    double height;    // This allows you to be certain of how they are accessed.     double width;     // This provides a level of security to the class.    public:         // Only need one public:    // Constructors are called when you define a new variable of type Rectangle    Rectangle()       // No arguments means this constructor takes no extra input when called.    {       width = 1.0;        // Sets width and height to 1      height = 1.0;    }     Rectangle(double a_width, double a_height) {  // Constructor needs exact(case-sensitive) match of class-name      /* Set width and hieght to the arguments passed into the constructor in here.*/             }     // Accessor / Mutator methods are more easily called get/set methods.    double getHeight(){   // Get height called on rectangle class      return height;      // Returns the value of the class member "height"    }      /*Make a method to get the width here.*/      // In c++ you can return the results of equations directly    // i.e.     // int x = 2;    // return x*x;  // returns 4    // Try something simliar for getArea() and getPerimeter();      double getArea() const     {         return /*area equation goes here*/ ;    }     double getPerimeter() const    {                           return /*perimeter equation goes here*/;     }   }    // You should split these displaying statements into the main function,    // Which is what you meant to do, right? ;)    int main(){      // In order to use your new class, you need to declare a variable of its type        Rectangle rectangle1;             // When you declare a new variable without any arguments its default constructor is called.        // This means that the rectangle class decides to use the height = 1; width = 1; constructor from above.        // You can check this using your getHeight() and getWidth(), when you implement them.          cout << "The area of the Rectangle is" << rectangle1.getArea() << endl;       return 0;   }   

Ah reminds me of TAing CSCI1100


Solution:2

Here is a working solution,

Source Code:

#include <iostream>    using namespace std;    class Rectangle {      private:          double width;          double height;      public:          Rectangle(double w=1, double h=1);            double getWidth();          void setWidth(double w);            double getHeight();          void setHeight(double h);            double getArea();          double getPerimeter();  };    Rectangle::Rectangle(double w, double h):width(w),height(h) {}    double Rectangle::getWidth() { return width; }    void Rectangle::setWidth(double w) {width = w; }    double Rectangle::getHeight() { return height; }    void Rectangle::setHeight(double h) { height = h; }    double Rectangle::getArea() { return width*height; }    double Rectangle::getPerimeter() { return 2*(width+height); }    int main()  {      Rectangle r1(4,40);      Rectangle r2(3.5,35.9);        cout << "Rectangle #1 :: width[" << r1.getWidth() << "] height[" << r1.getHeight() << "] area[" << r1.getArea() << "] perimeter[" << r1.getPerimeter() << "]" << endl;      cout << "Rectangle #2 :: width[" << r2.getWidth() << "] height[" << r2.getHeight() << "] area[" << r2.getArea() << "] perimeter[" << r2.getPerimeter() << "]" << endl;  }  

Output:

Rectangle #1 :: width[4] height[40] area[160] perimeter[88]  Rectangle #2 :: width[3.5] height[35.9] area[125.65] perimeter[78.8]  


Solution:3

Step 1: Remove all code that isn't in the spec:

class Rectangle {    private:      double height;      double width;    public:      Rectangle() {          // fill in code here      }        Rectangle(double width, double height) {          // fill in code here      }        double getArea() {          // fill in code here      }        double getPerimeter() {           // fill in code here      }        double getWidth() {          // fill in code here      }        void setWidth(double newWidth) {          // fill in code here      }        double getHeight() {          // fill in code here      }        void setHeight(double newHeight) {          // fill in code here      }    };  

The rest of your code is just making this more complicated. You don't need an area or perimeter variable, and a constructor accepting an area as an argument doesn't make sense (you seem to be treating your rectangle like a circle).


Solution:4

//This Is The Complete Program A Run This 10 Time on VC++ For More check   //For More Code and Information and News www.exploretomorrow.blogspot.com  #include <iostream>  using namespace std;  class Rectangle // Class Rectangle  {  public:      Rectangle(float L, float W); // Constructor      float getLength(){return length;}      void setLength(float L);      float getWidth(){return width;}      void setWidth(float W);      double perimeter(void){return (length*2 + width*2);} // Perimeter function      double area(void) {return (length*width);} // Area funcion    private:      float L, W, length, width;          }; // End of class Rectangle    Rectangle::Rectangle(float L, float W) // Scope function  {  length=L;  width=W;  }    void Rectangle::setWidth(float W)//I'd like to change this to a do/ while loop??  {  if ((W < 0.0) || (W > 20.0))          {            width = 1.0;       }         else           {              width = W;            }       return;        }  float getWidth()  {      return W;//Is this right?  }    void Rectangle::setLength(float L)//same here-do/while loop?  {  if ((L < 0.0) || (L > 20.0))          {            length = 1.0;       }         else           {              length = L;            }       return;    }  float getLength()  {      return L;  }    void Rectangle::get(float L, float W)  {        }  double Rectangle:perimeter(float L, float W)  {  perimeter = (2*L) + (2*W);  cout << "The perimeter of the rectangle is: " << perimeter << endl;  }    double Rectangle::area(float L, float W)  {  area = L*W;  cout << "The area of the rectangle is :  " << area << endl;  }    int main() // main() I'm sure something is wrong here.   // I understand the main function, but when I use classes I get confused...  {  rectangle MyRectangle;  cout << "Please enter the length of the rectangle: " << endl;  cin >> MyRectangle.getlength >> endl;  cout << "Please enter the width of the rectangle: " << endl;  cin >> MyRectangle.getwidth >> endl;      return 0;  } // End of main()  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »