Hi, I am trying to do my project but I am stuck. It looks like a big project but it is not. here is the question : I am given sample output of rectangles and some code. I need to add few free functions to make the outout like that
Sample output :
Initial rects - notice the automatic initialization!
+
+
+
+
After creating new rects
++++++++++
+********+
++++++++++
++++
+**+
+**+
+**+
+**+
++++
+++++++
+*****+
+*****+
+*****+
+++++++
+
Changing border for myArray[0]
&&&&&&&&&&
&******** &
&&&&&&&&&&
Changing fill for myArray[1]
++++
+~~+
+~~+
+~~+
+~~+
++++
Changing border & fill for myArray[2]
???????
? ?
? ?
? ?
???????
Changing fill for all rects
&&&&&&&&&&
&########&
&&&&&&&&&&
++++
+##+
+##+
+##+
+##+
++++
???????
?#####?
?#####?
?#####?
???????
+
here is the header file []
class Rect
{
public:
Rect(int length = 1, int width = 1);
int perimeter();
int area();
void display();
void setFillCharacter(char);
void setBorderCharacter(char);
private:
int length;
int height;
char fillChar;
char borderChar;
};
here is the code I have written so far :
# include <iostream>
using std::cin;
using std::cout;
using std::cin;
using std::endl;
#include "rect.h"
void displayAll (Rect someArray [], int length);
void changeAllFill(Rect someArray[], int length, char ch);
int longestPerimeter(Rect someArray [], int length);
Rect (int length =1 ; int width =1)
{
length = 1;
width = 1;
}
int rect:: perimeter()
{
int length;
int width;
return length * width;
}
int rect:: area ()
{
int length;
int width;
return length*width;
}
void rect:: display()
{
for(int i = 0 ;i <length;i++)
{
for(int j =0; j < width ; j++)
{
if((i==0)||(i ==(length-1)) ||(j==0) ||(j==(width-1))
{
cout<<borderChar;
}
else
{
cout<<fillChar;
}
}
cout<<endl;
}
}
void rect:: setFillCharacter (char cr)
{
fillChar = cr;
}
void rect:: setBorderCharacter ( char chr)
{
borderChar = chr;
}
int main()
{
const int NUMRECTS = 4;
Rect myArray [NUMRECTS];
// Wow, all rect objects are initialized!!!!
cout << "Initial rects - notice the automatic initialization!" << endl;
displayAll(myArray, NUMRECTS);
// Assign some new ones and display again
myArray[0] = Rect(10,3);
myArray[1] = Rect(4,6);
myArray[2] = Rect(7,5);
cout << "After creating new rects" << endl;
displayAll(myArray, NUMRECTS);
cout << "Changing border for myArray[0]" << endl;
myArray[0].setBorderCharacter('&');
myArray[0].display();
cout << "Changing fill for myArray[1]" << endl;
myArray[1].setFillCharacter('~');
myArray[1].display();
cout << "Changing border & fill for myArray[2]" << endl;
myArray[2].setFillCharacter(' ');
myArray[2].setBorderCharacter('?');
myArray[2].display();
cout << "Changing fill for all rects" << endl;
changeAllFill(myArray, NUMRECTS, '#');
displayAll(myArray, NUMRECTS);
cout << "The longest perimeter is: " << longestPerimeter(myArray, NUMRECTS) << endl;
}
void displayAll ( Rect someArray [] , int length)
{
for(int i = 0 ;i<length; i++)
{
someArray[i].display();
}
}
void changeAllFill(Rect someArray[], int length ,char ch)
{
for(int i = 0; i<length ; i++)
{
for(int j = 0 ; j<width ; j++)
{
if((!i==0) ||(!i==(length-1)) || (!j==0) || (!j ==(width-1)))
{
cout<<ch;
}
}
cout<<endl;
}
Seems like my changeall Fill() function and displayAll() function is not working!
Please help me to rewrite this code: