Sebastian Zander
Sebastian Zander

Reputation: 347

How to declare and initialize static constant strings in classes?

I'm writing a Yatzy program for c++ class. We are supposed to print the value of different dice using std::cout. What I want to do is so save a constant string as then just that constant to print the dice out, so rather than using:

std::cout << "-------\n|     |\n|  *  |\n|     |\n-------\n"

I want to have a constant string with that value and do this:

std::cout << theConstantString;

Generic programming wins again!

-------
|     |
|  *  |
|     |
-------

The solution I have for it seems suboptimal to me. This is the relevant code:

YatzyIO.h

class YatzyIO
{
    private:

    // Define die constants
    static const std::string dieOnePrint;
    static const std::string dieTwoPrint;
    static const std::string dieThreePrint;
    static const std::string dieFourPrint;
    static const std::string dieFivePrint;
    static const std::string dieSixPrint;


    void dieOne();
    void dieTwo();
    void dieThree();
    void dieFour();
    void dieFive();
    void dieSix();

};
(there are more code than that in there, I just cut anything that wasn't relevent, which I assume I'm supposed to anyway)

Now, outside the implementation of any function in YatzyIO.cpp:

const std::string YatzyIO::dieOnePrint = "-------\n|     |\n|  *  |\n|     |\n-------\n";
const std::string YatzyIO::dieTwoPrint = "-------\n|  *  |\n|     |\n|  *  |\n-------\n";
const std::string YatzyIO::dieThreePrint = "-------\n| *   |\n|  *  |\n|   * |\n-------\n";
const std::string YatzyIO::dieFourPrint = "-------\n| * * |\n|     |\n| * * |\n-------\n";
const std::string YatzyIO::dieFivePrint = "-------\n| * * |\n|  *  |\n| * * |\n-------\n";
const std::string YatzyIO::dieSixPrint = "-------\n| * * |\n| * * |\n| * * |\n-------\n";

And finally in YatzyIO.ccp I have these functions:

void YatzyIO::dieOne()
{
    //output die side 1
    std::cout << dieOnePrint;
}

(+1 for each die, and it looks similar)

This is only lab 2 of 3 I've finished so far, the third replaces these consts with arrays. I was graded on lab 1, which contain this code as well and my teacher said (and I'm translating here since I'm swedish, so whatever is lost in translation I'm sorry!):

"It's good that you use member variable so save the different dice output. I suggest you save the different lines (that form the different output) in your member variables instead."

What does he mean? Is there a better way to do it? I can't initizalize non-integer constants in the header file. I've tried a bunch of different way, because honestly my solution seems not so optimal to me.

Upvotes: 0

Views: 698

Answers (1)

Your teacher is probably suggesting that all different outputs are actually the combination of a small subset of lines, and that you could just maintain those lines and compose the solution as needed, rather than maintaining the whole dice image.

For example -------\n appears as the top and bottom borders for each and every different combination, | * * |\n can be used to generate the top and bottom lines of 4, 5 and 6, and also the middle element of 6. | * |\n can be the middle line for 1, 3, 5 and you will need an empty line for the top and bottom lines of 1, and for the middle lines in 2 and 4... You can store those as members and then generate the dice drawings based on those primitives.

For example, to draw a 6 you need to do:

std::cout << line        // -------
          << two_dots    // | * * |
          << two_dots    // | * * |
          << two_dots    // | * * |
          << line;       // -------

This way you only need to store the 4 primitives rather than all of the complete dice values.

Upvotes: 1

Related Questions