#pragma once
#include <iostream>
using namespace std;
class student
{
public:
void InitStudent(char name[]);
void ExpendMoney(float a);
void ShowMoney();
private:
static float m_ClassMoney;
char nm[20];
};
float student::m_ClassMoney = 1000;
void student::InitStudent(char name[])
{
int i = 0;
while (name[i])
{
nm[i] = name[i];
i++;
}
}
void student::ExpendMoney(float a)
{
m_ClassMoney += a;
}
void student::ShowMoney()
{
cout << "班费还剩" << m_ClassMoney << endl;
}
int main()
{
student A, B, C;
A.ExpendMoney(-50);
A.ShowMoney();
B.ExpendMoney(-98.5);
A.ShowMoney();
C.ExpendMoney(-500.53);
A.ShowMoney();
}
本次C++实验主要学习学习对象传递与静态成员功能,进一步加深了对类与对象的理解,掌握了静态成员的概念和使用,同时创建了student类initstudent,expendmoney和showmoney来实现显示剩余班费的功能。程序代码和实验结果如上。