分类: C/C++
2011-08-04 11:40:12
/*
* 使用静态数据成员实现数据共享
* Lzy 2011-8-4
*/
#include
using namespace std;
class Work
{
private:
int worktime;
public:
static int total;
Work(int i=0):worktime(i){total += worktime;}
};
int Work::total = 0;
int main(void)
{
Work p1(2),p2(3),p3(4);
cout<
return 0;
}
/*
* 使用静态数据成员实现数据共享
* Lzy 2011-8-4
*/
#include
using namespace std;
class Test
{
private:
int m;
public:
static int n;
Test(int i):m(i){n++;}
void display(){cout<<"n="<
};
int Test::n = 0;
int main(void)
{
Test t1(11);
t1.display();
Test t2(22);
t2.display();
Test t3(33);
t3.display();
return 0;
}
/*
* 用两种方法调用静态成员函数
* Lzy 2011-8-4
*/
#include
using namespace std;
class Person
{
private:
static int id;
public:
static int number(){return id;};
};
int Person::id = 1001;
int main(void)
{
Person p;
cout<<"类名引用:"<
cout<<"对象引用:"<
return 0;
}
/*
* 静态成员函数引用非静态数据成员
* Lzy 2011-8-4
*/
#include
using namespace std;
class Person
{
private:
int id;
public:
Person(int i):id(i){}
static int number(Person &p){return p.id;};
};
int main(void)
{
Person p(5);
cout<<"类名引用:"<
cout<<"对象引用:"<
return 0;
}