• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Function Overloading

I have been given a program that I have to Build a class with one data member that holds 80 characters.  And use it to concatenate 5 strings.  My professor has been gone for a week and none of us(students) have a clue of what he wants done.  

Here is the beginning of main.

void main (void)
{
  CSafeString FirstName,
              LastName,
              Space,
              Fullname,
              Attribute;

           FirstName = "James"
           LastName = "Tice"

           printf("\n%s%s", FirstName.GetString(), LastName.GetString();

I also have to use the >, >=, <, <=, ==, and + operators as member functions.



this is just the beginning and if you can help me get started I think I can finish it.

Thank you
0
James5578
Asked:
James5578
  • 4
  • 2
1 Solution
 
James5578Author Commented:
Edited text of question
0
 
Answers2000Commented:
The question appears to be incomplete.  So if this is no help you should reject my answer, and more specifically explain what you need.

From the fragment you have given, you can deduce the following

1. CSafeString is probably the class your professor wants you to create.
2. It needs a constructor which can take no parameters
3. It needs storage for a string
4. It needs an operator= which can accept a const char * parameter which assigns a value to the class
5. It needs a GetString function which returns a const char * pointer to the string the class is holding
6. You said 80 chars which is a clue

Something like (referencing the above)

class CSafeString // #1
{
private:
   char data[80] ; // <-- or the type your professor wants #3, #6

public:
   CSafeString() // #2
   {
     strcpy( data, "" ) ;
   }

   CSafeString& operator=( const char * pData ) // #4
   {
      strcpy( data, pData ) ;
      return *this ;
   }

   const char * GetString() const // #5
   {
      return &data[0] ;
   }
} ;

Now that's about all I can you give based on the information you've provided (and I think I've gonna about as far as I should, to help you with your homework: we aren't really supposed to do it for you).

You need to look more carefully at _all_ time information your professor gave you.  For example, there's nothing in my comments above or in the fragment you gave which include concentation (though you mention this in your question).

One point,
0
 
Answers2000Commented:
The question appears to be incomplete.  So if this is no help you should reject my answer, and more specifically explain what you need.

From the fragment you have given, you can deduce the following

1. CSafeString is probably the class your professor wants you to create.
2. It needs a constructor which can take no parameters
3. It needs storage for a string
4. It needs an operator= which can accept a const char * parameter which assigns a value to the class
5. It needs a GetString function which returns a const char * pointer to the string the class is holding
6. You said 80 chars which is a clue

Something like (referencing the above)

class CSafeString // #1
{
private:
   char data[80] ; // <-- or the type your professor wants #3, #6

public:
   CSafeString() // #2
   {
     strcpy( data, "" ) ;
   }

   CSafeString& operator=( const char * pData ) // #4
   {
      strcpy( data, pData ) ;
      return *this ;
   }

   const char * GetString() const // #5
   {
      return &data[0] ;
   }
} ;

Now that's about all I can you give based on the information you've provided (and I think I've gonna about as far as I should, to help you with your homework: we aren't really supposed to do it for you).

You need to look more carefully at _all_ time information your professor gave you.  For example, there's nothing in my comments above or in the fragment you gave which include concentation (though you mention this in your question).

One point, based on the class name you gave, I'll assume your professor wants to check you don't overrun the buffer in the object (in my case "data").  I delibrately haven't put that in, so you still need to do some work there too.

Now the rest of the homework's up to you.  If you need _specific_ help you'll find plenty of people at Ex-Ex who'll be glad to give you pointers.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Answers2000Commented:
BTW sorry about the accidental double submit.  The text is the same in both versions
0
 
Answers2000Commented:
You're editted text came thru while I was typing the above


"I also have to use the >, >=, <, <=, ==, and + operators as member functions. "

You need to add members for these functions into the CSafeString class.   You have the syntax for an operator member from my operator=, so you should be able to attempt these.

For example == should return true if 2 strings are equal, and false if not.  You may care to look up the strcmp function to do string comparisons.

I think this is enough now.
0
 
James5578Author Commented:
Thank you Answers2000.  I deliberately left out information so I have a chance at learning this stuff.  I just needed a direction to go.  We are basically in a self learing situation and C++ is not a good self taught language.

Thanks again
James5578
0

Featured Post

Technology Partners: 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!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now