Rectangle Class C++, please help!

my professor is asking us to make rectangle classes with C++
can someone make the following program easier to understand please? thank you!

Design a class named Rectangle to represent a rectangle. The class contains..
2 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 specified width and height
accessor and mutator functions for all data fields
a function named getArea() that returns the area of the rectangle
a function named getPerimeter() that returns the perimeter of the rectangle
Create the UML diagram with text in the Algorithm area of your submission.
Implement the class
Write a test program that creates 2 Rectangle objects.
Display the properties of both objects, and their areas and perimeters.
closed account (D4S8vCM9)
That is a simple and straight forward exercise. Where is your problem? In the naming?

EDIT: I won't solve the exercise for you, but I can help you in understanding it.
Last edited on
What do you think you should do? How do you think you should approach the problem? I can help you out (except for the UML diagram, never heard of such a thing).
Last edited on
@Helixirr:
except for the UML diagram, never heard of such a thing

"UML" = Unified Modeling Language is a widely used textual and graphical standard to describe f.e. use cases, class structures, state machines, algorithm, functional behavior etc. See http://en.wikipedia.org/wiki/Unified_Modeling_Language. Other than stated in Wikipedia its not only intended and useful - and used - by computer scientists. Any developer regardless of his/her discipline may use it (economic science, ...).
with the programming, like i pretty much understand what needs to be done. I just don't really understand how to approach it.
my teacher isn't really good at explaining the material.
so i'm stuck figuring things out by itself.

the whole programming thing is very confusing to me, cause i don't know where things go, and how to write them and such..
we went over how to do class circles and it was completely different for a rectangle. So..


pretty much words won't help me, if you can hint hint some stuff that'd be grateful.
Again, like Helixirr:
What do you think you should do? How do you think you should approach the problem?
would you like me to start writing a code?
Design a class named Rectangle to represent a rectangle. The class contains..
2 double data fields named width and height that specify the width and height of the rectangle

Do this and publish it.
#include <iostream>
using namespace std;

class Rectangle
{
public:
double height;
double width;
Wonderful! And now
a no-arg constructor that creates a default rectangle with width 1 and height 1
But ok now. We give you some time to go through your lesson step by step. Take some hours or better some days to write down your program. For more details on f.e. constructors see http://www.cplusplus.com/doc/tutorial/classes/.

Good bye and have fun!
can you explain to me what a no arg constructor is?
A constructor that takes no arguments, otherwise known as a default constructor.
Last edited on
we went over how to do class circles and it was completely different for a rectangle. So..


Your statement implies that you have already seen a rectangle class. And you are now asking for detailed info on how to do it?

Any way your statement is not really correct. Obviously a circle has a centre point & radius while a rectangle has width & height, and a circle has lots of different ways of being created, but the organisation of the code is the same.

You have constructors to set member variables when the object is created, and functions to change the value of the member variables (set functions), functions to retrieve values of member variables (get functions), and output functions to, say, print out values.

Professor wrote:
accessor and mutator functions for all data fields


Try to avoid writing a get / set function for each member variable - only write them if you need them. Remember class functions have direct access to all the member variables, so you can take advantage of this in your Output function. As well as that you can have functions that set several variables at once. IMO, one only needs get functions if the data is required by another class. Having get / set functions for every member variable is effectively the same as making them all public.

If you explain why you didn't have get functions & only 1 set function in this case, you professor would be a bit unfair to mark you down IMO.

Also, having get in a function name is a bit misleading - it implies the retrieval of a member variable. I would just name the functions Area & Perimeter.

There is a bit of a convention to name member variables with a leading m_ as in m_Width. This can make it easier to name function parameters.

There you go, some stuff to think about.
There is a bit of a convention to name member variables with a leading m_ as in m_Width.

Some others prefer prefixing object attributes (often called "members" in C) only with a leading '_'. Whatever you'll like, my opinion is trying to write a program so that it may be read as close to a text written in natural language.
what i meant was that since like you said a circle has a radius; a rectangle has two things to worry about..
i'm just asking for a simple and UNDERSTANDABLE explanation..
not a code.
because the words just accumulate to more and more misunderstanding since i'm not familiar with any of them.. and my teacher isn't a really good teacher..
i've had to take this class twice now because of lack of teaching and understanding..
Understanding mostly depends on yourself. So try it and teach yourself!!
Seeing that you appear to be really struggling. You should be able to understand the following code, that does what you are being asked to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>

using namespace std;

class Rectangle
{
public:
   Rectangle()
      : width(1), height(1)
   {
   }

   Rectangle(double w, double h)
      : width(w), height(h)
   {
   }

   void Width(double w)    { width = w; }
   double Width()          { return width; }
   void Height(double h)   { height = h; }
   double Height()         { return height; }

   double getArea()        { return width * height; }
   double getPerimeter()   { return 2 * (width + height); }

private:
   double width;
   double height;
};

int main()
{
   // 2 instances of our class:
   Rectangle r1, r2(10,20);

   // each obj's properties:
   cout << "width of 'r1': " << r1.Width() << endl;
   cout << "height of 'r1': " << r1.Height() << endl;
   cout << "width of 'r2': " << r2.Width() << endl;
   cout << "height of 'r2': " << r2.Height() << endl;

   // each obj's functions:
   cout << "area of 'r1': " << r1.getArea() << endl;
   cout << "area of 'r2': " << r2.getArea() << endl;
   cout << "perimeter of 'r1': " << r1.getPerimeter() << endl;
   cout << "perimeter of 'r2': " << r2.getPerimeter() << endl;
}


You shouldn't blame it all on your teacher, you need to read more on the subject and try and learn from material freely available on the net especially on this web site. Take a look at the C++ Tutorial to start with...

HTH

Andrew
As ajh32 said
especially on this web site.
The examples there will fit nearly exactly what you're looking for.
Topic archived. No new replies allowed.