Original question
/// Mid-exam @ 20120421-
# Include <iostream> // cout, Endl
# Include <cstring> // strlen, strcpy
Using namespace STD;
Class bug
{
Char * Data _;
Public:
Bug (const char * Str)
{
Data _ = new char [strlen (STR) + 1];
Strcpy (Data _, STR );
}
~ Bug ()
{
Delete data _;
}
Void show ()
{
Cout <data _ <Endl;
}
};
Void test (bug str1)
{
Str1.show ();
Bug str2 ("Tsinghua ");
Str2.show ();
Str2 = str1;
Str2.show ();
}
Int main ()
{
Bug str1 ("2011 ");
Str1.show ();
Test (str1 );
Str1.show ();
Return 0;
}
Answer:
# Include <iostream> // cout, Endl
Using namespace STD;
Class bug
{
Char * Data _;
Public:
Bug (const char * Str)
{
Data _ = new char [strlen (STR) + 1];
Strcpy (Data _, STR );
}
~ Bug ()
{
Delete [] data _;
Data _ = NULL;
}
Int size ()
{
If (Data _ = NULL)
Return-1;
Else
Return strlen (Data _);
}
Bool reget (const char * Str)
{
Delete [] data _;
Data _ = new char [strlen (STR) + 1];
If (null = data _)
{
Return false;
}
Else
{
Strcpy (Data _, STR );
Return true;
}
}
Bool get_str (char * & Str)
{
If (null! = Str)
{
Delete [] STR;
STR = NULL;
}
STR = new char [strlen (Data _) + 1];
If (null = Str) return false;
Strcpy (STR, data _);
Return true;
}
Void show ()
{
Cout <data _ <Endl;
}
Bug & operator = (bug & right)
{
Char * STR = NULL;
If (right. get_str (STR ))
{
Reget (STR );
Delete [] STR;
}
}
};
Void test (bug & str1)
{
Str1.show ();
Bug str2 ("Tsinghua ");
Str2.show ();
Str2 = str1;
Str2.show ();
}
Int main ()
{
Bug str1 ("2011 ");
Str1.show ();
Test (str1 );
Str1.show ();
Return 0;
}