Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

friend classes/functions & overloading ops

Posted on 1998-08-07
9
Medium Priority
?
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 440 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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