3. 实验6——类的继承与派生——保护继承 —— 学生信息类

【问题描述】

采用保护继承设计学生信息类,并实现 Set 和 PrintSID 函数,具体要求如下:

 

Student 类公有成员函数:void PrintSID(),函数输出成员变量 SID 的值,输出格式为:学号:SID。

普通函数:Set(int sid,string name,Student *ptr),它用前两个参数设置 ptr 对象的 SID 和 Name(继承 People 拥有的属性)属性值。

现在已有一个基类 People,它有一个公有成员变量姓名 Name,一个公有成员函数 PrintName(函数的功能是打印出 Name 的值)。


【样例输入】

1 严宏富

【样例输出】

学号:1

姓名:严宏富

 

#include<string>
#include<iostream>
using  namespace  std;

class  People
{
        public:
                string  Name;
                void  PrintName();
};

void  People::PrintName()
{
        cout  <<  "姓名:"  <<  Name  <<  endl;
}
//保护继承  People

class Student:protected People{
public:
    int SID;
    void PrintSID();
    string  gName(string  N){Name=N;return Name;} 
    int gSID(int S){SID=S;return SID;}    
};

void  Student::PrintSID()
{
        //输出学号  SID

cout<<"学号:"<<SID<<endl;

}

void  Set(int  sid,string  name,Student  *ptr)
{
        //给  ptr  对象的两个属性赋值

ptr->gSID(sid);
ptr->gName(name);

}
int  main()
{
        int  id;  
        string  name;
        cin  >>  id  >>  name  ;
                Student  st;
                Set(id,name,&st);
                  st.PrintSID();
        ((People*)&st)->PrintName();
              return  0;
}

【注】此分栏为西安理工大学C++练习题,所有答案仅供同学们参考。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值