Can someone help me write this code a different, preferably shorter, way, but it does the same thing.

// Displays constructors and classes


#include <iostream>
using namespace std;

class Rectangle // Class declaration
{
int width;
int length;

public:


// Default constructor
Rectangle()
{
width = 1;
length = 1;
}
// Parameterized constructor
Rectangle(int w, int l)
{
width = w;
length = l;
}
void SetValues() // Prompts the user to enter values
{
cout << "Enter the width;";
cin >> width;
cout << "Enter the length :";
cin >> length;
}
int GetWidth() const // The function is constant
{
return width;
}
int GetLength() const // The function is constant
{
return length;
}
int GetArea() // Sets the formula for area
{
return width*length;
}
int GetPerimeter() // Sets formula for perimeter
{
return width * 2 + length * 2;
}
};

int main()
{


Rectangle rect1; // Default constructor (1,1)
Rectangle rect2(1, 1);
Rectangle rect3;
Rectangle rect4;
Rectangle rect5;


rect1.SetValues(); // Prompts user to enter values
rect3.SetValues();
rect4.SetValues();
rect5.SetValues();

cout << rect1.GetWidth() << "," << rect1.GetLength() << endl; // Retrieves length and width
cout << rect2.GetWidth() << "," << rect2.GetLength() << endl;
cout << rect3.GetWidth() << "," << rect3.GetLength() << endl;
cout << rect4.GetWidth() << "," << rect4.GetLength() << endl;
cout << rect5.GetWidth() << "," << rect5.GetLength() << endl;

cout << "rect1 area: " << rect1.GetArea() << endl; // Retrieves area from the member function
cout << "rect2 area: " << rect2.GetArea() << endl;
cout << "rect3 area: " << rect3.GetArea() << endl;
cout << "rect4 area: " << rect4.GetArea() << endl;
cout << "rect5 area: " << rect5.GetArea() << endl;

cout << "rect1 perimeter: " << rect1.GetPerimeter() << endl; // Retrieves perimeter from member function from class
cout << "rect2 perimeter: " << rect2.GetPerimeter() << endl;
cout << "rect3 perimeter: " << rect3.GetPerimeter() << endl;
cout << "rect4 perimeter: " << rect4.GetPerimeter() << endl;
cout << "rect5 perimeter: " << rect5.GetPerimeter() << endl;

return 0;
}
Since you need to iterate through multiple objects, create an array of objects, and the step through the array calling the appropriate functions for each.

Here's a hint to get you started:

1
2
3
Rectangle arr[5];

arr[0].SetValues();
So where would this go? In main. I'm really having trouble so it would help if you can like explain it a little more in detail. (not trying to be rude) @mastakhan
the class is fine.

Main can be reduced greatly, ... put the rects in a vector, and if you can re-order the code a little one loop will do it..

for(int x = 0; x < v.size(); x++)
{
v[x].SetValues();
cout << v[x].GetWidth() << "," << v[x].GetLength() << endl; // Retrieves length and width
cout << "area: " << v[x].GetArea() << endl; // add stuff to print x if needed?
cout << "perimeter: " << v[x].GetPerimeter() << endl; //as above
}

If the code order matters, you need 4 loops.

Last edited on
I love your answer, but can you simplify this to a beginners terms. I have to present this and many people dont understand that. @jonnin
That is about as simple as I can make it...

1) use a vector or array to contain 5 or N instances of the class object
2) use a loop to call the functions


Topic archived. No new replies allowed.