mr_esmaily
شنبه 18 تیر 1384, 12:25 عصر
سلام
من یک کلاس بصورت زیر تعریف کردم:
[code]class xString
{
private :
static const int MAX=10;
char str[MAX];
public :
xString(){}
xString (char Str[])
{
if(strlen(Str) < MAX)
strcpy(str,Str);
else
{
cout << "Error : Overflow" << endl;
strcpy(str,"");
}
}
xString operator+(xString right)
{
xString temp;
if(strlen(str) + strlen(right.str) < MAX)
{
strcpy(temp.str,"");
strcat(temp.str,str);
strcat(temp.str,right.str);
}
else
{
cout << "Error : Overflow" << endl;
strcpy(temp.str,"");
}
return temp;
}
xString operator+(char Str[])
{
xString temp;
if(strlen(str) + strlen(Str) < MAX)
{
strcpy(temp.str,"");
strcat(temp.str,str);
strcat(temp.str,Str);
}
else
{
cout << "Error : Overflow" << endl;
strcpy(temp.str,"");
}
return temp;
}
void DisPlay()
{
if(strcmp(str,""))
cout << str << endl;
else
cout << "Error : xString Is NULL." << endl;
}
};[\code]
در تابع main()هم بصورت زیر تعریف و استفادش کردم :
[code]void main()
{
xString str1("Hello"),str2(" World!"),str3;
str3=str1 + str2;
str3.DisPlay();
str3=str1 + str2;
str3.DisPlay();
}[\code]
چجوری میشه عملگر + رو باراضافی داد که بتوان در تابع ()main از دستور زیر استفاده کرد :
[code]str3="GOOD." + str3;[\code]
من یک کلاس بصورت زیر تعریف کردم:
[code]class xString
{
private :
static const int MAX=10;
char str[MAX];
public :
xString(){}
xString (char Str[])
{
if(strlen(Str) < MAX)
strcpy(str,Str);
else
{
cout << "Error : Overflow" << endl;
strcpy(str,"");
}
}
xString operator+(xString right)
{
xString temp;
if(strlen(str) + strlen(right.str) < MAX)
{
strcpy(temp.str,"");
strcat(temp.str,str);
strcat(temp.str,right.str);
}
else
{
cout << "Error : Overflow" << endl;
strcpy(temp.str,"");
}
return temp;
}
xString operator+(char Str[])
{
xString temp;
if(strlen(str) + strlen(Str) < MAX)
{
strcpy(temp.str,"");
strcat(temp.str,str);
strcat(temp.str,Str);
}
else
{
cout << "Error : Overflow" << endl;
strcpy(temp.str,"");
}
return temp;
}
void DisPlay()
{
if(strcmp(str,""))
cout << str << endl;
else
cout << "Error : xString Is NULL." << endl;
}
};[\code]
در تابع main()هم بصورت زیر تعریف و استفادش کردم :
[code]void main()
{
xString str1("Hello"),str2(" World!"),str3;
str3=str1 + str2;
str3.DisPlay();
str3=str1 + str2;
str3.DisPlay();
}[\code]
چجوری میشه عملگر + رو باراضافی داد که بتوان در تابع ()main از دستور زیر استفاده کرد :
[code]str3="GOOD." + str3;[\code]