旭辉夜夜访 2022-03-07 15:43 采纳率: 100%
浏览 28
已结题

C++继承与派生string相关

这个主函数怎么不能匹配调用啊

img


#include<iostream>
#include<string>
using namespace std;
class Person
{
       private:
                string *name;
                int id;
       public:
                Person(string *name1,int id1);
                void Display();
                ~Person();
};

Person::Person(string *name1,int id1)
{
        name=name1;
        id=id1;
}
void Person::Display()
{
        cout<<"Name:"<<name<<endl;
        cout<<"ID:"<<id<<endl;
}
Person::~Person()
{
        delete [] name;
}

class CollegeStu : public Person
{
        private:
                string *major;
                double score;
        public:
                CollegeStu(string *name,int id,string *major1,double score1);
                void Display();
};
CollegeStu::CollegeStu(string *name,int id,string *major1,double score1) : Person(name,id)
{
        major=major1;
        score=score1;
}
void CollegeStu::Display()
{
        Person::Display();
        cout<<"Major:"<<major<<endl;
        cout<<"C++ Score:"<<score;
}
int main()
{
       string name,major;
       string id;
       double score;
       cin>>name>>id>>major>>score;
       CollegeStu cs(name,id,major,score);
       cs.Display();
       return 0;
}

  • 写回答

5条回答 默认 最新

  • _GX_ 2022-03-07 16:32
    关注
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class Person
    {
    private:
        string _name;
        int _id;
    
    public:
        Person(const string &name, int id);
        ~Person();
    
        void Display() const;
    };
    
    Person::Person(const string &name, int id) : _name(name), _id(id)
    {
    }
    
    Person::~Person()
    {
    }
    
    void Person::Display() const
    {
        cout << "Name:" << _name << endl;
        cout << "ID:" << _id << endl;
    }
    
    class CollegeStu : public Person
    {
    private:
        string _major;
        double _score;
    
    public:
        CollegeStu(const string &name, int id, const string &major, double score);
        void Display() const;
    };
    
    CollegeStu::CollegeStu(const string &name, int id, const string &major, double score)
        : Person(name, id), _major(major), _score(score)
    {
    }
    
    void CollegeStu::Display() const
    {
        Person::Display();
        cout << "Major:" << _major << endl;
        cout << "C++ Score:" << _score << endl;
    }
    
    int main()
    {
        string name, major;
        int id;
        double score;
        cin >> name >> id >> major >> score;
        CollegeStu cs(name, id, major, score);
        cs.Display();
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 3月15日
  • 已采纳回答 3月7日
  • 创建了问题 3月7日

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址