So my code runs fine if I don't make my own destructor. As soon as I do, according to debugger and watches (I might not use them properly tho) it deletes the members of an object array as soon as I make them in main. The purpose of the code is filling an array with rectangles (width, height, colour) and then printing their info out after a user chooses the colour, in descending order (by surface area). Also, my constructor without parameters conflicts with the overloaded operator+ (which is supposed to add two rectangles by adding their widths and heights). Again, code compiles and works just fine without my destructor and without the constructor without parameters ("disabled" in the code below).
#include <iostream>
#include <string.h>
using namespace std;
class rectangle
{
private:
float width;
float height;
char *colour;
public:
// rectangle(){width=0;height=0;colour=0;}
rectangle(float, float, char *);
// ~rectangle();
float reWidth(){return width;}
float reHeight(){return height;}
char * reColour(){return colour;}
void changeWid(float a){width=a;}
void ChangeHei(float b){height=b;}
float area();
rectangle largerArea(rectangle);
void printInfo();
void showColour(rectangle *, int, char *);
friend rectangle operator+(rectangle,rectangle);
static float totalArea;
};
float rectangle::totalArea=0;
rectangle::rectangle(float a=0, float b=0, char *c=""):width(a),height(b)
{
colour=new char[strlen(c)];
strcpy(colour,c);
totalArea+=area();
}
/*rectangle::~rectangle()
{
delete colour;
colour=0;
}*/
float rectangle::area()
{
return width*height;
}
rectangle rectangle::largerArea(rectangle a)
{
if(a.area()>area())
return a;
else if(a.area()<area())
return *this;
else return a.width>=width?a:*this;
}
void rectangle::printInfo()
{
cout<<width<<" x "<<height<<" - "<<colour<<endl;
}
rectangle operator+(rectangle a, rectangle b)
{
rectangle temp;
if((a.width && b.width && a.height && b.height) !=0)
{
temp.width=a.width+b.width;
temp.height=a.height+b.height;
}
else if(b.height==0)
{
temp.width=a.width+b.width;
temp.height=a.height+b.width;
}
else if(a.height==0)
{
temp.width=a.width+b.width;
temp.height=a.width+b.height;
}
return temp;
}
void rectangle::showColour(rectangle* arrayr, int n, char* col)
{
int i,j;
rectangle temp;
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (arrayr[i].area() > arrayr[j].area())
{
temp = arrayr[i];
arrayr[i] = arrayr[j];
arrayr[j] = temp;
}
}
}
for(int i=0;i<n;i++)
if(strcmp(col,arrayr[i].colour)==0)
arrayr[i].printInfo();
}
int main()
{
float a,b;
char c[20];
cout<<"Insert info for rectangle A: ";
cin>>a>>b>>c;
rectangle A(a,b,c);
cout<<"A=";A.printInfo();
cout<<"Insert info for rectangle B: ";
cin>>a>>b>>c;
rectangle B(a,b,c);
cout<<"B=";B.printInfo();
cout<<"A+B=";(A+B).printInfo();
cout<<"A+3=";(A+3).printInfo();
cout<<"3+A=";(3+A).printInfo();
int n;
cout<<"Array length? : ";
cin>>n;
rectangle arrayr[n];
for(int i=0;i<n;i++)
{
cout<<"Insert info for "<<i+1<<"-th rectangle: ";
cin>>a>>b>>c;
arrayr[i]=rectangle(a,b,c);
}
char colour[20];
cout<<"Choose colour: ";
cin>>colour;
arrayr[1].showColour(arrayr,n,coulour);
cout<<"Total area of created rectangles is: "<<rectangle::totalArea;
}