📄 pex3_4.cpp
字号:
#include <iostream.h>
#include <iomanip.h>
class Person
{
private:
int id;
int age;
char sex;
public:
// constructor
Person(int pid = 0, int page = 0, char psex = 'M');
// I/O methods
void Read(void);
void Print(void) const;
// data access methods
int GetId(void) const;
int GetAge(void) const;
char GetSex(void) const;
};
Person::Person(int pid, int page, char psex) : id(pid), age(page), sex(psex)
{}
void Person::Read(void)
{
cout << "Enter the person's id, age, and sex" << endl;
cin >> id >> age >> sex;
}
void Person::Print(void) const
{
cout << "ID: " << setw(4) << id << " Age: "
<< age << " Sex: ";
if (sex == 'M')
cout << "Male" << endl;
else
cout << "Female" << endl;
}
int Person::GetId(void) const
{
return id;
}
int Person::GetAge(void) const
{
return age;
}
char Person::GetSex(void) const
{
return sex;
}
void main(void)
{
// age of youngWoman = 1000, age of oldMan = 0 (default)
Person youngWoman(0,1000,'F'), oldMan, person;
for(;;)
{
person.Read();
if (person.GetId() == 0)
break;
if (person.GetSex() == 'M' && person.GetAge() > oldMan.GetAge())
oldMan = person;
else if (person.GetSex() == 'F' && person.GetAge() < youngWoman.GetAge())
youngWoman = person;
}
cout << "Young woman: ";
youngWoman.Print();
cout << "Old man: ";
oldMan.Print();
}
/*
<Run>
Enter the person's id, age, and sex
100 30 M
Enter the person's id, age, and sex
150 20 F
Enter the person's id, age, and sex
50 40 M
Enter the person's id, age, and sex
10 25 F
Enter the person's id, age, and sex
0 0 M
Young woman: ID: 150 Age: 20 Sex: Female
Old man: ID: 50 Age: 40 Sex: Male
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -