f0814.cpp
资源名称:c.rar [点击查看]
上传用户:puke2000
上传日期:2022-07-25
资源大小:912k
文件大小:1k
源码类别:
C#编程
开发平台:
Visual C++
- //=====================================
- // f0814.cpp
- // static member function
- //=====================================
- #include<iostream>
- using namespace std;
- //-------------------------------------
- class Student{
- static int number;
- string name;
- public:
- void set(string str){
- name = str;
- ++number;
- }
- static void printNumber(){ cout<<number<<" total numbersn"; }
- void print(){ cout<<name<<" -> students are "<<number<<" numbersn"; }
- };//-----------------------------------
- int Student::number = 0; //静态数据成员在类外分配空间和初始化
- //-------------------------------------
- void fn(){
- Student s1;
- s1.set("Jenny");
- Student s2;
- s2.set("Randy");
- s1.printNumber();
- }//------------------------------------
- int main(){
- Student s;
- s.set("Smith");
- fn();
- Student::printNumber();
- }//====================================