Solved

Function Overloading

Posted on 1998-09-01
6
210 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to convert c++ code to Android App 3 108
Which IDE to use to begin C++ training? 5 65
PDF library for Delphi 2 128
Compile GLUT with Visual Studio 2015 1 171
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

809 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