|
发表于 2020-4-10 10:55:45
|
显示全部楼层
说实话,这段代码看不出来楼主想要什么功能。
楼主看一下基础的书《C++ Primer》,然后代码风格的资料也看看。
根据输出内容我大概猜测一下这个类的功能时,可以存储学生的:姓名,学号,成绩。
#include <iostream>
#include <string>
using namespace std;
class CStudent
{
private:
unsigned int nStudentId;//使用unsigned int,学号应该为正数
unsigned int nScore;//使用unsigned int,成绩应该为正数
//char name[9];
string sName;//改用string的原因是楼主的char数组只能存9个字符
public:
CStudent(unsigned int nStudentId, unsigned int nScore,const string &sName)
: nStudentId(nStudentId)
, nScore(nScore)
, sName(sName)
{
}
//不需要默认构造函数
void showInfo()
{
cout << "姓名 = " << sName << endl;
cout << "学号 = " << nStudentId << endl;
cout << "分数 = " << nScore << endl;
}
};
int main()
{
CStudent xuesheng1(111,100,"张三");
xuesheng1.showInfo();
return 0;
} |
|