1. 程式人生 > >c++ 靜態函式

c++ 靜態函式

 

//物件與物件之間的成員變數是相互獨立的.要想共用資料,則需要使用靜態成員或靜態方法
//#只要在類中宣告靜態成員變數,即使不定義物件,也可以為靜態成員變數分配空間,進而可以使用靜態成員變數.(因為靜態成員變數在物件建立之前就已經被分配了記憶體空間)
//#靜態成員變數雖然在類中,但它並不是隨物件的建立而分配空間的,也不是隨物件的撤銷而釋放(一般的成員在物件建立時會分配空間,在物件撤銷時會釋放).靜態成員變數是在程式編譯時分配空間,而在程式結束時釋放空間.
//#初始化靜態成員變數要在類的外面進行.初始化的格式如下:資料型別 類名::靜態成員變數名 = 初值;
//#不能用引數初始化表,對靜態成員變數進行初始化.
//#即可以通過類名來對靜態成員變數進行引用,也可以通過物件名來對靜態成員變數進行引用.

//普通成員函式和靜態成員函式的區別是:普通成員函式在引數傳遞時編譯器會隱藏地傳遞一個this指標.通過this指標來確定呼叫類產生的哪個物件;但是靜態成員函式沒有this指標,不知道應該訪問哪個物件中的資料,所以在程式中不可以用靜態成員函式訪問類中的普通變數.
#include <iostream>
using namespace std;
class CShop
{
public:
    CShop(int size);
    void ShowSize();
    static void ShowPrice(); //宣告靜態成員函式用來顯示價格
    static
int ChangePrice(int price); //宣告靜態成員函式用來更改價格 private: int m_size; //宣告一個私有成員變數 static int m_price; //宣告一個私有靜態成員變數 }; CShop::CShop(int size) { m_size = size; } void CShop::ShowSize() { cout << "商品數量:" << m_size << endl; } void CShop::ShowPrice() { cout << "商品價格:
" << m_price << endl; } int CShop::ChangePrice(int price) { m_price = price; return m_price; } int CShop::m_price = 100; //初始化靜態成員變數 int main(int argc, char * argv[]) { CShop::ShowPrice(); CShop::ChangePrice(200); CShop::ShowPrice(); CShop shop(50); shop.ShowSize(); shop.ShowPrice(); return 0; }

 

 

#include<iostream>
#include<string>
using namespace std;
class player {
    string playerName;  //玩家姓名
    int attack; //攻擊力
    int defense;  //防禦力
    int health = 100;  //生命值 
    int maxHealth = 100; //最大生命值
public:
    static int playerNumber;
    //建構函式 總數+1
    player(int attackParam, int defenseParam, string nameParam) {
        attack = attackParam;
        defense = defenseParam;
        playerName = nameParam;
        ++playerNumber;
    }
    //析構 總數減1
    ~player() {
        --playerNumber;
    }
    //獲取攻擊力
    int getAttack() {
        return attack;
    }
    //設定攻擊力
    void setAttack(int attackParam) {
        attack = attackParam;
    }
    //獲取防禦力
    int getDefense() {
        return defense;
    }
    //設定防禦力
    void setDefense(int defenseParam) {
        defense = defenseParam;
    }
    //改變生命值
    void changeHealth(int healthParam) {
        health += healthParam;
        if (health > maxHealth)
        {
            health = maxHealth;
        }
        else if (health <= 0)
        {
            health = 0;
            cout << "Player " << playerName << "  is Dead ! " << endl;
            delete this;
        }
    }
    //顯示屬性
    void displayProperty() {
        cout << "名字:" << playerName << endl;
        cout << "攻擊力:" << attack << endl;
        cout << "防禦力:" << defense << endl;
        cout << "生命值:" << health << endl;
    }
    void attackPlayer(player*  otherPlayer) {
        otherPlayer->changeHealth(-this->attack);
    }
    static int displayNumber() {
        cout << playerNumber << endl;
        return playerNumber;
    }
};
int player::playerNumber = 0;
int main() {
    player* player01 = new player(50, 50, "GSL");
    player* player02 = new player(70, 60, "LJL");
    player01->displayProperty();
    player02->displayProperty();
 
    if (player01)
    {
        player01->attackPlayer(player02);
    }
    if (player02)
    {
        player02->attackPlayer(player01);
    }
    cout << player::playerNumber << endl;
    if (player01)
    {
        player01->attackPlayer(player02);
    }
    if (player02)
    {
        player02->attackPlayer(player01);
    }
    cout << player::playerNumber << endl;
    system("pause");
    return 9696969;
}

 

https://blog.csdn.net/ttt301/article/details/52326067

https://blog.csdn.net/longyanbuhui/article/details/71404308