C++ 静态成员函数
static是C和C++编程语言中的关键字。我们使用static关键字来定义类的静态数据成员或静态成员函数。让我们通过程序来了解静态数据成员和静态成员函数。
静态数据成员
当我们使用static关键字定义类的数据成员时,这些数据成员被称为静态数据成员。静态数据成员类似于静态成员函数,因为只能使用静态数据成员或静态成员函数来访问静态数据。而且,类的所有对象共享相同的静态成员副本来访问静态数据。
语法
static data_type data_member;
在这里, static 是预定义库的关键字。
data_type 是C++中的变量类型,如int、float、string等。
data_member 是静态数据的名称。
示例1: 我们来创建一个简单的程序来访问C++编程语言中的静态数据成员。
#include
#include
using namespace std;
// create class of the Car
class Car
{
private:
int car_id;
char car_name[20];
int marks;
public:
// declare a static data member
static int static_member;
Car()
{
static_member++;
}
void inp()
{
cout << " \n\n Enter the Id of the Car: " << endl;
cin >> car_id; // input the id
cout << " Enter the name of the Car: " << endl;
cin >> car_name;
cout << " Number of the Marks (1 - 10): " << endl;
cin >> marks;
}
// display the entered details
void disp ()
{
cout << " \n Id of the Car: " << car_id;
cout << "\n Name of the Car: " << car_name;
cout << " \n Marks: " << marks;
}
};
// initialized the static data member to 0
int Car::static_member = 0;
int main ()
{
// create object for the class Car
Car c1;
// call inp() function to insert values
c1. inp ();
c1. disp();
//create another object
Car c2;
// call inp() function to insert values
c2. inp ();
c2. disp();
cout << " \n No. of objects created in the class: " << Car :: static_member <
输出
Enter the Id of the Car:
101
Enter the name of the Car:
Ferrari
Number of the Marks (1 - 10):
10
Id of the Car: 101
Name of the Car: Ferrari
Marks: 10
Enter the Id of the Car:
205
Enter the name of the Car:
Mercedes
Number of the Marks (1 - 10):
9
Id of the Car: 205
Name of the Car: Mercedes
Marks: 9
No. of objects created in the class: 2
静态成员函数
静态成员函数是用来访问静态数据成员或其他静态成员函数的特殊函数。成员函数使用static关键字来定义。静态成员函数共享类的任何数量对象的单个成员函数的副本。我们可以使用类名或类的对象访问静态成员函数。如果静态成员函数访问任何非静态数据成员或非静态成员函数,它会引发错误。
语法
class_name::function_name (parameter);
在这里, class_name 是类的名称。
function_name :函数名是静态成员函数的名称。
parameter :它定义了传递参数给静态成员函数的名称。
示例2: 让我们创建另一个程序,在C++编程语言中使用类名访问静态成员函数。
#include
using namespace std;
class Note
{
// declare a static data member
static int num;
public:
// create static member function
static int func ()
{
return num;
}
};
// initialize the static data member using the class name and the scope resolution operator
int Note :: num = 5;
int main ()
{
// access static member function using the class name and the scope resolution
cout << " The value of the num is: " << Note:: func () << endl;
return 0;
}
输出
The value of the num is: 5
示例3: 让我们创建另一个程序,使用C++编程语言中的类对象来访问静态成员函数。
#include
using namespace std;
class Note
{
// declare a static data member
static int num;
public:
// create static member function
static int func ()
{
cout << " The value of the num is: " << num << endl;
}
};
// initialize the static data member using the class name and the scope resolution operator
int Note :: num = 15;
int main ()
{
// create an object of the class Note
Note n;
// access static member function using the object
n.func();
return 0;
}
输出
The value of the num is: 15
示例 4: 让我们来考虑一个例子,使用对象和类来访问C++编程语言中的静态成员函数。
#include
using namespace std;
class Member
{
private:
// declaration of the static data members
static int A;
static int B;
static int C;
// declare public access specifier
public:
// define the static member function
static void disp ()
{
cout << " The value of the A is: " << A << endl;
cout << " The value of the B is: " << B << endl;
cout << " The value of the C is: " << C << endl;
}
};
// initialization of the static data members
int Member :: A = 20;
int Member :: B = 30;
int Member :: C = 40;
int main ()
{
// create object of the class Member
Member mb;
// access the static member function using the class object name
cout << " Print the static member through object name: " << endl;
mb. disp();
// access the static member function using the class name
cout << " Print the static member through the class name: " << endl;
Member::disp();
return 0;
}
输出
Print the static member through object name:
The value of the A is: 20
The value of the B is: 30
The value of the C is: 40
Print the static member through the class name:
The value of the A is: 20
The value of the B is: 30
The value of the C is: 40