#include <string>
#include <iostream>
using namespace std;
class User
{
//成员声明
public:
string Name;
int Books;
User(string name, int books);
~User();
static void GetState();
private:
static int UserCount;
static int BookCount;
};
int User::UserCount = 0;
int User::BookCount = 0;
//成员函数声明
User::User(string name, int books)
{
Name = name;
Books = books;
cout << Name << " " << Books << " " << "进入" << endl;
UserCount++;
BookCount += books;
}
User::~User()
{
UserCount--;
BookCount -= Books;
cout << Name << " " << Books << " " << "离开" << endl;
}
void User::GetState()
{
cout << "书店人数:" << UserCount << ",书店共享书数量:" << BookCount << ",人均共享数量:" << BookCount / UserCount << endl;
}
int main()
{
string name1, name2, name3;
int a, b, c;
cin >> name1 >> a >> name2 >> b >> name3 >> c;
User* u1 = new User(name1, a);
User* u2 = new User(name2, b);
User::GetState();
delete u1;
u1 = new User(name3, c);
User::GetState();
delete u2;
User::GetState();
delete u1;
User::GetState();
}
头歌实训项目【静态成员 —— 模拟共享书店】
于 2022-04-03 15:24:35 首次发布