Program in C++ calculate area and perimeter of rectangle.

In summary: Instead, the function printrectangle() is called, and it takes two arguments: the perimeter and area of the rectangle.The prototype for printrectangle() should also have two arguments, like thisvoid printrectangle(float perimeter, float area);
  • #1
Mrencko
109
0
1. Homework Statement
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()

The Attempt at a Solution


this is my code currently i don't have idea if i am ok, i don't get how to run my things in the main function, i need some help i am veery new and the teacher don't like to explain just say, that's wrong..

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()
{
  rectangle ready;
  ready.calcperimeter()
  return 0;
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
Mrencko said:
1. Homework Statement
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()

The Attempt at a Solution


this is my code currently i don't have idea if i am ok, i don't get how to run my things in the main function, i need some help i am veery new and the teacher don't like to explain just say, that's wrong..
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()
{
    rectangle ready;
    ready.calcperimeter()
    return 0;
}
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]
C:
int main(void)
{
   // some code
   return 0;
}
[/code]
 
  • #3
C:
1-where i should put the arguments in main just there i don't 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
sorry bad internet conection can someone erase the spam post?
 
  • #5
this is the new 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()
{
    rectangle ready;
    ready.calcperimeter(float,float)
    ready.calcarea(float,float)
    ready.printrectangle(float.float)
    return 0;
}
 
  • #6
Mrencko said:
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()

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:
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:
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:
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:
  • #7
This function isn't formed correctly:
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.

Stephen Tashi said:
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".
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
well i don't 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
Mrencko said:
well i don't 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
You could ask for user input in main() for the base and height, and then call your rectangle constructor with those values.

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

   rectangle rect(base, height);
   .
   .
   .
 
  • #10
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
This is so far my code and can't 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?
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
Mrencko said:
This is so far my code and can't compile yet, it says in line 48 expected unqualified-id before "." token
I assume the first line below is line 48, :
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.
Mrencko said:
and well why do i need the print function if the program ask in main for input values?
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.
Mrencko said:
or the print its wrong and i need to change it to actually display the dimensions and sides of the rectangles?
Yes, your printrectangle() function is wrong -- it doesn't do anything useful. All it does is print two messages.
C:
void rectangle::printrectangle(){
cout<<"introduzca la base"<<endl;

cout<<"introduzca la altura"<<endl;}
Mrencko said:
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;
    }
Here are the prototypes for the member functions of your class:
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:
C:
float rectangle::calcarea(float b1,float h1)
{
    float area;
    area=h1*b1;
}

The header (top line above) should probably be this:
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().
 
  • #14
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
Mrencko said:
rectangle rect(base, height); the constructor and the arguments declared before in main but what means "rect",
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.

i am wondering if this line is rectangle(base,height);, or otherwise i need to create that "rect" as variable in main also
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
@Mrencko, any further progress on this problem, or have you given up?
 

1. How do I calculate the area of a rectangle in C++?

To calculate the area of a rectangle in C++, you need to first declare two variables to store the length and width of the rectangle. Then, use the formula area = length * width to calculate the area. Make sure to include the necessary #include statements and use the cout function to display the result.

2. What is the formula for calculating the perimeter of a rectangle in C++?

The formula for calculating the perimeter of a rectangle in C++ is perimeter = 2 * (length + width). Similar to calculating the area, you will need to declare variables, include necessary #include statements, and use the cout function to display the result.

3. Can I input the length and width of the rectangle from the user?

Yes, you can use the cin function to prompt the user to enter the length and width of the rectangle. Make sure to use proper variable types and include the necessary #include statements.

4. How do I handle errors for invalid input?

You can use conditional statements and loops to validate user input and ensure that the program only accepts valid values for length and width. You can also use exception handling to handle any runtime errors that may occur during the calculation process.

5. Can I use C++ to calculate the area and perimeter of other shapes?

Yes, C++ can be used to calculate the area and perimeter of various shapes such as squares, circles, triangles, etc. You will need to use the appropriate formulas and modify your code accordingly. It is always a good practice to plan and design your program before starting to code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
4K
Back
Top