2. Design a school registered personnel class (Person). Data members include: ID card number (idc), name (name), gender (sex), birthday (birth) and home address (addr). Data type is set according to needs. Function members include: input and display of personnel information, constructor and copy constructor. Other member functions can also be added by themselves. Write test code:
1) define an array of ten objects, and enter ten students in turn,
2) then print all boys' information.
#include <iostream> #include <stdlib.h> #include <string> using namespace std; class Person { string idc; string name; char sex; string birth; string addr; public: Person(); Person(Person &C); void Input(); void PrintPerson(); char Getsex(){return sex; }//Query gender }; Person::Person()//Constructor { idc = "No ID number entered"; name = "No name entered"; sex = '0'; birth = "No birthday entered"; addr = "No address entered"; } void Person::Input() //Input { cout << "Please enter ID number:"; getline(cin, idc); cout << "Please enter your name:"; getline(cin, name); cout << "Please enter gender (male: m Female: f): "; cin >> sex; getchar(); cout << "Please enter birthday:"; getline(cin, birth); cout << "Please enter your home address:"; getline(cin, addr); cout << "----------Input completed——————————"<<endl<<endl; } Person::Person(Person &C) //copying functions { idc = C.idc; name = C.name; sex = C.sex; birth = C.birth; addr = C.addr; } void Person::PrintPerson() //Printing { cout<< "ID number:"<<idc<<endl; cout << "Full name:"<<name<<endl; cout << "Gender (male: m Female: f): "<<sex<<endl; cout << "Birthday:"<<birth<<endl; cout << "Home address:"<<addr<<endl; cout << "----------Output complete——————————" << endl << endl; } int main() { Person Class1[10]; int i; for (i = 0; i < 10; i++) //1. Input ten students in turn { Class1[i].Input(); } //Class1[2] = Class1[0]; cout << "----------Output all boys' information——————————" << endl; for (i = 0; i < 10; i++) //2. Output all boys { if (Class1[i].Getsex() == 'm') { Class1[i].PrintPerson(); } } system("PAUSE"); return 0; }
Test data:
110102201900001111
YUXI
f
2019.00.01
Beijing
110102199900002222
Zhang san
m
1999.00.02
xiamen
110102199900003333
Li si
m
1999.00.03
Xiamen
110102199900004444
Wang wu
m
1999.00.04
xiamen
110102199900005555
Shen liu
f
1999.00.05
xiamen
110102199900006666
Xiao qi
m
1999.00.06
Xiamen
110102199900007777
Xiao ba
f
1999.00.07
xiamen
110102199900008888
Xiao jiu
m
1999.00.08
xiamen
110102199900009999
Xiao xiao
m
1999.00.09
xiamen
110102199900001010
Wu ming
f
1999.00.10
xiamen