Kelvin Davis
Kelvin Davis

Reputation: 355

Constructor Initialization and private variables set inside constructor

My constructor is suppose to only take one variable. But I'm curious if you ca initialize other variables that arent in the constructor defintion.

class WordAnalysis{
private:
    int timesDoubled;
    word *words;
    int wordCount;
    int index;
    void doubleArrayAndAdd(string);
    bool checkIfCommonWord(string);
    void sortData();
public:
    bool readDataFile(char*); //returns an error if file not opened
    int getWordCount();
    int getUniqueWordCount();
    int getArrayDoubling();
    void printCommonWords(int);
    void printResult(int);
    WordAnalysis(int);
    ~WordAnalysis();

};

Example: Would any instance of WordAnalysis now have timesdoubled as 0. and would a getter function be able to get this information without a setter?

WordAnalysis::WordAnalysis(int arrSize){

wordCount = arrSize;
int timesDoubled = 0;   
int index = 0;
}

Upvotes: 1

Views: 17553

Answers (3)

Eric Lieber
Eric Lieber

Reputation: 23

I suggest defining a default constructor such as:

WordAnalysis()
{
   timesDoubled = 0;
    words[0] = '\0'; //assuming it's an array of char
    wordCount = 0;
    index = 0;
}

That way all instances of the class would be initialized.

Upvotes: 1

Azeem
Azeem

Reputation: 14667

Yes. You can. But, you can do in-class initialization of your data members on declaration. You should use initializer list with constructor to initialize your required data members. All the data members are visible inside the constructor. You can assign their values in it. Technically, using initializer list is initialization and inside the ctor it is assignment when the assignment operator (=) is used.

Here's is the snippet of your code with comments:

class WordAnalysis
{
private:

    // Data member initialization on declaration

    int    timesDoubled  { 0 };
    word*  words         { nullptr };
    int    wordCount     { 0 };
    int    index         { 0 };

public:

    // Initializing timesDoubled using initializer list

    WordAnalysis( const int t ) : timesDoubled{ t }
    {
        // Assign default values here if need be

        index = 10; // assignment
    }

    // ...
};

Your compiler should be at least C++11 compliant to allow the in-class initializations of data members.

Upvotes: 1

user5473227
user5473227

Reputation:

Well yes, you can initialize other member variables in the constructor, even if it doesn't take the corresponding arguments.

However, in the example you gave above:

WordAnalysis::WordAnalysis(int arrSize){

wordCount = arrSize;
int timesDoubled = 0;   
int index = 0;
}

You aren't actually initializing the timesDoubled member variables, because you wrote "int" before it, which is declaring a new variable and setting that to 0.

If you want to set the classes timesDoubled variable you have to write:

timesDoubled = 0;

Or if you want to be more explicit about it, you can even write:

WordAnalysis::timesDoubled = 0;

Upvotes: 4

Related Questions