So ok, I have been doing like java for a year and a half, and this is my lovely 4th day of C++, and I have already read up on pointers, dynamic memory and all that fun stuff. I do know what the header file class does. However I got this long ass header file:
#ifndef WORD_H
#define WORD_H
/* Your initial comment goes here */
#include <iostream>
#include <cstring>
using namespace std;
/* Substitute for string class: does operations on words */
class Word {
public:
Word (); // Default constructor
Word (const Word& other); // Copy constructor
~Word (); // Destructor
Word& operator = (const Word& other); // Assignment operator
bool operator > (const Word& other) const; // Comparison op.
Word operator + (const Word& other) const; // Addition operator
// Input and output operators
friend istream& operator >> (istream& ins, Word& w);
friend ostream& operator << (ostream& outs, const Word& w);
private:
int length;
string data;
};
// Default constructor: initializes a new Word object
// Word is not yet filled in, so set length to 0
// and data to the empty string
Word::Word () {
length = 0;
data = "";
}
// Copy constructor: initializes a Word object as a copy
// of another Word object
Word::Word (const Word& other) {
length = other.length; // copy length field
data = other.data; // copy data field
}
// Destructor: unnecessary for this class
Word::~Word () { }
// Assignment operator: copy one word to another
// If w1 and w2 are of type Word, this defines how to do
// w1 = w2
// w1 gets mapped onto "this" and w2 gets mapped onto "other".
//
// We need to specify how the assignment works
// and return the new w1.
Word& Word::operator = (const Word& other) {
// If this word and the other word are at the same address
// (that is, if they are the same object),
// just return a pointer to this object
if (this == &other) {
return *this;
}
// Otherwise, copy their lengths and data
length = other.length;
data = other.data;
// Return what "this" points to (w1 in our example)
return *this;
}
// Comparison operator: how does this work?
// Read the note on the assignment operator
bool Word::operator > (const Word& other) const {
if (length != other.length) {
return (length > other.length);
}
if (data > other.data) {
return true;
}
return false;
}
// Addition operator: concatenates 2 words and returns the result
// This defines how to do
// w1 + w2
// w1 gets mapped onto "this" and w2 gets mapped onto "other".
//
// This time, we don't want to change either w1 or w2.
// We want to return a new Word whose value is (w1+w2).
Word Word::operator + (const Word& other) const {
Word temp; // Store the result in a new Word
temp.length = length + other.length;
temp.data = data + other.data;
return temp;
}
// Input operator: read in a word from the console or a file
//
// This defines how to do
// cin >> w
// or
// myfile >> w
// by filling in w based on what is read in.
//
// This function is *not* a member of the Word class!
// It's a "friend" function, which means it has access to Word's stuff.
istream& operator >> (istream& ins, Word& w) {
// Try reading into a temporary string first
string buffer;
ins >> buffer;
if (ins.fail()) {
return ins;
}
w.length = buffer.length();
w.data = buffer;
return ins;
}
// Output operator: write out a word to the console or to a file
// See note on >> operator
ostream& operator << (ostream& outs, const Word& w) {
outs << w.data;
return outs;
}
#endif
Word w1, w2, w3;
What will be stored in the two data members of the object w1 if we execute the statement
cin >> w1;
Well, I have really no idea. Is there any way to just test this? Or do I have to go and read this giant hting? Because I really can't seem to figure this out.