m0_63530151 2022-01-01 23:32 采纳率: 40%
浏览 87
已结题

c++设计一个小狗类并测试

设计一个小狗类Dog,具有多个属性和方法如下图所示,请编写Dog类并用主函数测试:

img

其中Init()初始化3个属性,position初值为0,Speak() 屏幕打印“汪汪”,Run()每次调用都使position+1,Info()函数以字符串形式返回小狗的整体信息,格式自定。

  • 写回答

1条回答 默认 最新

  • 关注

    你题目的解答代码如下:

    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;
    class Dog
    {
        int age;
        string color;
        string type;
        int position=0;
    
    public:
        Dog()
        {
            cout << "创建一个Dog对象" << endl;
        }
        ~Dog()
        {
            cout << "销毁一个Dog对象" << endl;
        }
        void Init(int age,string color,string type)
        {
            this->age = age;
            this->color = color;
            this->type = type;
            this->position = 0;
        }
        void Speak()
        {
            cout << "汪汪" << endl;
        }
        void Run()
        {
            position++;
        }
        string Info()
        {
            stringstream ss;
            ss << "Dog Info." << endl;
            ss << "age:" << age << endl;
            ss << "color:" << color << endl;
            ss << "type:" << type << endl;
            ss << "position:" << position << endl;
            return ss.str();
        }
    };
    int main()
    {
        Dog d1;
        d1.Init(1,"white","哈士奇");
        d1.Speak();
        d1.Run();
        cout << d1.Info() << endl;
        return 0;
    }
    

    img

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 1月10日
  • 已采纳回答 1月2日
  • 创建了问题 1月1日

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗