//This Runs fine on Borland but errors are shown on Dev C++
//The errors in Dev C++
/*
Errors when compiled on Dev C++;
In function `int main()':
83 no matching function for call to `polygon::polygon(polygon)'
note candidates are: polygon::polygon(polygon&)
note polygon::polygon(int)
83 initializing argument 1 of `polygon polygon::operator=(polygon)'
*/
#include <iostream.h>
class pause{public:~pause(){cout << "\nPress Enter to continue.."; cin.get();}};
class polygon
{
public:
polygon(int);
polygon(polygon&);
void setSides(int);
int getSides () const;
polygon operator=(const polygon apolygon);
~polygon();
private:
int * itsSides;
};
polygon::polygon(int sides)
{
cout << "CONSTRUCTOR " << this << " is the address of polygon created\n";
itsSides = newint;
*itsSides = sides;
}
void polygon::setSides(int sides)
{
*itsSides = sides;
}
int polygon::getSides() const
{
return *itsSides;
}
polygon::~polygon()
{
cout << "DESTRUCTOR " << this << " is address of polygon being destroyed\n";
delete itsSides;
}
polygon::polygon(polygon &aPolygon)
{
itsSides = newint;
*itsSides = aPolygon.getSides();
cout << "COPY CONSTRUCTOR 4 address " << this << "\n";
}
polygon polygon::operator=(const polygon aPolygon)
{
*(*this).itsSides = aPolygon.getSides();
//or
*this->itsSides = aPolygon.getSides();
//or
*(this->itsSides) = aPolygon.getSides();
return *this;
}
polygon withSides(int);
polygon withSides(int sides)
{
polygon *temp = new polygon(sides);
return *temp;
}
int main()
{
polygon traingle(5);
cout << &traingle << "is the address of traingle\n\n";
cout << "\nWrong traingle .. it has " << traingle.getSides() << " sides" << endl << endl;
traingle = withSides(3); //ERROR no matching function for call to `polygon::polygon(polygon)
cout << &traingle << "is the address of traingle\n";
cout << "\nMy traingle class is working on borland..it has " << traingle.getSides() << " sides" << endl;
cout << &traingle << "is the address of traingle\n";
cout << "ENd MaiN\n";
pause here;
return 0;
}
The output on borland
CONSTRUCTOR 0x0012ff88 is the address of polygon created
0x0012ff88is the address of traingle
Wrong traingle .. it has 5 sides
CONSTRUCTOR 0x009029f8 is the address of polygon created
COPY CONSTRUCTOR 4 address 0x0012ff50
COPY CONSTRUCTOR 4 address 0x0012ff80
DESTRUCTOR 0x0012ff50 is address of polygon being destroyed
DESTRUCTOR 0x0012ff80 is address of polygon being destroyed
0x0012ff88is the address of traingle
My traingle class is working on borland..it has 3 sides
0x0012ff88is the address of traingle
ENd MaiN
Press Enter to continue..