小白日常学代码 2020-05-25 14:14 采纳率: 100%
浏览 1665
已采纳

c++中string应该怎样用,应输入标识符是什么意思

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

class process
{
public:
    virtual void print() = 0;
};
class person :public process
{
private:
    string name;
    string gender;
    int age;

    person(string n, string g, int a)
    {
        name = n;
        gender = g;
        age = a;
    }
    virtual void print()
    {
        cout << "姓名:" << name << "性别:" << gender << "年龄:" << age << endl;
    }
};
class student :public person
{
public:

    int id;
    int score1;
    int score2;
    int score3;

    person(int i, int s1, int s2, int s3)
    {
        id = i;
        score1 = s1;
        score2 = s2;
        score3 = s3;
    }

    virtual void print()
    {
        cout << "学号:" << id << "语文成绩:" << score1 << "数学成绩:" << score2 << "英语成绩:" << score3 << endl;
    }

};
int main()
{
    person p("张三", "男", 20);
    student s(19060102, 100, 99, 98);
    process* pro1 = new person;
    pro1->print();
    process* pro2 = new student;
    pro2->print();
    system("pause");
    return 0;

}

```!![图片说明](https://img-ask.csdn.net/upload/202005/25/1590388783_830563.png)

有没有大佬看一下这个怎么改
  • 写回答

1条回答 默认 最新

  • threenewbee 2020-05-25 14:26
    关注

    class person(string n = 0,string g = 0,int a = 0) :
    构造函数不能写在类定义上

    #include<iostream>
    #include<string>
    using namespace std;
    
    class process
    {
    public:
        virtual void print() = 0;
    };
    class person: public process
    {
    private:
        string name;
        string gender;
        int age;
    public:
        person(string n = "默认名",string g = "男",int a = 0) 
        {
            name = n;
            gender = g;
            age = a;
        }
        virtual void print()
        {
            cout << "姓名:" << name << "性别:" << gender << "年龄:" << age << endl;
        }
    };
    class student :public person
    {
    private:
    
        int id;
        int score1;
        int score2;
        int score3;
    public:
        student(int i, int s1, int s2, int s3):person()
        {
            id = i;
            score1 = s1;
            score2 = s2;
            score3 = s3;
        }
    
        virtual void print()
        {
            cout << "学号:" << id << "语文成绩:" << score1 << "数学成绩:" << score2 << "英语成绩:" << score3 << endl;
        }
    
    };
    int main()
    {
        person p("张三", "男", 20);
        student s(19060102, 100, 99, 98);
        process* pro1 = new person("张四", "男", 21);
        pro1->print();
        process* pro2 = new student(19060103, 95, 80, 70);
        pro2->print();
    
    }
    

    姓名:张四性别:男年龄:21
    学号:19060103语文成绩:95数学成绩:80英语成绩:70

    问题解决的话,请点采纳。

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

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元