欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++-static

程序员文章站 2022-06-11 15:55:38
static成员变量 ......

static成员变量

#define _crt_secure_no_warnings
#include <iostream>

using namespace std;

class aa
{
public:
    aa(int a, int b)
    {
        m_a = a;
        m_b = b;
    }



    int getc()
    {
        m_c++;
        return m_c;
    }
    
    //静态的成员方法
    static int& getcc()
    {
        return m_c;
    }

private:
    //static修饰的静态成员变量
    static int m_c;
    int m_a;
    int m_b;
};

//静态成员变量的初始化,一定类的外边。
int aa::m_c = 100;



int main(void)
{
    aa a1(10, 20);
    aa a2(100, 200);

    cout << a1.getc() << endl;//101
    cout << a2.getc() << endl;//102

    //a1.getcc() = 200;
    aa::getcc() = 200;

    cout << a1.getc() << endl;//201
    cout << a2.getc() << endl;//202

    return 0;
}

static成员变量的练习

#define _crt_secure_no_warnings
#include <iostream>


using namespace std;

class box
{
public:
    box(int l, int w)
    {
        len = l;
        width = w;
    }

    int volume()
    {
        int v = len *width*hight;

        cout << "高度是" << hight << endl;
        cout << "体积是" <<  v << endl;

        return v;
    }

    static void changehight(int h)
    {
        hight = h;
    }
private:
    int len;
    int width;
    static int hight;

};

int box::hight = 100;



int main(void)
{
    box b1(10, 20);
    box b2(100, 200);

    b1.volume();
    b2.volume();

    box::changehight(300);

    b1.volume();
    b2.volume();


    return 0;
}

static练习求平均分

#define _crt_secure_no_warnings
#include <iostream>


using namespace std;

class box
{
public:
    box(int l, int w)
    {
        len = l;
        width = w;
    }

    int volume()
    {
        int v = len *width*hight;

        cout << "高度是" << hight << endl;
        cout << "体积是" <<  v << endl;

        return v;
    }

    static void changehight(int h)
    {
        hight = h;
    }
private:
    int len;
    int width;
    static int hight;

};

int box::hight = 100;



int main(void)
{
    box b1(10, 20);
    box b2(100, 200);

    b1.volume();
    b2.volume();

    box::changehight(300);

    b1.volume();
    b2.volume();


    return 0;
}

static成员变量占用大小

static不占实际空间

#include    <iostream>
using namespace std;

class   c1
{
public:
    int i;      //4
    int j;      //4
    int k;      //4
};  //12

class   c2
{
public:
    int i;
    int j;
    int k;
    static int  m;                                                                          //4
public:
    int getk()  const   { return    k; }        //4
    void    setk(int    val)    { k = val; }    //4
};


struct  s1
{
    int i;
    int j;
    int k;
};  //12

struct  s2
{
    int i;
    int j;
    int k;
    static int  m;
};  //12?

int main()
{
    cout << "c1 :   " << sizeof(c1) << endl;
    cout << "c1 :   " << sizeof(c2) << endl;

    c2 c1, c2;

    //c1.getk();  //为什么会return c1.k 
    //c2.getk(); // 为什么会return c2.k

    cout << " ----- " << endl;

    cout << "c1 :   " << sizeof(s1) << endl;
    cout << "c1 :   " << sizeof(s2) << endl;

    return 0;
}

static仓库进出练习

#define _crt_secure_no_warnings
#include <iostream>


using namespace std;


class goods
{
public:
    goods()
    {
        weight = 0;
        next = null;
        cout << "创建了一个重量为" << weight << "的货物" << endl;
    }

    goods(int w) {
        //需要创建一个w的货物,并且仓库加上这个重量
        weight = w;
        next = null;
        total_weight += w;
        cout << "创建了一个重量为" << weight << "的货物" << endl;
    }

    ~goods() {
        //仓库减少这个货物的重量
        cout << "删除了一箱重量是" << weight << "的货物" << endl;
        total_weight -= weight;
    }


    static int get_total_weight()
    {
        return total_weight;
    }

    goods *next;
private:
    int weight;//重量
    static int total_weight;//仓库的总重量
};

int goods::total_weight = 0;


void buy(goods * &head, int w)
{
    //创建一个货物 重量是w
    goods *new_goods = new goods(w);

    if (head == null) {
        head = new_goods;
    }
    else {
        new_goods->next = head;
        head = new_goods;
    }

}

void sale(goods * &head)
{
    if (head == null) {
        cout << "仓库中已经没有货物了。。" << endl;
        return;
    }

    goods *temp = head;
    head = head->next;

    delete temp;
    cout << "saled." << endl;
}



int main(void)
{
    int choice = 0;
    goods *head = null;
    int w;

    do {
        cout << "1 进货" << endl;
        cout << "2 出货" << endl;
        cout << "0 退出" << endl;

        cin >> choice;
        switch (choice)
        {
        case 1:
            //进货
            cout << "请输出要创建货物的重量" << endl;
            cin >> w;
            buy(head, w);
            break;
        case 2:
            //出货
            sale(head);
            break;
        case 0:
            //退出
            return 0;
        default:
            break;
        }

        cout << "当前仓库的总重量是"<<goods::get_total_weight() << endl;

    } while (1);
    
    return 0;
}