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.

Recommended Answers

All 5 Replies

It's never a bad idea to know what your code is doing. However, you don't necessarily have to run it yourself to see (which you could do by making a main() method in a new .cpp file and #include "word.h").

Take a look at the overridden >> operator. Normally the non-overridden method functions to get input from a stream and place it into a variable. However, you're trying to read data into an object of your class (w1). If that's the case what does cin >>w1 do? Should it just pick one of the objects members to the input and try it's luck? No, you've given it very specific instructions on what to do. Try to figure it out from just this (from your header):

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;
}

Active code really doesn't belong in a header. Prefer to break it up like this:

// word.h
#ifndef WORD_H
#define WORD_H
/* Your initial comment goes here */

#include <iostream>
using std::istream;
using std::ostream;
#include <string>
using std::string;

/* 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;
};

#endif
// word.cpp
#include "word.h"

// 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;
}

And then you can test the code something like this.

// main.cpp
#include <iostream>
using std::cin;
#include "word.h"

int main()
{
   Word w1, w2, w3;
   cin >> w1;
}

Let us know if separate compilation is an issue for you.

Excellent point DS. I was assuming that this came prepackaged as an assignment but I probably should have said something.

It's never a bad idea to know what your code is doing. However, you don't necessarily have to run it yourself to see (which you could do by making a main() method in a new .cpp file and #include "word.h").

Take a look at the overridden >> operator. Normally the non-overridden method functions to get input from a stream and place it into a variable. However, you're trying to read data into an object of your class (w1). If that's the case what does cin >>w1 do? Should it just pick one of the objects members to the input and try it's luck? No, you've given it very specific instructions on what to do. Try to figure it out from just this (from your header):

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;
}

Before I even looked at that, I just went into visual studio 2010...

I tried compiling it first but then got an error, I don't know if this is what I need but I just did
include <string> and that let it build.

Here is word.cpp

// word.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "word.h"
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{


Word w1, w2, w3; 
cin >> w1;
cout << w1 << endl;
int c;
cin >> c; 
 

	return 0;
}

It outputted the word hello after I typed it in, I think I am doing it wrong.

(I added the #include <string> to both the word.h and word.cpp file)
(And I also thought the way to over ride something was using type_def?

Well the code is doing what it's supposed to do, but I don't understand this, why are you doing this on your 4th day of C++ ? If it's just a matter of this piece of code then may be it's ok but if this is how you are trying to learn C++, you are probably trying to make big leaps without understanding the basics.

Ans:
Operators >> and << are overloaded for your class type. when you typed 'hello' it got stored in the 'data' member variable of the class and when you do a cout, the overloaded operator returns the value of the same 'data' member variable.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.