#include<iostream>usingnamespace std;classA{int h,i,j;staticint k;//必须在类体外初始化};classB:A{int m,n,p;conststaticint q =0;//const 只能在体外或者体内一处有值};int A::k =0;//静态数据成员必须初始化intmain(){
cout <<"size of int : "<<sizeof(int)<< endl;
cout <<"size of A :"<<sizeof(A)<< endl;
cout <<" size of B :"<<sizeof(B)<< endl;system("pause");return0;}
运行结果
size of int:4
size of A :12
size of B :24
示例代码
classBAG{int*const e;constint s;int p;public:BAG(int m):e(newint[m]),s(e ? m :0){ p =0;}~BAG(){if(e){delete e;*(int*)&s =0;*(int**)&e =0;//0代表空指针}}intgetp(){return p;}inthave(int f){for(int i =0; i < p; i++){if(e[i]== f)return1;return0;}}};classSET:protected BAG{//不允许重复的元素,故须覆盖基类的putepublic:
BAG::getp;
BAG::have;//从保护恢复为publicintpute(int f){returnhave(f)?1:BAG::have(f);//不能去掉BAG::,递归}SET(int m):BAG(m){}//C++缺省的析构函数~SET( )自动调用~BAG( )};//若定义析构函数,形式为~SET( ){ },功能同缺省析构函数