views:

236

answers:

3
+1  Q: 

Rectangle class

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;
}
A: 

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).

Brendan Long
+5  A: 

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

James
+1 very nice answer
Dave
I fear my efforts are for naught.
James
`<sigh>` Constructors are called when you __define__ variables. A declaration doesn't cause a ctor to be called. See here: http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632
sbi
Oh, and what about initialization lists? And why aren't those getters `const`??? I'm sorry to say, but the missing `const` alone warrants a `-1` from me.
sbi
@sbi i will agree with you about const and the define / declare. Intialization lists are a little over his head at the moment.
James
@James: Having taught C++ for many years, I'm a firm believer in the do-it-right-from-the-very-start method. I have yet to see a student who easily unlearned a bad habit picked up earlier. However, since you fixed the `const` issue, I removed my down-vote nevertheless.
sbi
Assignment to `double` should be `1.0`, not 1; although compilers are smart enough to perform the conversion.
Thomas Matthews
@Thomas please excuse my lack of precision, fixed.
James
+2  A: 

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]
Prabhu Jayaraman
`-1` from me for posting a pasteable answer to a `homework` question.
sbi
@sbi : +1 to nullify your -1
Bragboy