📄 3-21.txt
字号:
例3.21 使用静态成员函数访问静态数据成员
#include <iostream.h>
class small_cat{
private:
double weight;
static double total_weight; //静态数据成员
static double total_number; //静态数据成员
public:
small_cat(double w)
{ weight=w;
total_weight+=w;
total_number++;
}
void display()
{
cout<<"小猫的重量是:"<<weight<<"磅"<<endl;
}
static void total_disp() //静态成员函数
{
cout<<total_number<<"只小猫总重是:"<<total_weight<<"磅"<<endl;
}
};
double small_cat::total_weight=0;
double small_cat::total_number=0;
void main()
{ small_cat w1(1.8),w2(1.6),w3(1.5);
w1.display();
w2.display();
w3.display();
small_cat::total_disp(); //通过类调用静态成员函数
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -