CPP Snippets - Copy constructor vs Assignment Operator usage



#include
using namespace std;



class COPY
{
private:
int a;
char *str;
public:
COPY(int value, char* str1)
{
a = value;
str = new char[50];
strcpy(str,str1);
}
COPY(const COPY& );
COPY& operator=(const COPY& );
~COPY()
{
delete str;
}
};

COPY::COPY(const COPY& C1)
{
a = C1.a;
str = new char[50];
strcpy(str,C1.str);
cout<}
COPY& COPY::operator=(const COPY& C1)
{
if(this == &C1)
return *this;
a = C1.a;
delete str;
str = new char[50];
strcpy(str,C1.str);
cout< return *this;
}

int main()
{
COPY C1(4,"HI");
COPY C2 = C1;
COPY C3(5,"FS");
C2 = C3;

return 0;
}

No comments :

Post a Comment