要求:建立一个学生类,定义全部成员变量为私有,并添加一个公有的成员函数,实现从键盘录入该类成员变量的信息。
#include <iostream>
using namespace std;
class CStudent
{
// 公有成员变量和成员函数
public:
void GetStudent()
{
cout << "请输入学生的名字:";
cin >> name;
cout << "请输入学生的年龄:";
cin >> age;
cout << "请输入学生的性别:";
cin >> sex;
cout << "请输入学生的学号:";
cin >> num;
cout << "学生的名字:" << name <<endl;
cout << "学生的年龄:" << age << endl;
cout << "学生的性别:" << sex << endl;
cout << "学生的学号:" << num << endl;
}
// 私有成员变量和成员函数
private:
int age = 0;
int num = 0;
string sex;
string name;
};
int main()
{
CStudent Stu;
Stu.GetStudent();
return 0;
}