Solved

Function Overloading

Posted on 1998-09-01
6
217 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
  • 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
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

761 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