ex020102.cpp
来自「深入浅出Visual C++入门进阶与应用实例 随书光盘 作者 何志丹」· C++ 代码 · 共 95 行
CPP
95 行
// Ex020102.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
//学生类
class CStudent//学生类
{
public :
CStudent()
{
m_iID = ++s_iMaxID ;//学生ID是递增的,第一个是1,下一个是2...
}
void SetScore(int iScore)
{
m_iScore = iScore;
};
int GetScore()
{
return m_iScore;
};
int GetID()
{
return m_iID ;
}
private:
int m_iScore ;//分数
int m_iID ;//学生ID,用来模拟学号,
static int s_iMaxID ;//记录当前已分配的最大ID
};
int CStudent::s_iMaxID = 0 ;
//大学生类
class CUniversityMan : public CStudent
{
};
//班级类
class CClass
{
private:
CUniversityMan m_Stu[5] ;//假设班上只有五个学生
public:
bool SetScore(int iStuID,int iScore);//为学生打分
int GetAveScore();//取得平均分绩
};
bool CClass::SetScore(int iStuID, int iScore)
{
const int iStuCount = sizeof(m_Stu)/sizeof(m_Stu[0]) ;//得到学生数
for(int i = 0 ; i < iStuCount ; i++ )
{
if(iStuID == m_Stu[i].GetID())
{
m_Stu[i].SetScore(iScore) ;
return true ;
}
}
return false ;
}
int CClass::GetAveScore()
{
const int iStuCount = sizeof(m_Stu)/sizeof(m_Stu[0]) ;//得到学生数
int nScoreSum = 0 ;
for(int i = 0 ; i < iStuCount ; i++ )
{
nScoreSum += m_Stu[i].GetScore();
}
return nScoreSum / iStuCount ;//取得平均成绩
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CClass class2006 ;//定义20006班的对象
class2006.SetScore(1,100);
class2006.SetScore(2,90);
class2006.SetScore(3,80);
class2006.SetScore(4,70);
class2006.SetScore(5,60);
int nAveScore = class2006.GetAveScore();
return 0;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?