静态数据成员和静态成员函数在类中的应用举例

来源:互联网 发布:淘宝客服常见问题 编辑:程序博客网 时间:2024/05/26 22:55

静态数据成员有着和程序一致的生命周期,即当程序开始运行时为其分配一个内存空间,直到程序终止时,内存空间才会被释放。在其整个生命周期内,它的值一直存在,并且在其作用域内可以对其进行读写操作。

1.静态数据成员

#include <iostream>using namespace std;class CPoint{private:  //私有数据成员    int x,y;    static int count;  //静态数据成员,用于记录点的个数,为所有对象共用public:  //外部接口    CPoint(int x=0,int y=0):x(x),y(y){    count++;  //在构造函数中对count累加,所有对象共同维护一个count    }    CPoint(CPoint &p)  //复制构造函数    {        x=p.x;        y=p.y;        count++;    }    ~CPoint(){count--;}    int GetX(){return x;}    int GetY(){return y;}    void ShowCount()  //测试函数,用于显示有多少点    {        cout<<"Object Count = "<<count<<endl;    }};int CPoint::count=0;  //静态数据成员的初始化,使用类名限定int main(){    CPoint a(4,5);  //定义一个对象a,其构造函数使count加1    cout<<"CPoint a:"<<a.GetX()<<","<<a.GetY()<<"\t"<<"Total CPoint:";    a.ShowCount();    cout<<endl;    CPoint b(a);  //定义一个对象b,其拷贝构造函数使其count值加1    cout<<"CPoint b:"<<b.GetX()<<","<<b.GetY()<<"\t"<<"Total CPoint:";    b.ShowCount();    return 0;}

运行结果:



2.静态成员函数:

class CPoint{private:  //私有数据成员    int x,y;    static int count;  //静态数据成员,用于记录点的个数,为所有对象共用public:  //外部接口    CPoint(int x=0,int y=0):x(x),y(y){    count++;  //在构造函数中对count累加,所有对象共同维护一个count    }    CPoint(CPoint &p)  //复制构造函数    {        x=p.x;        y=p.y;        count++;    }    ~CPoint(){count--;}    int GetX(){return x;}    int GetY(){return y;}   static void ShowCount()  //测试函数,用于显示有多少点    {        cout<<"Object Count = "<<count<<endl;    }};

测试代码:

int main(){    CPoint::ShowCount();    CPoint a(4,5);  //定义一个对象a,其构造函数使count加1    cout<<"CPoint a:"<<a.GetX()<<","<<a.GetY()<<"\t"<<"Total CPoint:";    a.ShowCount();    CPoint::ShowCount();    cout<<endl;    CPoint b(a);  //定义一个对象b,其拷贝构造函数使其count值加1    cout<<"CPoint b:"<<b.GetX()<<","<<b.GetY()<<"\t"<<"Total CPoint:";    b.ShowCount();    CPoint::ShowCount();    return 0;}

此处对成员函数的调用可以通过类对象的方式调用,如a.ShowCount(),也可以用过类的作用域限定符的方式来调用,如CPoint::ShowCount(),二者效果一样:


3.一个综合实例:

“鱼额宝”类可以记录账户余额、存钱、取钱、计算利息。该类中有一个私有静态成员变量profitRate存储鱼额宝的利率,可以用共有静态成员函数setProfitRate修改利率的值。程序输入为第1天至第n天连续n天的账户操作,每天只能进行一次账户操作,或存或取,每一天产生的利息是前一天的账户余额与鱼额宝利率的乘积,产生的利息当天也将存入账户余额,由于第1天之前账户不存在,所以第1天一定是新建账户并存钱,且当天不会有利息存入余额。程序在接受n天操作的输入后,要求计算出第n天操作完成后的账户余额并输出。

类实现如下:

class Yuebao{private:    double funds;    static double profitRate;public:    Yuebao(double x):funds(x){}    static void setProfitRate(double rate)    {        profitRate=rate;    }    void addProfit()    {        funds=funds+funds*profitRate;    }    void deposit(double amount)    {        funds+=amount;    }    void withdraw(double amount)    {        funds-=amount;    }    double getBalance()    {        return funds;    }};

测试代码:

double Yuebao::profitRate=0;  //静态数据成员需要在类的外部,使用之前初始化。int main(){    int n;    while(cin >> n)    {        double profitRate;        cin >> profitRate;        Yuebao::setProfitRate(profitRate);//设定鱼额宝的利率        Yuebao y(0); //新建鱼额宝账户,余额初始化为0        int operation;//接受输入判断是存还是取        double amount;//接受输入存取金额        for (int i = 0; i < n; ++i)        {            y.addProfit();//加入前一天余额产生的利息            cin >> operation >> amount;            if (operation == 0)                y.deposit(amount);//存入金额            else                y.withdraw(amount);//取出金额        }        cout << y.getBalance() << endl;//输出最终账户余额    }    return 0;}

样例输入

30.10 100 101 10
样例输出
13.1

来自清华大学MOOC课件

0 0