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

猜你喜欢

转载自www.cnblogs.com/sea-stream/p/9926964.html
今日推荐