# Thread: I need help with making a rectangle using a class!

1. ## I need help with making a rectangle using a class!

Hey so I'm having trouble making this work. I do not know how to fix this code and I need some help!
Code:
```#include "stdafx.h"
#include <iostream>
using namespace std;

class Rectangle {
int width, height;
public:
void set_values (int,int);
int area() {

}
Rectangle(int w, int h) {  // parameterized constructor

}
};

void Rectangle::set_values (int x, int y) {

}

int main () {

Rectangle rect1(5,6);
cout << "area: " << rect1.area() << endl;

Rectangle rect2;
rect2.set_values (3,4);
cout << "area: " << rect2.area() << endl;
return 0;
}``` 2. That looks to me like homework, where you've been given sample code to finish, but haven't done touched it yourself. Give a suggestion of how you think this could be solved and we can assist if problems arise. 3. it is homework, I believe that I have to change it so the class Rectangle can receive values and input it as its values.

I'm not a very avid coder, and I struggle a lot. I would really appreciate it if you can teach me how to do this! 4. Syntax for constructors is:
Code:
```SomeClass(int a, int b)
{
member_a = a;
member_b = b;
}

// Modern approach via initialization list, preferred
SomeClass(int a, int b) : member_a(a), member_b(b)
{ }```
What do you think the area() function needs to compute? It's a mathematical question, nothing about coding. 5. It needs to compute the area of the rectangle? 6. Well yes, but how is the area of a rectangle computed? How does this relate to the data stored in your Rectangle class? You'll need to do the work. Your teacher won't have sent you home with this assignment without first explaining the necessary parts, like working with variables.

Anyway, I've given you the example for the Constructor. And the first part using regular x = y style assignment works all the same for a set_values kind of function.

Homework Policy 7. So, I had a lot of fun coding this up. It's so over-engineered that it can't possibly be turned in for a homework assignment: Compiler Explorer - C++

But that's some type-safe, awesome C++ for ya!

Boost's strong typedefs are severely underrated. The coolest part is, you can see in the assembly where the compiler calculates the area before the program is even run:
Code:
`mov     esi, 169` 8. It would appear that the entire class instance has been optimized down to a single integer.  Popular pages Recent additions answer;, class, int, rectangle, void 