当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
相机标定:关键点法 vs 直接法
爬西瓜视频url
进程间通信-互斥锁
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
识别验证码
渔网道路密度计算
Rust 中的 Cell 共享可变指针
A solution to replace not in in SQL
How SYSTEMd uses / etc / init D script
实现一个计算m~n(m<n)之间所有整数的和的简单函数
Graduation project, viewing screenshots of epidemic psychological counseling system
Robocode教程3——Robo机器剖析
ArcGIS表转EXCEL超出上限转换失败
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
Flask - 中间件
Flask操作多个数据库
Robocode教程4——Robocode的游戏物理
pyppeteer爬虫
C # Foundation