仿照课堂的身份证的例子,实现每个同学仅有一个学号这一问题。
实验要求:
1. 画出对应的类图;
2.提交源代码;
#include <iostream>
using namespace std;
class S_num {
private:
static S_num sno;
static int num;
public:
static S_num getintance() {
if (num == 0) {
cout << "第一次办理学生证,分配新号码!" << endl;
sno.setNum(20223902);
}
else {
cout << "重复办理学生证,获取旧号码!" << endl;
}
return sno;
}
int getNum() {
return num;
}
void setNum(int n) {
num = n;
}
};
int S_num::num = 0;
S_num S_num::sno = S_num();
int main()
{
S_num sno1 = S_num().getintance();
cout << "第一次获取学号" << sno1.getNum() << endl;
S_num sno2 = S_num().getintance();
cout << "第二次获取学号" << sno2.getNum() << endl;
cout << "内容是否相等:" << (sno1.getNum() == sno2.getNum()) << endl;
//cout<<"是否是相同对象:"<<(sno1==sno2)<<endl;
}