C++数据成员间 值传递

来源:百度知道 编辑:UC知道 时间:2024/06/22 19:38:01
这是一个计算学生们总成绩,学生人数,和计算平均分的程序,要用结构数组。以下是我的代码:

#include<iostream.h>

class Student
{
public:
float score;
static float total;
static int count;
float ave;
void scoretotalcount(float s);
static void sum();
static void average();
void out()
{
cout<<"总分:"<<total<<'\t'<<"平均分:"<<ave<<'\t'<<"总人数:"<<count<<endl;
}
};

float Student::total=0;
int Student::count=0;

void Student::sum()
{
float total=0,score=0;
total += score;
}

void Student::average()
{
float ave=0,total=0;
int count=0;
ave=total/count;
}

void Student::scoretotalcount(float s)
{
int count=0;
score=s;
count+=1;
}

void main()
{
float s=1,score=0,total=0,ave=0;
int count

我改了一下你的程序 我执行的时候都正确了 你试一下
我觉得你的程序有如下一些问题(有的地方可能是我没有理解,不对的地方请见谅)
1.student类中已经声明了score,ave,count这些变量,就不用再在每个类函数中再次声及赋值了,尤其是在主函数中再次声明是没有用的。要想给类中的数据成员赋值,就要编写构造函数。
2.类函数void sum()和void average(),不太明白为什么要定义成static型,这样调用起来很麻烦,不用static一样不会有问题
#include<iostream>
using namespace std;

class Student
{
public:
float score;
static float total;
static int count;
float ave;
void scoretotalcount(float s);
void sum();
void average();
void out()
{
cout<<"总分:"<<total<<'\t'<<"平均分:"<<ave<<'\t'<<"总人数:"<<count<<endl;
}
};

float Student::total=0;
int Student::count=0;

void Student::sum()
{
total +=score;
}

void Student::average()
{
ave=total/count;
}

void Student::scoretotalcount(floa