I/O in classes

1
2
3
4
5
6
7
8
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.

word.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream> 
#include "word.h"
using namespace std;

Word::Word(){}

void setWord(const string input)
{
	return null;
}


word.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string> 
#include "word.h" 
 
using namespace std; 
 
const int 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; 
} 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#ifndef _WORD_H_ 
#define _WORD_H_
 
#include <cstring> 
#include <string> 
#include <iostream> 
#include <iomanip> 
 
using namespace 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


Thank you!
1
2
3
4
5
6
void setWord(const string input)
{
    return null; //void functions don't return anything 
    //maybe you want to modify this->word
}
Word::Word(){} //where is the initialization?  
Last edited on
Read this: http://cplusplus.com/doc/tutorial/classes/

Ask what you don't understand :)
You mixed up the names in your post:
main.cpp should be word.h and vice versa.
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
?
Last edited on
Topic archived. No new replies allowed.