21xrx.com
2024-09-20 06:11:06 Friday
登录
文章检索 我的文章 写文章
用C++编写生日蛋糕代码
2023-06-22 21:54:52 深夜i     --     --
C++ 生日蛋糕 编程 代码 开发

  myCake.displayCake();

  cout << endl;

  cout << "Happy Birthday to You! May all your dreams come true!" << endl;

  cout << endl;

  cout << "  ___iiiii___ " << endl;

  cout << " ================ " << endl;

  cout << " | | | | | | | " << endl;

  cout << " | | | | | | | " << endl;

  cout << " | | | | | | | " << endl;

  cout << " ================ " << endl;

  cout << "^^^^^^^^^^^^^^^^^^" << endl;

  return 0;

style="text-indent:2em;">生日是每个人最重要的日子之一,而为TA准备一份生日蛋糕更是让TA感到温暖和幸福的表达方式。现在,我们来通过C++编写一个简单的生日蛋糕代码,为TA送上最美好的祝福!

首先,我们需要定义一个“Cake”类,该类包含以下类成员:cakeHeight(蛋糕高度)、cakeFlavor(蛋糕口味)、candleNum(蜡烛数量)、candleColor(蜡烛颜色)等属性。代码如下:


class Cake {

  private:

    int cakeHeight;

    string cakeFlavor;

    int candleNum;

    string candleColor;

  public:

    Cake (int height, string flavor, int num, string color)

      cakeHeight = height;

      cakeFlavor = flavor;

      candleNum = num;

      candleColor = color;

    

    void displayCake()

      cout << "A " << cakeHeight << " inch " << cakeFlavor << " birthday cake." << endl;

      cout << "With " << candleNum << " " << candleColor << " candles." << endl;

    

};

在这个“Cake”类中,我们定义了一个构造函数,并在这个函数中初始化了“cakeHeight”、“cakeFlavor”、“candleNum”和“candleColor”这些属性。另外,我们还定义了一个“displayCake()”方法,该方法用于输出蛋糕的属性信息。

接下来,我们实例化一个“Cake”对象,并调用“displayCake()”方法输出生日蛋糕的属性信息。代码如下:


int main() {

  Cake myCake(6, "chocolate", 18, "pink");

  myCake.displayCake();

  return 0;

}

在这个代码中,我们实例化了一个名为“myCake”的“Cake”对象,然后调用“displayCake()”方法,输出了生日蛋糕的属性信息。

最后,我们再添加一些额外的代码,用于输出生日祝福语和蛋糕图片,完善我们的生日蛋糕代码。代码如下:


int main() {

  Cake myCake(6, "chocolate", 18, "pink");

  myCake.displayCake();

  cout << endl;

  cout << "Happy Birthday to You! May all your dreams come true!" << endl;

  cout << endl;

  cout << "  ___iiiii___ " << endl;

  cout << " ================ " << endl;

  cout << " | | | | | | | " << endl;

  cout << " | | | | | | | " << endl;

  cout << " | | | | | | | " << endl;

  cout << " ================ " << endl;

  cout << "^^^^^^^^^^^^^^^^^^" << endl;

  return 0;

}

在这个代码中,我们输出了生日祝福语,并绘制了一个简单的蛋糕图案,让我们的生日蛋糕代码更加生动和有趣。

总的来说,通过C++编写一个简单的生日蛋糕代码,不仅可以实现蛋糕属性信息的自定义设置和输出,还能为TA送上最美好的祝福和温暖。让我们一起用代码点亮TA的生日吧!

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复