1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class String1 {
public:
// Constructors
String1();
String1(char *);
// Fill a character buffer argument
void GetString(char *);
// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);
friend String1 operator+(char *, String1 &);
// Assignment Operators
// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1 operator=(String1 &);
String1 operator=(char *);
// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
private:
char str[80];
};
String1::String1()
{
str[0] = '\0';
}
String1::String1(char * inputString)
{
strcpy(str, inputString);
}
void String1::GetString(char * bufferString)
{
strcpy(bufferString, str);
}
String1 String1::operator+(String1 & stringIn)
{
String1 temp(this->str);
strcat(temp.str, stringIn.str);
return temp;
}
String1 String1::operator+(char *bufferString)//this is were it breaks
{
String1 temp(this->str);
//cout << bufferString; //test to see whether function was taking "Test"...
//cout << temp; //test to see if the function gathered the pointer to the string value
strcat(temp.str, bufferString);
return temp;
}
String1 String1::operator=(String1 & stringIn)
{
String1 temp(this->str);
strcpy(stringIn.str, temp.str);
return temp;
}
String1 String1::operator=(char *bufferString)
{
String1 temp(this->str);
//strcpy(temp.str, bufferString);
return *this;
}
String1 operator+(char *bufferString, String1 &stringIn)
{
String1 temp;
cout << stringIn;
cout << bufferString;
//temp = bufferString + stringIn;
return temp;
}
ostream & operator<<(ostream &stream, const String1 & stringIn)
{
stream << stringIn.str;
return stream;
}
int main()
{
String1 S1("First ");
String1 S2("Last ");
String1 S3;
String1 S4;
String1 S5;
String1 S6;
cout << "\t\tHOMEMADE STRING CLASS" << endl;
cout << "\nLets use a friend function to see whats stored in the string array(s): " << endl;
cout << "String 1: " << S1 << endl;
cout << "String 2: " << S2 << endl;
cout << "\nTesting the function which concatenates two strings" << endl;
cout << "String 1 ( " << S1 << ") + String 2 ( " << S2 << ") appended = ";
cout << S1 + S2 << endl;
cout << "\nTesting the function which overloads the string assignment operator=" << endl;
cout << "For example, copy string 1 into string 3" << endl;
S1 = S3;
cout << "String 3 now carries the string " << S3 << endl;
cout << "\nTesting the string plus char* operator+ overload function ( S + 'Test')" << endl;
cout << "String 1 ( " << S1 << ") + 'Test' appended = ";
cout << S1 + "Test" << endl;
cout << "String 2 ( " << S2 << ") + 'Test' appended = ";
cout << S2 + "Test" << endl;
cout << "\nTesting the function which overloads the char* assignment operator=" << endl;
cout << "For example, copy char 'Test' into string 4" << endl;
S6 = "Test";
cout << "String 4 now carries the string " << S4 << endl;
cout << "\nTesting the friend function which again overloads the operator+ ('Test2' + S)" << endl;
cout << "Test2 " + S1 << endl;
cout << "Test2 " + S2 << endl;
system("pause");
}
|