vendredi 24 juin 2016
specific operator overloading help c++ [duplicate]
This question already has an answer here:
Operator overloading
7 answers
I'm trying to overload operators in this program and I'm getting confused what to do next...?
I'm wanting to include all off the following:
Assignment operator (“=”).
Comparison operators (“<”, “<=”, “==”, “!=”, “>=”, “>”)
Concatenation operator (“&”).
Concatenation and assignment operator (“&=”)
Input operator (“>>”).
Output operator (“<<”).
And trying to meet these requirements:
Above items 1 through 4 should have versions that work with a String object and a “C” type string on the right of the operator.
Items 5 and 6 require a String object to the right of the operator.
Items 1 and 4 will require a String object on the left of the operator.
Items 2 and 3 should have versions that allow both a String object and a “C” type string on the left of the operator.
Item 3 does not modify the object to the left of the operator.
TestString.cpp:
#include <iostream>
using namespace std;
#include <memory.h>
#include <string.h>
#include "TestString.h"
#pragma warning (disable:4996)
TestString::TestString() // the :: is telling the compiler that this method belongs to the class TestString
{
NumChars = 0;
MaxSlots = NumChars;
pChar = new char[1];
pChar[0] = '';
}
TestString::TestString(const char Str[])
{
NumChars = strlen(Str);
MaxSlots = NumChars;
pChar = new char[NumChars + 1];
strcpy(pChar, Str);
}
TestString::TestString(const TestString & Str)
{
NumChars = Str.NumChars;
MaxSlots = NumChars;
pChar = new char[NumChars + 1];
strcpy(pChar, Str.pChar);
}
TestString::~TestString() // destructor
{
delete[] pChar;
}
int TestString::Compare(const TestString & Str) const
{
return strcmp(pChar, Str.pChar);
}
int TestString::Compare(const char Str[]) const
{
return strcmp(pChar, Str);
}
TestString & TestString::Copy(const TestString & Str)
{
if (this != &Str)
{
if (MaxSlots < Str.NumChars)
{
delete[] pChar;
MaxSlots = Str.NumChars;
pChar = new char[MaxSlots + 1];
}
else;
NumChars = Str.NumChars;
strcpy(pChar, Str.pChar);
}
else;
return *this; // if we are a method in a class, the word this means the address of the class object
}
/*
TestString & TestString::Concat(const TestString & Str)
{
pTemp = new char[NumChars + Str.NumChars + 1];
strcpy(pTemp, pChar);
strcat(pTemp, Str.pChar);
delete[]pChar;
pChar = pTemp;
return *this;
}
*/
TestString & TestString::Concat(const char Str[])
{
delete[] pChar;
NumChars = strlen(Str);
MaxSlots = NumChars;
pChar = new char[MaxSlots + 1];
return *this;
}
ostream & TestString::Display(ostream & out) const // this const means that I am not changing any members of the class I belong to
{
out << pChar;
return out;
}
ostream & TestString::DisplayLine(ostream & out) const
{
out << pChar << endl;
return out;
}
istream & TestString::Read(istream & in)
{
const int Guess(50);
char c;
char * pTemp;
NumChars = 0;
while ((pChar[NumChars] = in.get()) != 'n')
{
NumChars++;
if (NumChars >= MaxSlots)
{
MaxSlots += Guess;
pTemp = new char[MaxSlots + 1];
memcpy(pTemp, pChar, NumChars);
delete[] pChar;
pChar = pTemp;
}
else;
}
pChar[NumChars] = '';
return in;
}
TestString TestString::operator & (const TestString & Str) const
{
TestString Temp(*this); //making a temporary TestString and setting it to be a copy of the object we are a member of
Temp.Concat (Str);
return Temp;
}
TestString.h:
#ifndef TEST_STRING_H
#define TEST_STRING_H
class TestString
{
// friend ostream & operator << (ostream &, const TestString &); // friend says that this method
// has full access to the class members even though it is not a member itself
public:
TestString(); // known as a default constructor
explicit TestString(const char[]); // explicit says that this constructor cannot be used for automatic promotion
TestString(const TestString &); // known as a copy constructor
~TestString();
int Compare(const TestString &) const;
int Compare(const char[]) const;
TestString & Copy(const TestString &);
TestString & Concat(const TestString & Str);
TestString & Concat(const char Str[]);
ostream & Display(ostream & = cout) const;
ostream & DisplayLine(ostream & = cout) const;
int Length() const;
istream & Read(istream & = cin);
TestString & operator = (const TestString &);
// TestString & operator = (const char []);
bool operator == (const TestString &);
bool operator == (const char[]);
TestString operator & (const TestString &) const;
TestString operator &= (const TestString &) const;
private:
char * pChar;
int NumChars;
int MaxSlots;
};
main.cpp:
#include <iostream>
using namespace std;
#include "TestString.h"
void main()
{
TestString Str1;
TestString Str2("abcd");
TestString Str3(Str2);
int Result;
cout << "Testing Display" << endl;
Str2.Display ();
cout << "Testing DisplayLine" << endl;
Str2.DisplayLine ();
Result = Str2.Compare (Str3);
if (Result < 0)
{
Str2.Display();
cout << " comes before ";
Str3.Display();
cout << endl;
}
else
if (Result > 0)
{
Str3.Display();
cout << " comes before ";
Str2.Display();
cout << endl;
}
else
{
Str2.Display();
cout << " is the same as ";
Str3.Display();
cout << endl;
}
Result = Str2.Compare ("wxyz");
Str1.Copy (Str3);
Str1 = Str3;
Str1.operator = (Str3);
Str2.Copy (Str2);
cout << "Str1 contains " << Str1.Length () << " characters" << endl;
if (Str1 == Str2)
cout << "Equal" << endl;
else
cout << "Not equal" << endl;
if (Str1 == "abcd")
cout << "Equal" << endl;
else
cout << "Not equal" << endl;
if ("wxyz" == Str1)
cout << "Equal" << endl;
else
cout << "Not equal" << endl;
cout << "String 1 is " << Str1 << endl;
do {
cout << "Enter characters: ";
cin >> Str1;
cout << "You entered: " << Str1 << endl;
} while (!(Str1 == "abcd"));
cout << "Bye" << endl;
Str1 = Str2 & Str3; //concatenate Str2 and Str3 (do not change either one) and place into Str1
Str1 = Str2 &= Str3; //concatenate Str2 and Str3 and put into Str2 then into Str1
}
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire