5-2 派生类的构造函数

来源:互联网 发布:淘宝客佣金计算规范 编辑:程序博客网 时间:2024/06/02 06:56

5-2 派生类的构造函数

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic

Problem Description

通过本题目的练习可以掌握派生类构造函数的定义和使用方法。

要求定义一个基类Person,它有3protected的数据成员:姓名name(char *类型)、性别 sex(char类型)、年龄age(int类型);一个构造函数用于对数据成员初始化;有一个成员函数show()用于输出数据成员的信息。

创建Person类的公有派生类Employee,增加两个数据成员 基本工资 basicSalaryint类型) 请假天数leaveDaysint型);为它定义初始化成员信息的构造函数,和显示数据成员信息的成员函数show()

Input

 

5个数据,分别代表姓名、性别、年龄、基本工资、请假天数。

Output

 

如示例数据所示,共5行,分别代表姓名、年龄、性别、基本工资、请假天数

Example Input

zhangsan m 30 4000 2

Example Output

name:zhangsanage:30sex:mbasicSalary:4000leavedays:2

Hint


#include<bits/stdc++.h>using namespace std;class Person{protected:    string name;    char sex;    int age;public:    Person()    { cin>>name>>sex>>age;    }    void show1()    {        cout<<"name:"<<name<<endl;        cout<<"age:"<<age<<endl;        cout<<"sex:"<<sex<<endl;    }};class Employee:public Person{private:    int basic, leave;public:    Employee()    { cin>>basic>>leave;    }    void show2()    {        cout<<"basicSalary:"<<basic<<endl;        cout<<"leavedays:"<<leave<<endl;    }};int main(){    Employee p;    p.show1();    p.show2();    return 0;}



#include<iostream>#include<stdio.h>#include<string.h>using namespace std;class Person{    private:string name;char sex;int age;    public:        Person()        {            cin >> name >> sex >> age;        }        void show1()        {            cout << "name:" << name <<endl;            cout << "age:" << age << endl << "sex:" << sex << endl;        }};class Employee:public Person{    private:int basicSalary, leaveDays;    public:        Employee()        {            cin >> basicSalary >> leaveDays;        }        void show()        {            show1();            cout<<"basicSalary:"<<basicSalary<<endl;            cout<<"leavedays:"<<leaveDays<<endl;        }};int main(){    Employee a;    a.show();    return 0;}