#include<iostream>
#include <string.h>
using namespace std;
class student
{
public:
void Init()
{
cout << number << " " << score << " " << name;
}
void GetN(int n) { number=n; }
void GetS(int s) { score=s; }
void GetA(char a[]) { strcpy_s(name, a); }
private:
int number, score;
char name[50];
};
int main()
{
char a[9] = { "小红" };
student stu;
stu.GetN(10);
stu.GetS(100);
stu.GetA(a);
stu.Init();
return 0;
}
定义一个学生类Student,包含三个属性(学号,姓名,成绩)均为私有的,分别给这三个属性定义两个成员函数,一个设置它的值,另一个获得它的值。然后在main函数中通过对象调用成员函数。
最新推荐文章于 2025-11-09 00:00:49 发布
本文展示了一个使用C++实现的学生信息类,包括初始化、获取学号、成绩和姓名的功能。通过实例化student类,设置并输出了学生的小红,学号为10,成绩为100的信息。
1953

被折叠的 条评论
为什么被折叠?



