オブジェクト指向プログラムでこのコピー コンストラクターを実行するのに助けが必要です。Hello World
結果は、文字列 1:を文字列 2:にコピーする必要がありますThis is a test
。
私の.h
ファイルで:
void Copy(MyString& one);
私の.cpp
ファイルで:
void MyString::Copy(MyString& one)
{
one = String;
}
私のmain.cpp
ファイルで:
String1.Print();
cout << endl;
String2.Print();
cout << endl;
String2.Copy(String1);
String1.Print();
cout << endl;
String2.Print();
cout << endl;
出力:
Hello World
This is a test
is a test
This is a test
次のようになります。
Hello World
This is a test
Hello World
Hello World
私が間違っていることを教えてください。
これが私の .cpp ファイル全体です。
MyString::MyString()
{
char temp[] = "Hello World";
int counter(0);
while(temp[counter] != '\0') {
counter++;
}
Size = counter;
String = new char [Size];
for(int i=0; i < Size; i++)
String[i] = temp[i];
}
MyString::MyString(char *message)
{
int counter(0);
while(message[counter] != '\0') {
counter++;
}
Size = counter;
String = new char [Size];
for(int i=0; i < Size; i++)
String[i] = message[i];
}
MyString::~MyString()
{
delete [] String;
}
int MyString::Length()
{
int counter(0);
while(String[counter] != '\0')
{
counter ++;
}
return (counter);
}
void MyString:: Set(int index, char b)
{
if(String[index] == '\0')
{
exit(0);
}
else
{
String[index] = b;
}
}
void MyString::Copy(MyString& one)
{
one = String;
}
char MyString:: Get(int i)
{
if( String[i] == '\0')
{
exit(1);
}
else
{
return String[i];
}
}
void MyString::Print()
{
for(int i=0; i < Size; i++)
cout << String[i];
cout << endl;
}