|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include "1.h"
//得到当前所有学员的当前平均分,学员的分数是填写在构造的形参上的
CStudent s1(75);
void main()
{
std::cout << CStudent::GetAvgScore() << std::endl;
CStudent s2(80);
std::cout << CStudent::GetAvgScore() << std::endl;
CStudent* s3 = new CStudent(85);
std::cout << CStudent::GetAvgScore() << std::endl;
CStudent** s4 = new CStudent*[3];
s4[0] = new CStudent(60);
s4[1] = new CStudent(65);
s4[2] = new CStudent(70);
std::cout << CStudent::GetAvgScore() << std::endl;
delete s3;
std::cout << CStudent::GetAvgScore() << std::endl;
delete s4[0];
std::cout << CStudent::GetAvgScore() << std::endl;
delete s4[1];
std::cout << CStudent::GetAvgScore() << std::endl;
delete s4[2];
std::cout << CStudent::GetAvgScore() << std::endl;
delete s4;
system("pause");
}
//声明部分
#ifndef _STUDENT_H_
#define _STUDENT_H_
class CStudent
{
static double zongfen;
static double renshu;
double fenshu;
public:
CStudent(double a);
~CStudent();
static double GetAvgScore();
};
#endif
//定义部分
#include "1.h"
#include <iostream>
double CStudent::zongfen;
double CStudent::renshu;
CStudent::CStudent(double a)
:
fenshu(a)
{
zongfen += a;
renshu += 1;
}
CStudent::~CStudent()
{
renshu -= 1;
zongfen -= fenshu;
}
double CStudent::GetAvgScore()
{
return (zongfen / renshu);
}
求解
正确输出如下:
75
77.5
80
72.5
70
72.5
75
77.5
请按任意键继续. . .
|
|