爱喝百事快乐水 2022-05-22 11:37 采纳率: 100%
浏览 25
已结题

关于#运算符#的问题,如何解决?


#include<iostream>
#include<string>
using namespace std;

class Person//基类
{
public:
    Person() = default;
    Person(string nam, int ag):name(nam),age(ag)//带参构造函数
    {
        cout << "基类已创建" << endl;
    }
    void PersonOut()
    {
        cout << "姓名:" << name << endl;
        cout << "年龄:" << age << endl;
    }
    ~Person() = default;
protected:
    string name;
    int age = 0;
};

class Student :public Person//派生类
{
public:
    Student() = default;
    Student(string nam, int ag, string cla, int ID) :Person(nam, ag), id(ID), classid(cla)//带参构造函数
    {
        cout << "派生已创建" << endl;
    }

    void StudentOut()
    {
        cout << "学生姓名:" << name << endl;
        cout << "学生年龄:" << age << endl;
        cout << "学生班级:" << classid << endl;
        cout << "学生ID:" << id << endl;
    }
protected:
    int id = 0;
    string classid;
};

int main()
{
    Student stu(" 张三 "21" 20测一 ",01);
    ///stu.PersonOut();
    stu.StudentOut();
    return 0;
}

img

  • 写回答

1条回答 默认 最新

  • 程序喵正在路上 后端领域新星创作者 2022-05-22 11:55
    关注

    Student类的构造方法没赋值,然后你创建对象的时候逗号打成中文字符了,帮你改好了,希望采纳支持一下博主

    #include<iostream>
    #include<string>
    using namespace std;
    
    class Person//基类
    {
    public:
        Person() = default;
        Person(string nam, int ag) :name(nam), age(ag)//带参构造函数
        {
            cout << "基类已创建" << endl;
        }
        void PersonOut()
        {
            cout << "姓名:" << name << endl;
            cout << "年龄:" << age << endl;
        }
        ~Person() = default;
    protected:
        string name;
        int age = 0;
    };
    
    class Student :public Person//派生类
    {
    public:
        Student() = default;
        Student(string nam, int ag, string cla, int ID) :Person(nam, ag), id(ID), classid(cla)//带参构造函数
        {
            this->id = id;
            this->classid = classid;
        }
    
        void StudentOut()
        {
            cout << "学生姓名:" << name << endl;
            cout << "学生年龄:" << age << endl;
            cout << "学生班级:" << classid << endl;
            cout << "学生ID:" << id << endl;
        }
    protected:
        int id = 0;
        string classid;
    };
    
    int main()
    {
        Student stu(" 张三 ",21," 20测一 ", 01);
        ///stu.PersonOut();
        stu.StudentOut();
        return 0;
    }
    

    img

    img

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

报告相同问题?

问题事件

  • 系统已结题 5月30日
  • 已采纳回答 5月22日
  • 创建了问题 5月22日

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记