Thread: String Class

  1. #91
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Going back to your operator[], you fixed the wrong thing. I'm guessing you tried it and an exception was thrown? You have to try to understand why that happens. When I said it was obvious, I didn't mean it would necessarily be obvious when you first look at it (otherwise you wouldn't have coded it that way), I just meant that once you realized what it was it would make sense. It sounds like you still don't know what the flaw was.

    So your fix was to return sink instead of throwing an exception in the first if statement. That wasn't where the problem was. The problem was the if. Pick a number and run it through the if and see where you get. You should always do this in your mind or on paper to test the if to see if it does what you want. Let's say the stringlength is 8 and the user wants the character at position 3. Follow your if in the earlier code and see what gets executed.

    >> isn't printf used in C++?
    It can be, but cout is generally used instead.

  2. #92
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    I made some changes:

    Code:
    char& String::operator [](int i) const 
    {
    	if(i < 0 || i> stringlength)
    		return sink;
    	else
    		return buf[i];
    }
    
    char& String::operator [](int i)
    {
    	if(i < 0 || i>stringlength)
    		return sink;
    	else
    		return buf[i];
    }
    Also, how do I make a +operator() that acts as a unary operator for the string class.

  3. #93
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You mean like
    Code:
    String String::operator+ (const String & other);
    *edit* Often, such operators are better declared not as members, so that
    you could group those functions with others of similar type -- namely:
    Code:
     String operator+ (const String & left, const char* right);
    Last edited by CodeMonkey; 08-06-2007 at 11:28 PM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #94
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by CodeMonkey View Post
    You mean like
    Code:
    String String::operator+ (const String & other);
    I think so.

  5. #95
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    I am really stuck on how to do the unary operator. I do not know how to define it. The unary operator should look like this:

    Code:
    String operator+() const;
    What its supposed to do is if there are some modifications that are to be done to a String object like make it all lowercase then the function should return a new object with all the modifications (for example all lowercase letters) and the original object should have no modifications done to it. I am really stumped on this. Can anybody help?

  6. #96
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> I made some changes
    That looks much better.

    >> how do I make a +operator() that acts as a unary operator for the string class.
    Step by step. It is a unary operator. operator++ is also a unary operator, so you can use that as an example of how to make the function declaration.

    >> the original object should have no modifications done to it.
    This means you need to make a copy of your current object, then modify the copy, and then return it. Give that a go and show us what you come up with.

  7. #97
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    >>This means you need to make a copy of your current object, then modify the copy, and then return it. Give that a go and show us what you come up with.

    I don't get how to do that if the unary operator takes no arguments.

  8. #98
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by BKurosawa View Post
    >>This means you need to make a copy of your current object, then modify the copy, and then return it. Give that a go and show us what you come up with.

    I don't get how to do that if the unary operator takes no arguments.
    The only code that I can think of is this, but I don't think it is right:

    Code:
    String String::operator +() const
    {
    	return *this;
    }

  9. #99
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    >>Step by step. It is a unary operator. operator++ is also a unary operator, so you can use that as an example of how to make the function declaration.

    And now that I think of it I don't think my ++operators are right either:

    Code:
    String& String::operator ++()
    {
    	for(int i = 0; i<stringlength; i++)
    		++buf[i];
    	return *this;
    
    }
    
    
    String String::operator ++(int x)
    {
    	String temp = *this;
    	++*this;
    	return temp;
    
    }

  10. #100
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> The only code that I can think of is this, but I don't think it is right:
    That code for your operator+ is a good start. Now you have to add code to make a copy of *this, and then add code to change the copy. Finally, you should return the copy, not *this.

    In your operator++(int x), you make a copy called temp and you return temp. The difference is you are (correctly) modifying this. In your operator+, you are not supposed to modify this.

    I don't see anything immediately wrong with your operator++ implementations.

  11. #101
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    >> then add code to change the copy.
    The thing is I don't know how the copy is supposed to change. The change is arbitrary. It could be making the string upper or lower case or it could be something else.

  12. #102
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I think you have to decide what you want your operator+ to do. There's no way to tell a unary operator+ to do different things at different times. Perhaps operator+ is supposed to make it all upper case and the unary operator- is supposed to make it all lower case?

    This all sounds like exercises in writing different operators. Many of these operators are not intuitive, and you would not actually use them in real code. So it doesn't matter so much what you have them doing. The point is that they do something.

  13. #103
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    I think you have to decide what you want your operator+ to do. There's no way to tell a unary operator+ to do different things at different times. Perhaps operator+ is supposed to make it all upper case and the unary operator- is supposed to make it all lower case?

    This all sounds like exercises in writing different operators. Many of these operators are not intuitive, and you would not actually use them in real code. So it doesn't matter so much what you have them doing. The point is that they do something.
    In that case, based on the below test what does it look like the unary operator does?

    Code:
    void test12() {
    
        system("cls");
    
        cout << "12. Testing: String unary operator." << endl << endl;
    
        csis << "12. Testing: String unary operator." << endl << endl;
    
        String s12("Unary +");
    
        String t12(+s12);
    
        s12.setName("s12");
    
        t12.setName("t12");
    
        s12.print();
    
        t12.print();
    
        s12 = +s12;
    
        s12.print();
    
        wait();
    
    }

  14. #104
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That test doesn't indicate what the unary operator+ does. Just that it does something. I think making the string uppercase makes as much sense as anything else.

  15. #105
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Do you know how to convert to upper case letters? Is there a predefined function that does that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM