#ifndef WORD_
#define WORD_
usingnamespace std;
#include <iostream>
class Word
{
public:
// Capacity of a Word
//
staticconstunsignedint MAX = 32;
private:
unsigned Length; // Number of characters in Word
char Mem[MAX]; // Memory to hold characters in Word
public:
// Construct empty Word
//
Word() { Length = 0; }
// Reset Word to empty
//
void reset() { Length = 0; }
// Return status information
//
bool empty() const { return Length == 0; }
bool full() const { return Length == MAX; }
unsigned length() const { return Length; }
unsigned maximum() const { return MAX; }
// Return reference to element I
//
char& operator[]( unsigned I ) { return Mem[I]; }
// Return constant reference to element I
//
constchar& operator[]( unsigned I ) const { return Mem[I]; }
// Construct Word by copying array of characters
//
Word( constchar [] );
// Copy Word to the current Word
//
Word& operator=( const Word& );
// Append Word to the current Word
//
Word& operator+=( const Word& );
};
// Return Word which is the concatenation of two Words
//
Word operator+( const Word&, const Word& );
// Compare two Words (equality and relational operators)
//
booloperator==( const Word&, const Word& );
booloperator!=( const Word&, const Word& );
booloperator< ( const Word&, const Word& );
booloperator<=( const Word&, const Word& );
booloperator> ( const Word&, const Word& );
booloperator>=( const Word&, const Word& );
// Insert Word into stream
//
ostream& operator<<( ostream&, const Word& );
// Extract Word from stream
//
istream& operator>>( istream&, Word& );
#endif
Here are the function definitions that I've tried to write:
/*-----------------------------------------------------
Name: copy constructor for class "Word"
Purpose: Initialize the letters inside a Word object
Return: The boolean value "true" if word is empty
-----------------------------------------------------*/
Word::Word( constchar Existing[])
{
Mem[Length] = Existing[Length];
}
/*-----------------------------------------------------
Name: Assignment operator for class "Word"
Purpose: Copy the contents of one word into another
Receive: The word which is to be copied
Return: The word
---------------------------------------------------*/
Word& Word::operator=( const Word& Existing)
{
Length = Existing.Length;
Mem[MAX] = Existing.Mem[MAX];
}
/*-----------------------------------------------------
Name: operator+=
Purpose: Append a word to the current word
Receive: A word
Return: Concatenation
-----------------------------------------------------*/
Word& Word::operator+=( const Word& Existing)
{
Length = Length + Existing.Length;
char ExistingTemp = Existing.Mem[Length];
char Temp[Length];
//Temp = Mem + ExistingTemp;
return Existing.Mem[Length];
}
/*-----------------------------------------------------
Name: operator+
Purpose: Concatenate 2 words together
Receive: 2 words
Return: Concatenation of the 2 words
-----------------------------------------------------*/
Word operator+(const Word& One, const Word& Two)
{
Word.Mem[Length] = One.Mem[Length] + Two.Mem[Length];
return Word;
}
I've tried defining the basic functions, but I'm totally confused. I've been trying to work on this with some other people, but they're just as lost as I am.
We just don't know how to call the actual strings when we want to add/concatenate them.