最近做的一些题:
问题: 设计一个Student类,它包含三个私有数据成员:int num、string name、char sex;两个公有的成员函数:set()和display(),分别用来设置一个学生的信息和显示该学生的信息。编写类和主函数进行验证。
问题:
输入学号,姓名,性别(0表示男,1表示女)
Output:
学号
姓名
性别
Sample Input
101 张三 0
Sample Output
101
张三
男
代码如下:
#include <iostream>
using namespace std;
struct student
{
private :
int num;
string name;
int sex;
public:
void set()
{
cin>>num;
cin>>name;
}
void display()
{
cin>>sex;
if(sex==0)
{
**(下面是接着的代码)**
cout<<num<<endl<<name<<endl<< "男"<<endl;
}
if(sex==1)
{
cout<<num<<endl<<name<<endl<< "女"<<endl;
}
}
};
int main()
{
student p;
p.set();
p.display();
return 0;
}