一道关于派生类及成员函数的C++编程题

2024-12-02 20:24:38
推荐回答(1个)
回答1:

#include
#include
using namespace std;

class people
{
public:
people();
people(string name, string sex, int age, string work);
~people();
void set_name(string name);
string get_name();
void display_all();
private:
string name;
string sex;
int age;
string work;
};
class student : public people
{
public:
student(string name,string sex,int age,string work,string number);
void set_number(string number);//自己定义,我只声明
string get_number();//自己定义
private:
string name;
string sex;
int age;
string work;
string number;//为方便起见,只增加了学号属性

};
people::people()
{ }
people::people(string name, string sex, int age, string work)
{
this ->name = name;
this ->sex = sex;
this ->age = age;
this ->work = work;
}
people::~people()
{ }
void people::set_name(string name)
{
this ->name = name;
}
string people::get_name()
{
cout< return name;
}
void people::display_all()
{
cout<<"对象信息:"<}
int main()
{
people p1("wanlaoban", "男", 23, "学生");
p1.display_all();
return 0;
}

程序比较长,打了好久,我只做了重要的地方,其他该你做的我都写了提示的,自己看吧,打代码好累