飞云天晴 发表于 2015-7-1 00:06:42

这代码在VS2005上运行正确,可在VS2013上却运行不对,为什么?

#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*;
        s4 = new CStudent(60);
        s4 = new CStudent(65);
        s4 = new CStudent(70);
        std::cout << CStudent::GetAvgScore() << std::endl;

        delete s3;
        std::cout << CStudent::GetAvgScore() << std::endl;

        delete s4;
        std::cout << CStudent::GetAvgScore() << std::endl;

        delete s4;
        std::cout << CStudent::GetAvgScore() << std::endl;

        delete s4;
        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
请按任意键继续. . .
页: [1]
查看完整版本: 这代码在VS2005上运行正确,可在VS2013上却运行不对,为什么?