# Homework Help: Program in C++ calculate area and perimeter of rectangle.

1. Sep 4, 2016

### Mrencko

1. The problem statement, all variables and given/known data
calculate the area and perimeter of 2 rectangles(two objetcs and one builder), print the sides, area and perimeter, the function printrectangle must identify which side belongs to base and height...

the teacher suggest this in private: float side1 float side2
and this in public:
rectangle()
rectangle(float s1, float s2)
void printrectangle()
float calcarea()
float calcperimeter()

3. The attempt at a solution
this is my code currently i dont have idea if i am ok, i dont get how to run my things in the main function, i need some help i am veery new and the teacher dont like to explain just say, thats wrong..

Code (C):
#include<iostream>

using namespace std;

class rectangle {
private:
float b,h;

public:
rectangle( float b,float h);
void printrectangle();
float calcarea();
float calcperimeter();
};

rectangle::rectangle(float a, float b)
{
b=b1;
h=h1;
}

void rectangle::calcarea(float b1,float h1)
{
float area;
area=h1*b1
}

void rectangle::calcperimeter(float b1,float h1)
{
float perimeter;
perimeter=(2*b1)+(2*h1)
}

void rectangle::printrectangle(float perimeter, float area)
cout<<"input the  base"<<endl;
cin>>b1;
cout<<"input the height"<<endl;
cin>>h1;

int main()
{
return 0;
}

Last edited by a moderator: Sep 4, 2016
2. Sep 4, 2016

### Staff: Mentor

1. Your rectangle constructor takes two arguments, the base and height. When you call this constructor, in main(), you aren't providing either of these arguments.
2. The prototypes (declarations) for calcperimeter(), calcarea(), and printrectangle() don't agree with the headers (i.e., top lines) for the definitions for these functions. For example, the definition for calcarea() shows that it has two arguments. The prototype in your rectangle class should also have two arguments.
3. The calcperimeter() function also takes two arguments. You are calling it as if it takes no arguments.
4. You aren't calling your calcarea() function at all. It should be called somewhere in main().
5. You aren't calling your printrectangle() function at all. It should be called somewhere in main().

Also, please use code tags for your code, like I did above.
They work like this:
[code=c]
Code (C):

int main(void)
{
// some code
return 0;
}
[/code]

3. Sep 4, 2016

### Mrencko

Code (C):

1-where i should put the arguments in main just there i dont get it how to call those arguments to the main
2- ok then this is correct in the prototype--float calcarea(float,float);-- ?
3-do you mean call it like this in main --calcperimeter(float,float)-- or like this ---calcperimeter(float b1,float h1)---
4 and 5 how i should call those functions in the main correctly

4. Sep 4, 2016

### Mrencko

sorry bad internet conection can someone erase the spam post?

5. Sep 4, 2016

### Mrencko

this is the new code

Code (C):

#include<iostream>

using namespace std;

class rectangle {
private:
float b,h;

public:
rectangle( float b,float h);
void printrectangle(float,float);
float calcarea(float b1,float h1);
float calcperimeter(float b1,float h1);

};

rectangle::rectangle(float a, float b)
{
float b1,h1;
b=b1;
h=h1;
}
float rectangle::calcarea(float b1,float h1)
{
float area;
area=h1*b1;
}

float rectangle::calcperimeter(float b1,float h1)
{
float perimeter;
perimeter=(2*b1)+(2*h1);
}
void rectangle::printrectangle(float b1, float h1)
cout<<"introduzca la base"<<endl;
cin>>b1;
cout<<"introduzca la altura"<<endl;
cin>>h1;

int main()
{
return 0;
}

6. Sep 4, 2016

### Stephen Tashi

The teacher suggested two "builders" (which are more commonly called "constructors") for class rectangle.

If you use the constructor "rectangle" (with no arguments) in main() , then this constructor must provide a way for the user to input the dimensions of the rectangle. So the code for "rectangle()" should resemble the code you tried for printrectangle, which was
Code (Text):

void rectangle::printrectangle(float perimeter, float area)
cout<<"input the  base"<<endl;
cin>>b1;
cout<<"input the height"<<endl;
cin>>h1;

except that the constructor function rectangle() doesn't have any arguments and you want to store the dimensions of the rectangle in the private variables "b" and "h" that are defined in class rectangle. (You didn't take the teacher suggestion of naming those private variables "side1" and "side2", but that's ok).

If you use the constructor for class rectangle in main() that requires two arguments, then you must provide them.

If your program is supposed to demonstrate two different ways of constructing a rectangle by using two different types of constructor then it should look like

Code (Text):

int main()
{
...
rectangle rectangleA;
rectangle rectangleB( 3.0, 4.0);
...

If you want to create two rectangles using only the two argument constructor, it would look like:

Code (Text):

int main()
{
rectangle rectangleA(1.0,4.0);
rectangle rectangleB(3.0, 4.0);
....

The printrectangle() function does not need to ask the user to input the dimensions of the rectangle. The dimensions are established when the rectangle is created. So rectangleA.printrectangle() and rectangleB.printrectangle() can produce two different print-outs since the rectangles were created with different dimensions. All the code for printrectangle() must do is to print "b" and "h".

The reason so many of the functions suggested by the teacher don't need arguments is that they can do their work using "b" and "h".

Last edited by a moderator: Sep 4, 2016
7. Sep 4, 2016

### Staff: Mentor

This function isn't formed correctly:
Code (C):

void rectangle::printrectangle(float b1, float h1)
cout<<"introduzca la base"<<endl;
cin>>b1;
cout<<"introduzca la altura"<<endl;
cin>>h1;

The body of a function definition has to be enclosed in braces - { }. Yours is missing them.
Also, this function should not be doing input -- the lines the start with cin>> ... shouldn't be there. The purpose of this function is to print the rectangle whose dimensions are the values stored in the private member variables b and h.

Based on what Stephen Tashi is saying, the printrectangle doesn't need parameters, and shouldn't have them. Member functions have access to the private variables b and h, so just print them.

8. Sep 5, 2016

### Mrencko

well i dont get it where i should ask to input the values for rectangle sides if i erase that from the printrectangle function where i should ask for the values, do i need another function or in main function, sorry for the noob questions

9. Sep 5, 2016

### Staff: Mentor

You could ask for user input in main() for the base and height, and then call your rectangle constructor with those values.

Code (C):
int main(void)
{
float base, height;
cout << "Enter the base ";
cin >> base;
cout << "Enter the height ";
cin >> height;

rectangle rect(base, height);
.
.
.

10. Sep 5, 2016

### Mrencko

rectangle rect(base, height); the constructor and the arguments declared before in main but what means "rect", i am wondering if this line is rectangle(base,height);, or otherwise i need to create that "rect" as variable in main also

11. Sep 5, 2016

### Mrencko

This is so far my code and cant compile yet, it says in line 48 expected unqualified-id before "." token
and well why do i need the print function if the program ask in main for input values? or the print its wrong and i need to change it to actually display the dimensions and sides of the rectangles?

Code (C):

#include<iostream>

using namespace std;

class rectangle {
private:
float b,h;

public:
rectangle( float b,float h);
void printrectangle();
float calcarea(float b1,float h1);
float calcperimeter(float b1,float h1);

};

rectangle::rectangle(float a, float b)
{
float b1,h1;
b=b1;
h=h1;
}
float rectangle::calcarea(float b1,float h1)
{
float area;
area=h1*b1;
}

float rectangle::calcperimeter(float b1,float h1)
{
float perimeter;
perimeter=(2*b1)+(2*h1);
}
void rectangle::printrectangle(){
cout<<"introduzca la base"<<endl;

cout<<"introduzca la altura"<<endl;}

int main(){

float b1,h1;{
cout<<"enter the base";
cin>>b1;
cout<<"enter the height";
cin>>h1;}
rectangle rect(b1,h1);
rectangle.printrectangle()
rectangle.calcarea()
rectangle.calcperimeter()
return 0;
}

12. Sep 5, 2016

### Staff: Mentor

I assume the first line below is line 48, :
Code (C):

rectangle.printrectangle()
rectangle.calcarea()
rectangle.calcperimeter()

"rectangle" is not a variable -- it's the name of a class. The variable that you declared in your code is rect.
The most important reason is, because it's part of the specification for this class. The printrectangle() member function's purpose is to display the length and width of the rectangle.
Yes, your printrectangle() function is wrong -- it doesn't do anything useful. All it does is print two messages.
Code (C):
void rectangle::printrectangle(){
cout<<"introduzca la base"<<endl;

cout<<"introduzca la altura"<<endl;}
Here are the prototypes for the member functions of your class:
Code (C):

rectangle( float b,float h);
void printrectangle();
float calcarea(float b1,float h1);
float calcperimeter(float b1,float h1);
The rectangle() constructor looks OK, and so does the printrectangle() function.
The calcarea() and calcperimeter() functions don't need parameters -- they can use the private member variables b and h.

If you change the prototypes for calcarea() and calcperimeter(), you need to change the function headers in the definitions of these functions. For example, you have this:
Code (C):

float rectangle::calcarea(float b1,float h1)
{
float area;
area=h1*b1;
}
The header (top line above) should probably be this:
Code (C):
float rectangle::calcarea()
En otras palabras, no parameters. Use the private member variables.

Also, this function should return a value, which means that you need a return statement. Same is true for calcperimeter().

13. Sep 8, 2016

### Staff: Mentor

@Mrencko, have you made any progress on this problem?

14. Sep 8, 2016

### Mrencko

yes and not, i will update tomorrow, my new issues , will present a test of complex variable calculus and this is stand by until then.

15. Sep 8, 2016

### Stephen Tashi

In your program "rectangle" is general type of thing. The constructor for "rectangle" creates specific things of that type, such as "rect".
The C++ notation convention may seem somewhat peculiar because the arguments of the contructor are used with "rect", which his the specific thing that is constructed instead of with the general type of thing "rectangle", where the constructor is defined. But that's just the way it is.

The line in man.c "rectangle rect(b1,h1);" is what creates "rect" as an object of general type "rectangle".

Look on this page http://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm at the example for "Parmeterized Constructor". Instead of a two parameter constructor, it shows a class "Line" that has a one-parameter constructor. Note that in that example the class is named using a capital letter "Line" and a particular object is named in lowercase as "line". In your work, you didn't not use a capital letter to name the class "rectangle", so you have to name the particular object "rect" or some other name that is different from "rectangle".

16. Sep 18, 2016

### Staff: Mentor

@Mrencko, any further progress on this problem, or have you given up?