Solved

Function Overloading

Posted on 1998-09-01
6
223 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 100 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
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!

 
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

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

726 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