Tasks
Constructor:
Your first programming task is to implement the default constructor: Word(); Your constructor must adhere to the following specifications:
* Your constructor will set the string stored in Word to NULL initially
* Initialize all the counters in the object to zero
How do I start? I've already created a constructor word.cpp but It's my first time working with I/O with classes. I do understand the basic of it-outputting text files with strings. Note that this isn't all that im doing, theres more but i wanted to start out with the first step.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include "word.h"
usingnamespace std;
constint MAX = 100;
int main ()
{
// Open file for input
ifstream input("words.txt");
string inword;
Word words[MAX];
// Intitialize word counter
int count = 0;
// Read until array is filled or end of file is found
while (count < MAX && input >> inword)
{
// Insert word into array
words[count].setWord(inword);
// Increment counter
count++;
}
// Close input file
input.close();
// Print report header
cout << setw (12) << left << "Word";
cout << setw (8) << right << "Vowels";
cout << setw (8) << right << "Const.";
cout << setw (8) << right << "Digits";
cout << setw (8) << right << "Special";
cout << endl;
// Loop through all words in array
for (int i = 0; i < count; i++)
{
// Print data for word
words[i].write(cout);
cout << endl;
}
return 0;
}
#ifndef _WORD_H_
#define _WORD_H_
#include <cstring>
#include <string>
#include <iostream>
#include <iomanip>
usingnamespace std;
class Word
{
public:
Word();
~Word();
void setWord(const string input);
void write(ostream &outstream) const;
private:
string word; // C-string to hold the word
int vowels; // vowel counter
int consonants; // consonant counter
int digits; // digit counter
int specialchars; // special character counter
};
// Function implementations will be placed in word.cpp
#endif
Sorry about the mix up.
@blackcoder41: What is word.cpp? Is it the constructor? How do I set the string stored in Word to NULL initially? And how do initialize all the counters in the object to zero ?