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
Solved

Overloading Operators

Posted on 1998-09-02
5
269 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
ID: 1171990
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
ID: 1171991
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
ID: 1171992
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
ID: 1171993
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
ID: 1171994
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 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