开发者

C++ code snippet for a new baby greeting card

A friend of mine sent me this code snippet to celebrate his new baby birth:

void new_baby_name() { father_surname++; }

The snippet is from his point of view, he is the father and the new baby get the surname from him.

I answered with this:

class father_name {};
class mother_name {};
class new_baby_name: public father_name, public mother_na开发者_JS百科me {};

But I'm not fully satisfied of my answer...


The correct reply is:

Sleep(0);


class baby
{
public:
    vector<gene> genes;
    baby(baby* logical_father, baby* biological_mother, baby* other)
    {

        int i;
        if (other == null)
        {
            for (i = 0; i < logical_father->genes.size())
            {
                if (rand() > 0.5)
                {
                    genes.push_back(logical_father->genes[i]);
                }
                else
                {
                    genes.push_back(biological_mother->genes[i]);
                }
            }
        }
        else
        {
            for (i = 0; i < other->genes.size())
            {
                if (rand() > 0.5)
                {
                    genes.push_back(other->genes[i]);
                }
                else
                {
                    genes.push_back(biological_mother->genes[i]);
                }
            }
        }
    }
}

There are, of course, other methods for constructing a baby.


destroy Sanity();

May not run, may stack overflow. I am not good at c.


Just a small bugfix to avoid bad names:

class male {};
class female {};

class father_name {};
class mother_name {};

template <class gender>
class new_baby_name;

template <>
class new_baby_name<male>: public father_name {};

template <>
class new_baby_name<female>: public mother_name {};

Note that you have a serious problem if this should fire a compiler error ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜