学生派生类 (12 分)

该博客介绍了如何在C++中根据已有的`Student`类定义一个派生类`Student1`,并使用构造函数初始化成员变量,包括`num`、`name`、`sex`、`age`和`address`。示例代码展示了如何创建`Student1`对象并展示其信息,符合指定的输出格式。

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

学生派生类 (12 分)

根据所给的类Student定义其派生类,并利用构造函数进行数据初始化,使程序能按照"样例"的格式进行输出

类定义:


#include <iostream> #include<string> using namespace std; class Student {public: Student(int n,string nam,char s ) {num=n; name=nam; sex=s; } ~Student( ) { } protected: int num; string name; char sex ; }; /* 请在这里添加派生类定义 */

裁判测试程序样例:


int main( ) {Student1 stud1(10010,"Wang-li",'f',19,"115 Beijing Road,Shanghai"); Student1 stud2(10011,"Zhang-fun",'m',21,"213 Shanghai Road,Beijing"); stud1.show( ); stud2.show( ); return 0; }

输出样例:

num: 10010
name: Wang-li
sex: f
age: 19
address: 115 Beijing Road,Shanghai

num: 10011
name: Zhang-fun
sex: m
age: 21
address: 213 Shanghai Road,Beijing

#include <iostream>
#include<string>
using namespace std;
class Student
{
public:
    Student(int n, string nam, char s)
    {
        num = n;
        name = nam;
        sex = s;
    }
    ~Student() { }
protected:
    int num;
    string name;
    char sex;
};
class Student1 : public Student
{
protected:
    int age;
    string address;
public:
    Student1(int n, string nam, char s, int age, string address) :Student(n, nam, s)
    {
        this->age = age;
        this->address = address;
    }
    void show()
    {//num : 10011
        cout << "num" << " " <<":" << " " << num << endl;
        cout << "name" << " " << ":" << " " << num << endl;
        cout << "sex" << " " <<":" << " " << num << endl;
        cout << "age" << " " <<":" << " " << num << endl;
        cout << "address" << " " <<":" << " " << num << endl;
        cout << endl;
    }
};
int main()
{
    Student1 stud1(10010, "Wang-li", 'f', 19, "115 Beijing Road,Shanghai");
    Student1 stud2(10011, "Zhang-fun", 'm', 21, "213 Shanghai Road,Beijing");
    stud1.show();
    stud2.show();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值