Create a parametric string type. the basic type is to act as container class that contains a class t object. in the prototype case, the object is a char. the normal end-of-string Sentinel is 0. the standard behavior shocould model the functions found in
String library. The class definition cocould parameterize the Sentinel as well. Such a type exists in the standard library string.
Create a parameterized string type, which is a container class containing the object class T. In the string prototype, the object is of the char type, and the end mark of the string is 0. When this parameter type is implemented, the string library can be imitated, and the end mark of the string can be parameterized during class definition.
// BookProgramCompiled with vcsp6
# Include <iostream>
# Include <cassert>
# Include <string>
Using namespace STD;
Const int max_size = 100;
template
class my_string
{< br> Public:
my_string (const my_string & Str) // define constructor
{< br> Len = Str. len;
end = Str. end;
for (INT I = 0; I {< br> S [I] = Str. s [I];
}< BR >}; // constructor function 1
my_string ()
{< br> Len = 0;
end = 0;
S [0] = end;
};
my_string (const T * STR, const t end): end (end)
{< br> Len = 0;
while (STR [Len]! = END)
{< br> S [Len] = STR [Len ++];
}< br> S [Len] = end;
}; // constructor function 2
~ My_string () {}; // destructor function
My_string & operator = (const my_string & Str) // '='-> overloading, assignment operations
{
Len = Str. Len;
End = Str. end;
For (INT I = 0; I <Len; I ++)
{
S [I] = Str. s [I];
}
Return * this;
};
T operator [] (INT index) // overloading of '[]'
{
Return s [Index];
};
// Define the friend Functions
Friend my_string & operator + (const my_string & str1, const my_string & str2); // the overloading of '+'
Friend bool operator = (const my_string & str1, const my_string & str2); // judge if two strings are equal
Int size ()
{
Return Len;
};
Bool empty ()
{
Return Len = 0;
};
Void print ()
{
For (INT I = 0; I <Len; I ++)
Cout <s [I];
Cout <Endl;
};
PRIVATE:
Int Len; // record the length of the string
T end; // the ends of a string
T s [max_size]; // s stores the string
};
Template <typename T>
Bool operator = (const my_string <t> & str1, const my_string <t> & str2)
{
If (str1.len = str2.len)
{
For (INT I = 0; I <Len; I ++)
{
If (str1.s [I]! = Str2.s [I])
Return 0;
}
Return 1;
}
Return 0;
}
Template <typename T>
My_string <t> operator + (const my_string <t> & str1, const my_string <t> & str2) // the overloading of '+'
{
My_string newmy_string;
Newmy_string.len = str1.len + str2.len-1;
For (INT I = 0; I <str1.len; I ++)
{
Newmy_string.s [I] = str1.s [I];
}
For (Int J = 0; j <= str2.len; j ++)
{
Newmy_string.s [I + J] = str2.s [J];
}
Return newmy_string;
}
Int main ()
{
Char;
Int A1;
Double A2;
Char type1 [10];
Int type;
Char up [100];
Int flag1 = 0, flag2 = 0, flag3 = 0;
Int number1 [100], num;
Double number2 [100];
Cout <"I implemented the +, =, =, [], empty (), size () features. Welcome to" <Endl;
Cout <"************************************ * ******************* "<Endl;
Cout <"Enter The expected type (char Int or double):" <Endl;
Cin> type1;
If (strcmp (type1, "char") = 0)
Flag1 = 1;
If (strcmp (type1, "int") = 0)
Flag2 = 1;
If (strcmp (type1, "double") = 0)
Flag3 = 1;
Cout <"Enter the end mark (corresponding to the type you entered)" <Endl;
If (flag1 = 1)
Cin>;
If (flag2 = 1)
Cin> A1;
If (flag3 = 1)
Cin> A2;
Cout <"************************************ * ******************* "<Endl;
Cout <"Enter data:" <Endl;
Cout <"if you select char type, enter a row of data to end with the corresponding end sign" <Endl;
Cout <"if it is another type, enter the number of numbers" <Endl;
If (flag1 = 1)
{
Cin> up;
}
If (flag2 = 1)
{
Cin> num;
For (INT I = 0; I <num; I ++)
{
Cout <"Enter the" <I + 1 <"count" <Endl;
Cin> number1 [I];
}
Number1 [num] = A1;
}
If (flag3 = 1)
{
Cin> num;
For (INT I = 0; I <num; I ++)
{
Cout <"Enter the" <I + 1 <"count" <Endl;
Cin> number2 [I];
}
Number2 [num] = a2;
}
My_string <char> temp1 (up, );
My_string <int> temp2 (number1, A1 );
My_string <double> temp3 (number2, A2 );
Cout <"************************************ * **************** "<Endl;
Cout <"1. Print the entire row of data:" <Endl;
Cout <"2. Calculate the size of the input data volume:" <Endl;
Cout <"0. End operation:" <Endl;
Cin> type;
While (type! = 0)
{
Switch (type)
{Case 1:
{
If (flag1 = 1)
Temp1.print ();
If (flag2 = 1)
Temp2.print ();
If (flag3 = 1)
Temp3.print ();
}
Break;
Case 2:
{
If (flag1 = 1)
Cout <temp1.size () <Endl;
If (flag2 = 1)
Cout <temp2.size () <Endl;
If (flag3 = 1)
Cout <temp3.size () <Endl;
}
Break;
}
Cin> type;
}
Return 0;
}