C++第二天
#include"stdafx.h"
#include<iostream>
using namespace std;
class CStudent {
private :
int s_id;
static int m_count;
public :
CStudent(int id ) {
s_id = id;
m_count += 1;
}
int Getid() { return s_id; }
CStudent(CStudent &stu);
void Getcount() {
cout << "学生人数:" << m_count << endl;
}
};
CStudent::CStudent(CStudent &A) {
s_id = A.s_id;
m_count += 1;
}
int CStudent::m_count = 0;
int main() {
CStudent A(1002);
cout << "学生A:" << A.Getid() << endl;
A.Getcount();
CStudent B(A);
cout << B.Getid() << endl;
B.Getcount();
A.Getcount();
return 0;
}
- 使用静态成员的方法:
直接将调用函数声明为静态,则可以直接对类的静态数据进行访问。
static void Getcount(){
cout << ""这部分代码不变""
}