Overloading the + operator

This is a discussion on Overloading the + operator within the C++ Programming forums, part of the General Programming Boards category; Designing custom string class for school. This is what I have so far. How does it look? Ok great knew ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    Overloading the + operator

    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

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    You are storing a newline char in each swcString as you read it in with the >> operator. Then when you use the + operator it gets copied to the result. When the result is read char by char it outputs the newline char as directed.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    Ok, thanks. Thats exactly what I thought was happening. Now I just need to find a way to remove that from the vector . Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 01:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21