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:

OMG I take it you haven't even attempted to compile that code because it is just littered with errors. Fix the errors then come back to us, post new code and questions. I started to make some corrections but gave up not because I didn't know how to correct them but because it isn't work my time and effort when it is obvious you haven't bothered to do your job.

Hey I compile the program and have no errors however it is not outputting anything. Here is the new code :_

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;
};
# 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::Rect (int length,int width)
   {
	   length = 1;
	   height = width;
	   fillChar = '*';
	   borderChar = '+';
   }

   int Rect:: perimeter()
   {
	   return 2*(length + height);
   }

   int Rect:: area ()
   {
	   
	   return length*height;
   }

   void Rect:: display()
   {
	   for(int i = 0 ;i <length;i++)
	   {
		   for(int j =0; j < height ; j++)
		   {
			   if((i==0)||(i ==(length-1)) ||(j==0) ||(j==(height-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++)
	   {
		   someArray[i].setFillCharacter(ch);
	   }
   }

Ok that compiles without errors/warnings now. The problem the display function doesn't work is because the Rect class constructor is wrong. Here is one way to correct it

Rect::Rect (int len,int width)
   {
	   this->length = len;
	   this->height = width;
	   this->fillChar = '*';
	   this->borderChar = '+';
   }

wow! its workin' YAY
Thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.