Re: Need Help Reg : Overloading assignment Operator,Coppy constructor
Posted by: Hasani Blackwell
Date: January 06, 2006 05:41AM

OK, I wrote code.
1) I made the default constructor call the constructor with the signature MyClass(Int, char*)
2) I removed the default argument statements from the constructor with the signature MyClass(Int, char*)

http://www.estorecorp.com/mysql/myclass.html

So you'll be able to create stack variables using the default constructor.


#include<stdio.h>
#include<windows.h>
//
class MyClass
{
public:
MyClass();
MyClass(INT strlen , CHAR * str );
MyClass(MyClass const & A);
MyClass& operator= (MyClass const & A);
~MyClass();
private:
INT stringlen;
CHAR *mystring;
};

MyClass::MyClass()
{
MyClass(0, "");
}

MyClass::~MyClass()
{
//if( mystring )
// free(mystring);
}

MyClass::MyClass(MyClass const &A)
{
stringlen = strlen( A.mystring );
mystring = new char[ stringlen+1 ];
strcpy( mystring, A.mystring);
}

//assignement operator
MyClass& MyClass::operator= (MyClass const &A)
{
if ( this != &A )
{
delete [] this->mystring;
this->mystring = new char[strlen(A.mystring)];
strcpy( this->mystring, A.mystring);
}
return *this;
}

MyClass::MyClass( INT strlen1 , CHAR * str)
{
stringlen = strlen( str );
mystring = (CHAR*) calloc( sizeof( CHAR ), stringlen + 1);
strcpy ( mystring, str );
}

void main()
{
MyClass M1(6,"sarat");
MyClass M2 = M1;
MyClass M3(M2);
MyClass M4(6,"sarat");
M4 = M1;

CHAR *str, *str1 = "sarat";
str = ( CHAR* ) calloc ( sizeof (CHAR), 10);
strcpy( str,"sarat");
str[2]= 's';// shud work
printf("%d %d ",strlen(str),strlen(str1));
//str1[2]= 's'; will not work coz we assigned a constant
printf("%s %s", str, str1);
}

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: Need Help Reg : Overloading assignment Operator,Coppy constructor
561
January 06, 2006 05:41AM
686
January 06, 2006 08:46AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.