c++ 设计一个学生类
题目是要设计一个学生类,包含学号、姓名和三门课的成绩,并且要计算出三门课的平均成绩。主程序已经给定如下:intmain(){Studentstu1("200906264"...
题目是要设计一个学生类,包含学号、姓名和三门课的成绩,并且要计算出三门课的平均成绩。主程序已经给定如下:
int main()
{
Student stu1("200906264","Li Weiwei",88,75,91);
stu1.display();
Student stu2("200902164","Chen Hanfu",86,78,93);
stu2.display();
Student stu3("200908079","Zhao Gaolin",94,69,97);
stu3.display();
cout << "The average grade of course1: " << stu1.average1() << endl;
cout << "The average grade of course2: " << stu2.average2() << endl;
cout << "The average grade of course3: " << stu3.average3() << endl;
return 0;
}
我就是不懂怎样在类中计算每门课的平均成绩。。。求大神教教,万分感谢 展开
int main()
{
Student stu1("200906264","Li Weiwei",88,75,91);
stu1.display();
Student stu2("200902164","Chen Hanfu",86,78,93);
stu2.display();
Student stu3("200908079","Zhao Gaolin",94,69,97);
stu3.display();
cout << "The average grade of course1: " << stu1.average1() << endl;
cout << "The average grade of course2: " << stu2.average2() << endl;
cout << "The average grade of course3: " << stu3.average3() << endl;
return 0;
}
我就是不懂怎样在类中计算每门课的平均成绩。。。求大神教教,万分感谢 展开
1个回答
展开全部
class Student
{
string sNum;
string sName;
double scoreA;
double scoreB;
double scoreC;
public:
void display()
{
cout << sNum.c_str() << " " <<sName.c_str() << " "<<scoreA<<" "<<scoreB<<" "scoreC<<endl;
}
Student(string num,string name,double A,double B,double C)
{
sNum = num;sName = name;scoreA = A;scoreB=B;scoreC=C;
}
double average()
{
return (scoreA+scoreB+scoreC)/3;
}
};
}
}
};
{
string sNum;
string sName;
double scoreA;
double scoreB;
double scoreC;
public:
void display()
{
cout << sNum.c_str() << " " <<sName.c_str() << " "<<scoreA<<" "<<scoreB<<" "scoreC<<endl;
}
Student(string num,string name,double A,double B,double C)
{
sNum = num;sName = name;scoreA = A;scoreB=B;scoreC=C;
}
double average()
{
return (scoreA+scoreB+scoreC)/3;
}
};
}
}
};
追问
你求的平均数是每个学生三门的平均成绩吧…题目要求的是每门课三个学生的平均成绩…怎么弄初学者求解答…
追答
如果一定要那样
static double TotalA = 0; static double TotalB = 0; static double TotalC = 0 ;static int iCount = 0;
class Student{
string sNum; string sName; double scoreA; double scoreB; double scoreC;
public:
void display(){ cout << sNum.c_str() << " " << sName.c_str() << " " << scoreA << " " << scoreB << " "<< scoreC << endl;
}
Student(string num, string name, double A, double B, double C){
sNum = num; sName = name; scoreA = A; scoreB = B; scoreC = C;
TotalA += A; TotalB += B; TotalC += C; iCount++;
}
double average1(){ return TotalA / iCount; }
double average2(){ return TotalB / iCount; }
double average3(){ return TotalC / iCount; }
};
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询