Solved

friend classes/functions & overloading ops

Posted on 1998-08-07
9
212 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

821 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