6-33.写出派生类构造方法(C++)

本文提供了一个C++编程问题,要求补全一个关于定义基类People和派生类Student的代码片段,包括构造函数和输出操作符重载,以确保测试程序能正常运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

代码实现:

#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(){
        Student  zs("370202X",  "Zhang  San",  "1052102",  96);
        cout  <<  zs    <<  endl;
        return  0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值