当前位置:网站首页>Static member
Static member
2022-04-23 06:37:00 【*Flowers bloom on the street】
In order to realize data sharing among multiple objects of a class ,C++ Put forward Static members The concept of .
Static members include :
Static data members and static member functions
The role of static members :
1. Static members do not belong to an object , and Belongs to a class ( Belongs to all objects ), No matter how many objects are created , All share static members —— It is a bridge connecting various objects .
2. Static data members are mainly used to share data among objects . Such as : Statistics of total 、 Average, etc .
Definition of static data members :
static data type Data member name ;
class student
{
private:
int num;
char* name;
static float score;
};
Static data members can be initialized , But only outside the class
Format : data type Class name :: Static data members = initial value ;
explain :
1) Static data members can be described as public 、 Private or protected , If the description is public, it can be accessed directly through the class name .
Class name :: Static member name
2) Private or protected static data members can only be accessed through public member functions .
3) Static members are different from global variables —— Encapsulated inside a class , Replace global variables with static data members , Make data more secure ;
Conclusion : When a data member needs to be shared in a class, it should be defined as a static data member .
#include<iostream>
using namespace std;
class box
{
public:
int width; int length;
static int height;
box(int w, int l);
int volume();
};
int box::height = 10;
box::box(int w, int l)
{
width = w;
length = l;
}
int box::volume()
{
return width * length * height;
}
int main()
{
box a(10, 20), b(20, 30);
cout << a.height << endl;
cout << b.height << endl;
cout << a.volume() << endl;
cout << b.volume() << endl;
return 0;
}
Storage of static data members :
Separate storage space outside all objects , As long as static data members are defined in the class , Even if no object is defined , Space is also allocated for static data members , It can refer to... By class name when the object has not been established .
After using static data members in a class , As long as an object changes its value , The value of the corresponding static data member of the object of the whole class changes .
Effective solutions :
Static member functions —— Access static data members
Definition of static member function :
static Return type Function name ( parameter list );
{
… };
Such as : static float averge( )
{return (sum/3) ;}
Static member function call :
1) Static member functions belong to a class , Call... With the class name .
Format : Class name :: Static member function name ( Argument table );
2) Static members can call... With objects .
Format : Object name . Static member function name ( Argument table );
explain :
1) Static member functions Sure Define... Inside a class , You can also define... Outside the class .
Such as : Class : static float averge();
Off class definition : float student::averge( )
{return (sum/3); }
2) Static member function: none this The pointer (this A pointer belongs to an object , Static members belong to a class ), Mainly used to access static data members , Non static data members cannot be accessed directly , If you want to access non static data members , Object name is required .
3) Ordinary member functions can call static member functions ;
4) Constructors and destructors cannot be static .
Conclusion : When static data members are defined in a class , In general, you should define static member functions to access static data members .
#include<iostream>
using namespace std;
class student
{
public:
student(int n,string m,float s):num(n),name(m),score(s){}
void total();
static float averge();
private:
int num;
string name;
float score;
static float sum;
};
float student::sum = 0;
void student::total() { sum += score; }
float student::averge() { return (sum / 3); }
int main()
{
student stu[3] = {student(1,"zhangsan",89),student(2,"lisi",85),student(3,"wangwu",75)};
for (int i = 0; i < 3; ++i)
{
stu[i].total();
}
cout << "averge=" << student::averge() << endl;
return 0;
}
版权声明
本文为[*Flowers bloom on the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547207043.html
边栏推荐
猜你喜欢
类的继承与派生
Robocode教程7——雷达锁定
从源代码到可执行文件的过程
SQL sorts according to the specified content
[ThreadX] h743zi + lan8720 + ThreadX + netx duo transplantation
Explanation of the second I interval of 2020 Niuke summer multi school training camp
Explanation of login page
利用文件保存数据(c语言)
PM2 deploy nuxt project
Detailed arrangement of knowledge points of University probability theory and mathematical statistics
随机推荐
爬西瓜视频url
SVN简单操作命令
P1586 solution to tetragonal theorem
渔网道路密度计算
for()循环参数调用顺序
Rust:如何 match 匹配 String 字符串?
Rust 中的 RefCell
类和对象的初始化(构造函数与析构函数)
SQL -- data definition
Excel打开超大csv格式数据
代理服务器
Rust 的 Box指针
Arcpy为矢量数据添加字段与循环赋值
从源代码到可执行文件的过程
Make your own small program
Matlab标定板角点检测原理
C语言输入和输出(printf和scanf函数、putchar和getchar函数)
Rust 中的 Rc智能指针
根据SQL语句查询出的结果集,将其封装为json
PM2 deploy nuxt project