鍍金池/ 教程/ C++/ C++ static關(guān)鍵字
C++用戶定義異常
C++ this指針
C++存儲類
C++函數(shù)
C++交換變量值
C++ continue語句
C++注釋
C++函數(shù)遞歸
C++ goto語句
C++函數(shù)-通過值調(diào)用和引用調(diào)用
C++重載
C++語言歷史
C++反轉(zhuǎn)數(shù)字
C++ try/catch語句
C++是什么?
C++變量
C++ break語句
C++運算符
C++第一個程序
C++繼承
C++虛函數(shù)
C++將十進制轉(zhuǎn)換為二進制
C++矩陣乘法
C++對象和類
C++基礎(chǔ)輸入輸出(cin,cout,endl)
C++文件和流
C++求素數(shù)
C++ if/else語句
C++友元函數(shù)
C++命名空間
C++面向?qū)ο蟾拍?/span>
C++階乘
C++關(guān)鍵字
C++重載
C++聚合
C++結(jié)構(gòu)體
C++的特點(特性)
C++打印字母表三角
C++ switch語句
C++多態(tài)
C++ do-while循環(huán)
C++字符串
C++ static關(guān)鍵字
C++錯誤處理
C++ for循環(huán)
C語言與C++的區(qū)別
C++ while循環(huán)
C++開發(fā)環(huán)境的安裝
linux環(huán)境下編譯C++ 程序
C++枚舉
C++指針
C++斐波納契數(shù)列
C++阿姆斯壯數(shù)字
C++接口
C++教程
C++數(shù)組
C++數(shù)據(jù)抽象
C++回文程序?qū)嵗?/span>
C++打印數(shù)字三角
C++將數(shù)組傳遞到函數(shù)
C++多維數(shù)組
C++將數(shù)字轉(zhuǎn)換字符

C++ static關(guān)鍵字

在C++中,static是屬于類而不是實例的關(guān)鍵字或修飾符。 因此,不需要實例來訪問靜態(tài)成員。 在C++中,static可以是字段,方法,構(gòu)造函數(shù),類,屬性,操作符和事件。

C++ static關(guān)鍵字的優(yōu)點

內(nèi)存效率: 現(xiàn)在我們不需要創(chuàng)建實例來訪問靜態(tài)成員,因此它節(jié)省了內(nèi)存。 此外,它屬于一種類型,所以每次創(chuàng)建實例時不會再去獲取內(nèi)存。

C++靜態(tài)字段

使用static關(guān)鍵字聲明字段稱為靜態(tài)字段。它不像每次創(chuàng)建對象時都要獲取內(nèi)存的實例字段,在內(nèi)存中只創(chuàng)建一個靜態(tài)字段的副本。它被共享給所有的對象。

它用于引用所有對象的公共屬性,如:Account類中的利率(rateOfInterest),Employee類中的公司名稱(companyName)等。

C++靜態(tài)字段示例

下面來看看看C++中靜態(tài)(static)字段的簡單示例。

#include <iostream>  
using namespace std;  
class Account {  
   public:  
       int accno; //data member (also instance variable)      
       string name; //data member(also instance variable)  
       static float rateOfInterest;   
       Account(int accno, string name)   
        {    
             this->accno = accno;    
            this->name = name;    
        }    
       void display()    
        {    
            cout<<accno<< "<<name<< " "<<rateOfInterest<<endl;   
        }    
};  
float Account::rateOfInterest=6.5;  
int main(void) {  
    Account a1 =Account(201, "Sanjay"); //creating an object of Employee   
    Account a2=Account(202, "Calf"); //creating an object of Employee  
    a1.display();    
    a2.display();    
    return 0;  
}

上面代碼執(zhí)行結(jié)果如下-

201 Sanjay 6.5
202 Calf 6.5

C++靜態(tài)字段示例:統(tǒng)計對象數(shù)量

下面來看看看C++中static關(guān)鍵字的另一個例子,統(tǒng)計創(chuàng)建對象的數(shù)量。

#include <iostream>  
using namespace std;  
class Account {  
   public:  
       int accno; //data member (also instance variable)      
       string name;   
       static int count;     
       Account(int accno, string name)   
        {    
             this->accno = accno;    
            this->name = name;    
            count++;  
        }    
       void display()    
        {    
            cout<<accno<<" "<<name<<endl;   
        }    
};  
int Account::count=0;  
int main(void) {  
    Account a1 =Account(201, "Sanjay"); //creating an object of Account  
    Account a2=Account(202, "Calf");   
     Account a3=Account(203, "Ranjana");  
    a1.display();    
    a2.display();    
    a3.display();    
    cout<<"Total Objects are: "<<Account::count;  
    return 0;  
}

上面代碼執(zhí)行結(jié)果如下-

201 Sanjay
202 Calf
203 Ranjana
Total Objects are: 3