?
Solved

Function Overloading

Posted on 1998-09-01
6
Medium Priority
?
229 Views
Last Modified: 2011-10-03
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
Comment
Question by:James5578
[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
  • 4
  • 2
6 Comments
 

Author Comment

by:James5578
ID: 1171808
Edited text of question
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1171809
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
 
LVL 8

Accepted Solution

by:
Answers2000 earned 400 total points
ID: 1171810
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
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!

 
LVL 8

Expert Comment

by:Answers2000
ID: 1171811
BTW sorry about the accidental double submit.  The text is the same in both versions
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1171812
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
 

Author Comment

by:James5578
ID: 1171813
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

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