Designing custom string class for school. This is what I have so far. How does it look? Ok great knew you liked it :P. No, I am having trouble figuring out how to write the +operator overload. What I seem to have works, except the out put is always seperated by a new line. For example, the output of a+b would be:
a
b
instead of:
ab
I am thinking that maybe it hold the newline from the first string input. Let me know what you think.
Code:
/*
** Name: swcString
** Description: A custom class to input/output strings of characters. There
** are some other special functions of the class but not sure what they are
** at the moment.
** Date: 2-25-05
** Author: Steven Davis
*/
#include <iostream> //Using for istream and ostream (<< >>)
#include <vector> //Using for vectors
#include <iomanip> //Using for Text manipulation
using namespace std; //Using standard namespace
class swcString; //Predeclaration of the class swcString
class swcString //Declaration of my String class
{
public: //Public Member Functions
//Constructors
swcString(); // Define Default Values
swcString(char []); // Store a copy of the string
swcString(swcString, int n); // Store n characters of str starting at pos 0
swcString(swcString, int pos, int n); // Same as above but with user defined start pos
swcString(int n, char); // Store n amounts of ch
//Accessors
int swcSize();
int swcLength();
bool swcEmpty();
swcString operator+(swcString &);
//Input and Output Functions
void swcString::swcDisplay(ostream & out) const; // Output Function
void swcString::swcRead(istream & in); // Input Function
private: //Private Data Members
vector<char> myString; // Vector of type char
}; // End of class
//Prototype of operator overloads
ostream & operator<<(ostream & out, const swcString & s);
istream & operator>>(istream & in, swcString & s);
int main( void ) //Begin of main Function
{
swcString s, two;
cout << "Enter a string: ";
cin >> s;
cout << "Enter a string: ";
cin >> two;
cout << "Add: " << s + two;
swcString s2(s);
swcString s3(s, 8);
swcString s4(s, 3, 5);
swcString s5(10, 'z');
cout << "s = " << s << endl
<< "s2 = " << s2 << endl
<< "s3 = " << s3 << endl
<< "s4 = " << s4 << endl
<< "s5 = " << s5 << endl;
cout << endl;
cout << "Size = " << s.swcSize() << endl;
return 0;
} //End of main Function
//Constructors for class swcString
swcString::swcString()
:myString(0)
{
}
swcString::swcString(char str[])
{
for (int i=0; str[i] != '\0'; i++)
{
myString.push_back(str[i]);
}
}
swcString::swcString(swcString str, int n)
{
for (int i=0; i < n; i++)
{
myString.push_back(str.myString[i]);
}
}
swcString::swcString(swcString str, int pos, int n)
{
for (int i=pos; i < (pos + n); i++)
{
myString.push_back(str.myString[i]);
}
}
swcString::swcString(int n, char ch)
{
for (int i=0; i < n; i++)
{
myString.push_back(ch);
}
}
//End of Constructors
//Begin Overload Functions
void swcString::swcDisplay(ostream & out) const
{
for (int i=0; i < myString.size(); i++)
{
out << myString[i];
}
}
void swcString::swcRead(istream & in)
{
char ch;
while (ch != '\n')
{
in.get(ch);
myString.push_back(ch);
}
}
ostream & operator<<(ostream & out, const swcString & s)
{
s.swcDisplay(out);
return out;
}
istream & operator>>(istream & in, swcString & s)
{
s.swcRead(in);
return in;
}
swcString swcString::operator+(swcString & str)
{
swcString one;
for (int i=0; i < myString.size(); i++)
{
one.myString.push_back(myString[i]);
}
for (i=0; i < str.swcSize(); i++)
{
one.myString.push_back(str.myString[i]);
}
return one;
}
//End Overload Functions
//Begin Accessors
int swcString::swcSize()
{
return myString.size() - 1;
}
int swcString::swcLength()
{
return myString.size() - 1;
}
bool swcString::swcEmpty()
{
return myString.empty();
}
//End Accessors