Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Overloading Operators

Posted on 1998-09-02
5
Medium Priority
?
276 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
[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
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
pagladasu earned 400 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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 …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.
Suggested Courses

636 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