学校在册人员类

185 阅读1分钟

设计一个学校在册人员类(Person)。数据成员包括:身份证号(idc),姓名(name),性别(sex), 生日(birth),家庭地址(addr),人数(count),其中人数为静态数据成员,数据类型根据需 要自行设定。函数成员包括:人员信息的录入和显示,还包括构造函数与拷贝构造函数, 其他成员函数也可自行添加。编写测试代码:

​ 1)定义包含十个对象的数组,依次录入十个学生。

​ 2)然后打印所有男生的信息。

​ 3)打印学校在册人员总数量。

# include <iostream>
using namespace std ;
const int n = 10 ;
class Person{
    static int cnt ;
private:
    string idc ;
    string name ;
    char sex ;
    string birth;
    string addr;
public:
    Person(){
        idc = "未输入身份证号";
        name = "未输入姓名";
        sex = '0';
        birth = "未输入生日";
        addr = "未输入地址";
    }
    void input(){
        cout << "请输入身份证号:";
        cin >> idc ;
        cout << "请输入姓名:";
        cin >> name ;
        cout << "请输入性别(男:m 女:f):";
        cin >> sex;
        getchar();
        cout << "请输入生日:";
        cin >> birth ;
        cout << "请输入家庭住址:";
        cin >> addr ;
        cnt ++ ;
    }
    void output(){
        cout<< "身份证号:"<<idc<<endl;
        cout << "姓名:"<<name<<endl;
        cout << "性别(男:m 女:f):"<<sex<<endl;
        cout << "生日:"<<birth<<endl;
        cout << "家庭住址:"<<addr<<endl;
    }
    [[nodiscard]] char getSex() const
    {
        return sex ;
    }
    static void total(){
        cout << cnt ;
    }
};

int Person::cnt = 0 ;

int main() {
    system("chcp 65001 > null");
    Person *per = nullptr ;
    per = new Person[n];
    for(int i = 0 ; i < n ; i ++ )
        per[i].input();
    cout << "———————————输入完毕———————————" << endl ;
    cout << "所有男生信息:" << endl ;
    for(int i = 0 ; i < n ; i ++ )
        if(per[i].getSex() == 'm')
            per[i].output() ;
    cout << "———————————输出完毕———————————" << endl ;
    cout << "学校在册人员总数量:";
    Person::total() ;
    cout << endl ;
    return 0 ;
}