Solved

friend classes/functions & overloading ops

Posted on 1998-08-07
9
211 Views
Last Modified: 2012-05-04
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

0
Comment
Question by:swright243
  • 7
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 220 total points
ID: 1169694
answers coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169695
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;
};

0
 
LVL 22

Expert Comment

by:nietod
ID: 1169696
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.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 22

Expert Comment

by:nietod
ID: 1169697
Can you write those 4 functions to start with?  If you have questions.  I check in about every hour.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169698
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).
0
 

Author Comment

by:swright243
ID: 1169699
Thank you for the help. I will start your advice and see what
I get.
S.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169700
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?
0
 

Author Comment

by:swright243
ID: 1169701
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.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1169702
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.  
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question