static 函数 、const 函数小结(实战篇)

static 函数小结

#include<iostream>
#include<stdio.h>
using namespace std;

class  point 
{

    public:

    void show(void)                        //普通方法可以访问访问static data 
    {
        cout << "x = "<< x <<endl;
        cout << "y =  "<< y<<endl;
        cout<< " flag = "<<flag<<endl;
    }

    public:
        point()
        {}
        point(int x,int y):x(x),y(y)
        {}
        point (point &sd)
        {
            x = sd.x;
            y = sd.y;
            flag = sd.flag;

        }
        point &operator = (point &sd)
        {
            if(this != &sd)
            {
                x= sd.x;
                y = sd.y;
                flag = sd.flag;
            }
            return *this;

        }
        ~point()
        {}
    private:
        int x;
        int y;
        static int flag;      //静态数据,他不属于某一个对象的,因为他封闭了this指针
                               //所有对象共享一份数据,

};




int point ::  flag  = 100;    //静态数据成员这样初始化,注:必须初始化
int main()
{
      point a(1,3);
      a.show();

      return 0;
}

运行结果:

x = 1
y =  3
 flag = 100
Press any key to continue

1.static 成员数据 ,可以由普通方法来访问
2.static 必须初始化,并且初始化不同于普通变量初始化。
3.static 函数(封锁了this 指针) 只能访问static data 数据。

#include<iostream>
#include<stdio.h>
using namespace std;

class  point 
{

    public:

/*  void show(void)                        //普通方法可以访问访问static data 
    {
        cout << "x = "<< x <<endl;
        cout << "y =  "<< y<<endl;
        cout<< " flag = "<<flag<<endl;
    }

*/

    static  void show(void )
    {
        cout << "flag = "<<flag<<endl;

    }
    public:
        point()
        {}
        point(int x,int y):x(x),y(y)
        {}
        point (point &sd)
        {
            x = sd.x;
            y = sd.y;
            flag = sd.flag;

        }
        point &operator = (point &sd)
        {
            if(this != &sd)
            {
                x= sd.x;
                y = sd.y;
                flag = sd.flag;
            }
            return *this;

        }
        ~point()
        {}
    private:
        int x;
        int y;
        static int flag;      //静态数据,他不属于某一个对象的,因为他封闭了this指针
                               //所有对象共享一份数据,

};




int point ::  flag  = 100;    //静态数据成员这样初始化,注:必须初始化
int main()
{
      point a(1,3);
      a.show();
      point ::show();

      return 0;
}



运行结果:

flag = 100
flag = 100
Press any key to continue

两种调用方式:

1 . 类名::static 函数
2 . 实例化一个对象.进行调用



#include<iostream>
using namespace std;
class Student
{
public:
    Student(int n,float s):num(n),score(s){}
    void change() const                     
    {
       cout << "num =  "<<num++ <<endl;    //表示对象里的数据不能改变
    }
    void display() const
    {
        cout<<num<<"\t"<<score<<endl;
    }
    private:
    const int num;
    const  float score;
} ;
int main()
{
     Student const stud(101,78.5);
    stud.display();
    stud.change();
    stud.display();
    return 0;
};


Compiling...
u.cpp
D:\VC++\Microsoft Visual Studio\MyProjects\uuu\u.cpp(66) : error C2166: l-value specifies **const object**
执行 cl.exe 时出错.


#include<iostream>
using namespace std;
class Student
{
public:
    Student(int n,float s):num(n),score(s){}
    void change(int n,float s) const
    {
        num=n;
        score=s;
    }
    void display() const
    {
        cout<<num<<"\t"<<score<<endl;
    }
    private:
    mutable int num;
    mutable float score;
} ;
int main()
{
     Student const stud(101,78.5);
    stud.display();
    stud.change(101,80.5);
    stud.display();
    return 0;
};


101     78.5
101     80.5
Press any key to continue
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值