ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

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

2026/8/5 8:37:29 拓冰建站 浏览量
6-33.写出派生类构造方法(C++)

裁判测试程序样例中展示的是一段定义基类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;
}