CMyclass是A类的对象
静态数据成员的说明和初始化,私有静态数据成员
#include<iostream>
using namespace std ;
class counter
{
static int num ; // 私有 静态数据成员
public :
void setnum ( int i ) { num = i ; }
void shownum() { cout << num << '\t' ; }
} ;
int counter :: num = 0 ; // 在类外定义和初始化
int main ()
{
counter a , b ;
a.shownum() ; b.shownum() ;
a.setnum(10) ;
a.shownum() ; b.shownum() ;
//cout<<counter :: num<<endl ; //error: `int counter::num' is private
}
/*
输出
0 0 10 10
*/
使用公有静态数据成员
#include<iostream>
using namespace std ;
class counter
{
public :
counter (int a)
{ mem = a; }
int mem; // 公有数据成员
static int Smem ; // 公有 静态数据成员
} ;
int counter :: Smem = 1 ; // 初始值为 1
int main()
{
counter c(5);
int i ;
for( i = 0 ; i < 5 ; i ++ )
{
counter::Smem += i ; //Smem引用必须是静态数据成员,public
cout << counter::Smem << '\t' ;
}
cout<<endl;
cout<<"c.Smem = "<<c.Smem<<endl;
cout<<"c.mem = "<<c.mem<<endl;
}
/*
输出
1 2 4 7 11
c.Smem = 11
c.mem = 5
*/
设计包含静态数据成员的类
某商店经销一种货物,货物成箱进,成箱卖出,购进和卖出都是 以重量为单位(每箱的重量不同),商店需要记录下存货的总重量。
分析:
定义一个货物类,表示一箱货物,类中包含
私有成员 weight
一个静态数据成员total_weight;
定义in函数,表示进货,进货时total_weight的值增加
定义out函数,表示出货,出货时total_weight的值减少 类的定义?
#include <iostream>
using namespace std;
class Goods
{
int weight; //static int total_weight;
public:
static int total_weight; //定义成公有
Goods(int x):weight(x){ }
void in(){total_weight=total_weight+weight; }
void out(){total_weight=total_weight-weight; }
void display_store(){ cout<<total_weight<<endl;}
};
int Goods::total_weight=0;
void display_store(){ cout<<"out "<<Goods::total_weight<<endl;}
int main()
{
Goods g1(10), g2(20);
g1.in(); g2.in(); g1.display_store(); g2.display_store(); display_store(); g1.out(); g1.display_store(); display_store();
cout<<Goods::total_weight<<endl;
return 0;
}
/*
输出
30
30
out 30
20
out 20
20
*/
静态成员函数
静态成员函数用于操作静态数据成员,它只能直接引用
属于该类的静态数据成员或静态成员函数。
类的静态成员是属于类的而不是属于哪一个对象的,它们都不是对象成员。通常定义成公有的,因此,对静态成员的引用不需要用对象名。类外代码可以使用类名和作用域操作符来调用静态成员函数。
静态成员函数没有this指针,只能对静态数据操作
定义静态成员函数的格式如下:
static 返回类型 静态成员函数名(参数表);
与静态数据成员类似,调用公有静态成员函数的一般格式有如下几种:
类名::静态成员函数名(实参表)
对象. 静态成员函数名(实参表)
对象指针->静态成员函数名(实参表)
静态成员函数访问非静态数据成员
#include<iostream>
using namespace std;
class small_cat
{
public:
small_cat(double w){weight=w;total_weight+=w;total_number++;}
static void display(small_cat &w) //静态成员函数访问非静态数据成员
{cout<<"The small_cat weights "<<w.weight<<"kg\n";}
static void total_disp() //静态成员函数访问静态数据成员
{ cout<<total_number<<"small_cat total weight ";
cout<<total_weight<<" kg "<<endl;}
private:
double weight;
static double total_weight;
static double total_number; //静态数据成员
};
double small_cat::total_weight=0;
double small_cat::total_number=0;
int main()
{
small_cat w1(0.9),w2(0.8),w3(0.7);
small_cat::display(w1);
small_cat::display(w2);w2. total_disp() ;
small_cat::display(w3);
small_cat::total_disp(); w2. display(w3);
return 0;
}
输出
The small_cat weights 0.9kg
The small_cat weights 0.8kg
3small_cat total weight 2.4 kg
The small_cat weights 0.7kg
3small_cat total weight 2.4 kg
The small_cat weights 0.7kg
*/
在程序执行的某个时刻,有时需要知道某个类已创建的对象个数,现在 仍存活的对象个数。
类设计:
数据成员:在类中定义两个静态的数据成员:obj_count和obj_living。
成员函数:要实现计数功能,可以在创建一个对象时,对这两个数各加1。 当撤销一个对象时, obj_living减1。为了知道某一时刻对象个数的信息, 可以定义一个静态成员函数返回这两个值。
#include <iostream>
using namespace std;
class StaticSample
{
private:
static int obj_count;
static int obj_living;
public:
StaticSample() {++obj_count; ++obj_living;}
~StaticSample() {--obj_living;}
static void display() //静态成员函数
{ cout << "总对象数:" << obj_count << "\t存活对象数:" << obj_living << endl; }
};
int StaticSample::obj_count = 0; //静态数据成员的定义及初始化
int StaticSample::obj_living = 0; //静态数据成员的定义及初始化
int main()
{
StaticSample::display(); //通过类名限定调用成员函数
StaticSample s1, s2;
StaticSample::display();
StaticSample *p1 = new
StaticSample, *p2 = new
StaticSample; s1.display(); //通过对象调用静态成员函数
delete p1; p2->display(); //通过指向对象的指针调用静态成员函数
delete p2;
StaticSample::display();
return 0;
}
输出
总对象数:0 存活对象数:0
总对象数:2 存活对象数:2
总对象数:4 存活对象数:4
总对象数:4 存活对象数:3
总对象数:4 存活对象数:2