2

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;
}
egrunin
  • 24,650
  • 8
  • 50
  • 93
John
  • 41
  • 1
  • 1
  • 6
  • If you add 4 spaces before your code, it gets formatted as code. I suspect you have some other problems though (look at the code starting with `rectangle(double newArea)`). Also what's with the `setPerimeter` function? Last time I checked, rectangles don't have a radius (and function definitions end with either `{ /* code goes here */ }` or `;`) – Brendan Long Jul 23 '10 at 19:04
  • 1
    You should also explain what's not working when you post a question. Is it not compiling? Is some part of it not working? Can you not think of how to do one part? – Brendan Long Jul 23 '10 at 19:24
  • This is the third problem I've seen from you today that consists of a homework assignment and some code, not well formatted, and no indication of what you're actually finding wrong. Could you verify that what's posted is what you wrote, and tell us what's actually wrong? – David Thornley Jul 23 '10 at 19:42
  • Summer session is almost up and he hasn't done anything all semester is whats wrong here. – James Jul 23 '10 at 19:48

3 Answers3

7

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
  • 2,454
  • 1
  • 22
  • 22
  • 2
    I fear my efforts are for naught. – James Jul 23 '10 at 20:21
  • 2
    `` 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 Jul 23 '10 at 20:29
  • 3
    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 Jul 23 '10 at 20:30
  • @sbi i will agree with you about const and the define / declare. Intialization lists are a little over his head at the moment. – James Jul 23 '10 at 20:46
  • 2
    @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 Jul 23 '10 at 20:55
  • Assignment to `double` should be `1.0`, not 1; although compilers are smart enough to perform the conversion. – Thomas Matthews Jul 23 '10 at 23:03
  • @Thomas please excuse my lack of precision, fixed. – James Jul 26 '10 at 17:20
4

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]
josh
  • 13,793
  • 12
  • 49
  • 58
2

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
  • 53,280
  • 21
  • 146
  • 188