1. 定义一个student类,用C++
1.定义一个student类,包括学号、姓名、性别、数学、计算机、英语、平均分等属性,并分别定义输出属性值的成员函数,还可根据需要定义其它的成员函数。应用该类生成一对象数...
1. 定义一个student类,包括学号、姓名、性别、数学、计算机、英语、平均分等属性,并分别定义输出属性值的成员函数,还可根据需要定义其它的成员函数。应用该类生成一对象数组stu[10]并给各数据成员赋值,然后按平均分从高到低排序后输出。
展开
1个回答
展开全部
#include <iostream>
using namespace std;
class student {
char name[10];
float score;
static int count ;
static float total;
public:
student(){ }
//student(char Name[10],float Score)
// {strcpy(name ,Name); score=Score; ++count; }
void totalscore(char Name[10],float Score )
{strcpy(name ,Name); score=Score;++count;}
void sum(){total=total+score; }
static float average(){return (total/count);}
static float getcount(){return count;}
static float gettotal(){return total;}
};
float student::total=0;
int student::count=0;
void main()
{
student stu[5];
int i;
float score;
char name[10];
cout<<" "<<"姓名"<<'\t'<<"成绩:\n";
for (i=0;i<5;i++)
{
cout<<"请输入第"<<i+1<<"个学生的姓名";
cin.getline(name,10);
cout<<"请输入第"<<i+1<<"个学生的成绩";
cin>>score;
cin.get();
stu[i].totalscore(name,score);
stu[i].sum();
}
cout<<student::getcount()<<endl;
cout<<student::gettotal()<<endl;
cout<<student::average();
}
using namespace std;
class student {
char name[10];
float score;
static int count ;
static float total;
public:
student(){ }
//student(char Name[10],float Score)
// {strcpy(name ,Name); score=Score; ++count; }
void totalscore(char Name[10],float Score )
{strcpy(name ,Name); score=Score;++count;}
void sum(){total=total+score; }
static float average(){return (total/count);}
static float getcount(){return count;}
static float gettotal(){return total;}
};
float student::total=0;
int student::count=0;
void main()
{
student stu[5];
int i;
float score;
char name[10];
cout<<" "<<"姓名"<<'\t'<<"成绩:\n";
for (i=0;i<5;i++)
{
cout<<"请输入第"<<i+1<<"个学生的姓名";
cin.getline(name,10);
cout<<"请输入第"<<i+1<<"个学生的成绩";
cin>>score;
cin.get();
stu[i].totalscore(name,score);
stu[i].sum();
}
cout<<student::getcount()<<endl;
cout<<student::gettotal()<<endl;
cout<<student::average();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询