Link to home
Start Free TrialLog in
Avatar of swright243
swright243

asked on

friend classes/functions & overloading ops

I need some help with this code. I need to operate on the following lines:

string x = "0123456789";
string x25 = x(2,4)  // creates "2345"
string x69 = x(6); // creates "6789";
string x57 = x(-5,3);  // creates "567"
string x29 = x25 + x69; // creates "23456789"

A negative index is taken relative to the end of the string, and a missing second argument
meams to go to the end of the string. If the left index is past the upper bound
of the string, then a zero length can be returned.

This needs to have an array of strings which can then be sorted using the
ordering operators.


This is the header file I developed:

class String {

public:


      // string concatenation
      friend String operator+( const String& lhs, const String& rhs );
      // Comparison - equality
      friend bool operator==( const String& lhs, const String& rhs );
      // Comparison - inequality
      friend bool operator!=( const String& lhs, const String& rhs );
      // Ordering
      friend bool operator>( const String& lhs, const String& rhs );

      // substring - overloading the () - function call operator
      const String String::operator() (int pos, int count);

   void poscount(int pos, int count);

private:

      int size_of_string;

};


This is the "()" overload function I have "tried" to create...............

// overload the function call operator to provide a substring.
const String String::operator() (int pos, int count)
{
poscount(pos,count);
String str;
return(str);
}

void String::poscount(int pos, int count)
{
/*   This function is incomplete.........
     it is used within the overloaded function operator: ()

       if (pos < 0 || pos > length() - 1 ||
            count <= 0 || count + pos > length() )

            throw StringError(pos, count, *this); */

}


Can someone please explain how to write these "friend" functions?
Are they written as classes or functions?

S

ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

This is probably going to take a couple of communications back and forth between us.  

First of all I see that the class stores a string size (Size_of_String), but it doesn't have a way to store the string.  You need a pointer to the array of characters somewhere.  Like

class String {
   public:

// removed for space savings.

   private:
   int size_of_string;
  char *StrPtr;
};

I'm not sure what to tell you about the other functions yet.  I don't think you are read for them.  You need to have a working constructor (or constructorS) and destructor first.  Then worry about those other functions.  

I would start with the following 4 functions.
1.  A default constructor.  This takes no parameters and stores an empty string (0 length).
2.  A constructor from a "const char *"  This creates a String that stores a copy of the string specified in the parameters.
3.  A copy constructor.
4.  A destructor.
Can you write those 4 functions to start with?  If you have questions.  I check in about every hour.
I'm getting done for the day.  I might be able to check in one more time.  I will check a few times over the weekend.  (actually probably a lot this weekend).
Avatar of swright243

ASKER

Thank you for the help. I will start your advice and see what
I get.
S.
I will be working most of the day and will check in every hour or two.  You can e-mail me at nietod@theshop.net for (potentially) faster results.  Do you have a deadline?  when?
Hi nietod,
I will Email my instructor later today to find out what the deadline is.
I think it is in September, but whether it is and if so what day, I don't recall.
S.
Good, is that for all these things?  You've got SO MUCH going on it will take time.  (I think it would take me a solid day to do these things and I'd be a lot faster than you.)  Its good you sought help in time.  Most students wait until the last minute to ask for help.  "I need XXXX by tommorow at 8:00...."  If you have the assignment(s) in electronic form, it might help to e-mail copies to me.