Solved

Overloading Operators

Posted on 1998-09-02
5
264 Views
Last Modified: 2010-04-10
I need to execute operator overloading functions and don't know how to set them up?
Below is my class App file and cpp file in that order. I do not understand how to use the functions from the class. Nor do I have a clue as to how the 2 different cpp files interact with each other.
any insight into this would be greatly appreciated.

// SafeString.h: interface for the CSafeString class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_SAFESTRING_H__C6FEFAE3_42A2_11D2_AE4A_30620FC10000__INCLUDED_)
#define AFX_SAFESTRING_H__C6FEFAE3_42A2_11D2_AE4A_30620FC10000__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

class CSafeString  
{

private:
      char m_strString[81];

public:
      CSafeString();
      {
            strcpy(m_strString," ");
      }

       ~CSafeString();

      CsafeString(const CSafeString &aString);
      int operator > (const CSafeString &aString);
      int operator >= (const CSafeString &aString);
      int operator < (const CSafeString &aString);
      int operator <= (const CSafeString &aString);
      int operator == (const CSafeString &aString);
      CSafeString & operator = (const CSafeString & aString);
      void operator = (char * strString);
      CSafeString operator + (const CSafeString & aString);
      char * GetString(void);

};

#endif // !defined(AFX_SAFESTRING_H__C6FEFAE3_42A2_11D2_AE4A_30620FC10000__INCLUDED_)

// SafeString.cpp: implementation of the CSafeString class.
//
//////////////////////////////////////////////////////////////////////

#include "SafeString.h"
#include <stdio.h>

void main (void)
{//First physical line of code
      CSafeString FirstName,
                        LastName,
                        Space,
                        FullName,
                        Attribute;

      FirstName = "James";
      LastName = "Tice";

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

      Space = " ";

      Attribute = "The greatest programmer ever!";

      FullName = FirstName + Space + LastName + Space + Attribute;

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


      if(FirstName<LastName);
      {
            printf("\n\n%s", FirstName.GetString(),LastName.GetString());
      }

      else
      {
            printf("\n\n%s is not < %s", FirstName.GetString(), LastName.GetString());
      }

      if(FirstName <= LastName)
      {
            printf("\n\n is <= %s",FirstName.GetString(),LastName.GetString());
      }
      
      else
      {
            printf("\n\n is not <= %s",FirstName.GetString(),LastName.GetString());
      }

      if(FirstName > LastName)
      {
            printf("\n\n is > %s",FirstName.GetString(),LastName.GetString());
      }

      else
      {
            printf("\n\n is not > %s",FirstName.GetString(),LastName.GetString());
      }
      if(FirstName >= LastName)
      {
            printf("\n\n is >= %s",FirstName.GetString(),LastName.GetString());
      }

      else
      {
            printf("\n\n is not >= %s",FirstName.GetString(),LastName.GetString());
      }
      if(FirstName == LastName)
      {
            printf("\n\n is == %s",FirstName.GetString(),LastName.GetString());
      }

      else
      {
            printf("\n\n is not == %s",FirstName.GetString(),LastName.GetString());
      }

      Attribute = " Wonderful, Kind, WarmHearted, Awesomely Intelligent,
            Grossly UnderPaid but Talented Programmer";

      FullName = FirstName + Space + LastName + Space + Attribute;


}// Last physical line of code

// THIS IS WHERE ALL THE FUNCTIONS MUST BE PLACED I DON'T
// KNOW HOW TO SET THEM UP BASED ON THE PROTOTYPES IN THE // CLASS.

#include <stdio.h>
#include "SafeString.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSafeString::CSafeString(const CSafeString &aString)
{
      strcpy (m_strString, aString.m_strString);
}

CSafeString::~CSafeString()
{
}

0
Comment
Question by:James5578
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pagladasu earned 100 total points
Comment Utility
Hello out there. You seem to be all out of sorts with operator
overloading. Actually the ideal way to handle the situation is
to have 3 files
1.      SafeString.h -       the interface of the CSafeString class
2.      SafeString.cpp-      the implementation of CSafeString class
                  You must include its header file.
3.      App.cpp            -      This could be your main application file
                  containing main and actual usage of your
                  class instance. Rememeber to include the
                  header file containing the class's declaration.

I have checked your code and have tried to offer a solution in the
source code itself. Have a look into it.

PaglaDasu
      
// SafeString.h: interface for the CSafeString class.
//
 
#include <string.h>
class CSafeString
{
private:
 char m_strString[200];//size increased
                  //to accomodate larger strings
 public:
   CSafeString()
       {
             strcpy(m_strString," ");
       }

   ~CSafeString();
   CSafeString(const CSafeString &aString);
   int operator > (const CSafeString &aString);
   int operator >= (const CSafeString &aString);
   int operator < (const CSafeString &aString);
   int operator <= (const CSafeString &aString);
   int operator == (const CSafeString &aString);
   CSafeString & operator = (const CSafeString & aString);
   void operator = (char * strString);
   CSafeString operator + (const CSafeString & aString);
   char * GetString(void);
};



// SafeString.cpp: implementation of the CSafeString class.
//
//////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "SafeString.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//this is the copy constructor
CSafeString::CSafeString(const CSafeString &aString)
{
 strcpy (m_strString, aString.m_strString);
}

CSafeString::~CSafeString()
{
 //nothing here - 'cos no dynamic allocation is being done
}

int CSafeString::operator > (const CSafeString &aString)
{
    return ((strcmp(m_strString,aString.m_strString))>0);
}

int CSafeString::operator >= (const CSafeString &aString)
{
    return ((strcmp(m_strString,aString.m_strString))>=0);
}

int CSafeString::operator < (const CSafeString &aString)
{
    return ((strcmp(m_strString,aString.m_strString))<0);
}

int CSafeString::operator <= (const CSafeString &aString)
{
    return ((strcmp(m_strString,aString.m_strString))<=0);
}

int CSafeString::operator == (const CSafeString &aString)
{
    return (!strcmp(m_strString,aString.m_strString));
}

CSafeString & CSafeString::operator = (const CSafeString & aString)
{
    strcpy(m_strString,aString.m_strString);
    return *this;
}

void CSafeString::operator = (char * strString)
{
    strcpy(m_strString,strString);
}

CSafeString CSafeString::operator + (const CSafeString & aString)
{
    CSafeString temp;
    strcpy(temp.m_strString,m_strString);
    strcat(temp.m_strString,aString.m_strString);
    return temp;
}

char * CSafeString::GetString(void)
{
    return m_strString;
}


//This file should be app.cpp or
//whatever you want your project file to be named

#include "SafeString.h"
#include <stdio.h>

void main (void)
{//First physical line of code
 //There are some changes(cosmetic), corrections and remarks added
 CSafeString FirstName,LastName,Space,FullName,Attribute;

 FirstName = "James";//equivalent to calling FirstName.operator=("James")
 LastName = "Tice";
 printf("\n%s%s\n", FirstName.GetString(), LastName.GetString());

 Space = " ";
 Attribute = "The greatest programmer ever!";
 
 //the next line calls
 //overloaded + and oveloaded = opeerator functions
 FullName = FirstName + Space + LastName + Space + Attribute;
 printf("\n\n%s", FullName.GetString());

 //equivalent to calling FirstName.operator<(LastName)
 if(FirstName<LastName)
 {
 printf("\n\n%s is < %s\n", FirstName.GetString(),LastName.GetString());
 }
 else
 {
 printf("\n\n%s is not < %s", FirstName.GetString(), LastName.GetString());
 }

 //equivalent to calling FirstName.operator<=(LastName)
 if(FirstName <= LastName)
 {
 printf("\n\n%s is <= %s",FirstName.GetString(),LastName.GetString());
 }
 else
 {
 printf("\n\n%s is not <= %s",FirstName.GetString(),LastName.GetString());
 }

 //equivalent to calling FirstName.operator>(LastName)
 if(FirstName > LastName)
 {
 printf("\n\n%s is > %s",FirstName.GetString(),LastName.GetString());
 }
 else
 {
 printf("\n\n%s is not > %s",FirstName.GetString(),LastName.GetString());
 }

 //equivalent to calling FirstName.operator>=(LastName)
 if(FirstName >= LastName)
 {
 printf("\n\n%s is >= %s",FirstName.GetString(),LastName.GetString());
 }
 else
 {
 printf("\n\n%s is not >= %s",FirstName.GetString(),LastName.GetString());
 }

 //equivalent to calling FirstName.operator==(LastName)
 if(FirstName == LastName)
 {
 printf("\n\n%s is == %s",FirstName.GetString(),LastName.GetString());
 }
 else
 {
 printf("\n\n%s is not == %s",FirstName.GetString(),LastName.GetString());
 }

 Attribute = "Wonderful,Kind,WarmHearted,Awesomely Intelligent,Grossly UnderPaid but Talented Programmer";

 FullName = FirstName + Space + LastName + Space + Attribute;
 printf("\n\n%s",FullName.GetString());

 }// Last physical line of code

0
 

Author Comment

by:James5578
Comment Utility
Thank you I'll give it a try.  This is a really hard concept to grasp.  I'm a new c/c++ programmer and it's not coming very easy.
I would also like to know if there are any good, basic OOPs books on the market.  I have bought many and all have great information but to solve one problem I have to look in 4 or 5 books. Is there a catch all book available for the basics.

Thank you again
0
 
LVL 4

Expert Comment

by:pagladasu
Comment Utility
Are you on the lookout for a good book covering OOP with C++.
Try Herbert Schildt's "C++ The Complete Refernce"

0
 

Author Comment

by:James5578
Comment Utility
I have a few questions about the code.

#1 What is the ampersand used for in this case?
            V
CSafeString & CSafeString::operator = (const CSafeString & aString)
{
    strcpy(m_strString,aString.m_strString);
    return *this;
}


#2 Can you explain the Following return? I know what it is doing but no sure how it really works.

    return (!strcmp(m_strString,aString.m_strString));

#3 Same Thing?
return ((strcmp(m_strString,aString.m_strString))>=0);

I was able to do the same thing but it required an if, else statement. I would like to know how this works?
Thank you


0
 
LVL 4

Expert Comment

by:pagladasu
Comment Utility
Hello,
The ampersand out there specify that the function is returning a reference to a CSafeString object. The return type of that function is a reference data type.

Regarding the second question, strcmp returns 0 if the 2 strings are equal. So using a ! operator will make the return value 1 if the 2 strings are equal.
The next instance that you specified would return 1 if the first string is greater or equal to the second string.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 clear a vector as well as how to detect empty vectors in C++.
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now