当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
1006 finding a mex (hdu6756)
C array
【UDS统一诊断服务】二、网络层协议(1)— 网络层概述与功能
【UDS统一诊断服务】一、诊断概述(4)— 基本概念和术语
GNU EFI header file
selenium+webdriver+chrome实现百度以图搜图
Rust:如何实现一个线程池?
Rust: Tcp 服务器与客户端的一个简单例子
Export of data
Rust 的多线程安全引用 Arc
Excel打开超大csv格式数据
爬取手游网站游戏详情和评论(MQ+多线程)
斯坦福机器学习课程汇总
Rust的闭包类型(Fn, FnMut, FnOne的区别)
Jeu de devinettes
爬取彩票数据
【UDS统一诊断服务】二、网络层协议(2)— 数据传输规则(单帧与多帧)
Flask - 中间件
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
渔网道路密度计算









