用C++定义一个学生类,成员包括:学生姓名、学号、学习成绩(10门课程)。

2024-11-17 21:46:40
推荐回答(2个)
回答1:

#include
#include
using namespace std;
class Student
{
private:
string SName;
string SNo;
string SScore[10];
public :
void init();
void print();
};
void Student::init()
{
cout<<"请输入学生姓名:";
cin>>this->SName;
cout<<"学号:";
cin>>this->SNo;
cout<<"各科成绩:";
for(int i=0;i<10;i++)
{
cin>>this->SScore[i];
}
}
void Student::print()
{
cout<<"学生信息:姓名:"< for(int i=0;i<10;i++)
{
cout< }
cout<}
int main()
{
Student s;
s.init();
s.print();

return 0;
}

回答2:

#include
#include
using namespace std;

class student
{
private:
string studentName;
int studentId;
float studentScore[10];

public:
student(string name, int id, float *score)
{
studentName = name;
studentId = id;
for(int i = 0; i < 10; ++i)
{
studentScore[i] = score[i];
}
}
float average()
{
float temp = 0.0;
for(int i = 0; i < 10; ++i)
{
temp += studentScore[i];
}
temp = temp / 10.0;
return temp;
}
void print()
{
cout<<"name: "< cout<<"id: "< cout<<"scores: ";
for(int i = 0; i < 10; ++i)
cout< cout< cout<<"averageScore: "< }
};

void main()
{
string name;
int id;
float score[10];
float aveScore;
cout<<"please input student name,id and scores"< cin>>name;
cin>>id;
for(int i = 0; i < 10; ++i)
cin>>score[i];
student studentA(name, id, score);
studentA.print();

return ;
}