LX708 派生类构造之面向对象

题目描述

裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关C++代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。

函数接口定义:

提示:
观察类的定义和main方法中的测试代码,补全缺失的代码。

裁判测试程序样例:

注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关函数。

 
#include <iostream>
using namespace std;
class People{
private:
    string id;
    string name;
public:
    People(string id, string name){
        this->id = id;
        this->name = name;
    }
    string getId(){
        return this->id;
    }
    string getName(){
        return name;
    }
};
class Student : public People{
private:
    string sid;
    int score;
public:
    Student(string id, string name, string sid, int score)
        /** 补充您的代码 **/

    }
    friend ostream& operator <<(ostream &o, Student &s);
};
ostream& operator <<(ostream &o, Student &s){
    o << "(Name:" << s.getName() << "; id:"
      << s.getId() << "; sid:" << s.sid
      << "; score:" << s.score << ")";
    return o;
}
int main(){
    string id,name,sid;
    int score;
    getline(cin,id);
    getline(cin,name);
    cin>>sid>>score;
    Student zs(id, name, sid, score);
    cout << zs << endl;
    return 0;
}

输入格式

4行,分别表示id,name,sid和score

输出格式

见样例输出

样例 #1

样例输入 #1

370202X
Zhang San
1052102
96

样例输出 #1

(Name:Zhang San; id:370202X; sid:1052102; score:96)

提示

本题目主要考察面向对象

复制主程序代码,完成缺失部分后将完整的代码提交进行测评!除了填充部分,已给定的代码不要做任何修改。

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

#include <iostream>
using namespace std;
class People{
private:
    string id;
    string name;
public:
    People(string id, string name){
        this->id = id;
        this->name = name;
    }
    string getId(){
        return this->id;
    }
    string getName(){
        return name;
    }
};
class Student : public People{
private:
    string sid;
    int score;
public:
    Student(string id, string name, string sid, int score):People(id,name),sid(sid),score(score){}//代码补充只加这一部分,要深刻理解其内容,类似于函数
       friend ostream& operator <<(ostream &o, Student &s);
};
ostream& operator <<(ostream &o, Student &s){
    o << "(Name:" << s.getName() << "; id:"
      << s.getId() << "; sid:" << s.sid
      << "; score:" << s.score << ")";
    return o;
}
int main(){
    string id,name,sid;
    int score;
    getline(cin,id);
    getline(cin,name);
    cin>>sid>>score;
    Student zs(id, name, sid, score);
    cout << zs << endl;
    return 0;
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值