Solved

friend classes/functions & overloading ops

Posted on 1998-08-07
9
204 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now