zoukankan      html  css  js  c++  java
  • c++学习-特殊类成员

    静态变量:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    
    public:
        
        A(){ total++; }
        static int total;
    
    };
    
    //@warn 静态成员变量必须在全局进行定义
    int A::total = 0; 
    
    void main()
    {
        A a1;
        A a2;
    
        cout << a1.total<< endl;
        cout << a2.total<< endl;
      cout << A::total << endl; }

    静态成员函数:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    
    public:
        A(){ total++; }
        static void show()
        {
            cout << total << endl;
        }
    
        void echo()
        {
            cout << total << endl;
        }
    
    private:
        static int total;
        int a;
    };
    
    int A::total = 10; 
    
    void main()
    {
        A::show();
    
        A a1;
        a1.echo();

    A a1,a2;
    a1.echo();
    a1.show();//也可以通过对象调用静态函数


    }

    继承后静态成员函数和方法仍旧可用:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    
    public:
        A(){ total++; }
        static void show()
        {
            cout << total << endl;
        }
    
        void echo()
        {
            cout << total << endl;
        }
    
    private:
        static int total;
        int a;
    };
    
    class B :public A{
    
    };
    
    int A::total = 10; 
    
    void main()
    {
        A::show();
        B::show();
    
    
    }

    静态成员函数不能说明为虚函数 

    int(*fun)(int);//声明一个函数指针,指向的函数的参数为int,返回值为int
    int*fun(int);//声明一个函数,函数的参数为int,返回值为int型指针

    函数指针使用:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    
    void main()
    {
        int(*p)(int, int);
        p = test1;
        cout << p(1, 2)<< endl;
    
        p = test2;
        cout << p(1, 2) << endl;
        
    
    }

    函数指针数组:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    
    void main()
    {
        int(*p[2])(int, int) = { test1, test2 };
    
        cout << p[0](1,2) << endl;
        cout << p[1](2, 3) << endl;
        
    
    }

    函数指针做参数:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    void show(int(*p)(int, int), int x,int y)
    {
        cout << p(x,y) << endl;
    }
    
    void main()
    {
        show(test1, 1,2);
        show(test2, 1, 2);
    }

    typedef来简化定义:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    typedef int(*p)(int, int); // p代表 int(*p)(int, int);
    
    int test1(int x,int y){
        return x + y;
    }
    
    int test2(int x, int y){
        return x * y;
    }
    
    void show(p vp, int x,int y)
    {
        cout << vp(x, y) << endl;
    }
    
    void main()
    {
        show(test1, 1,2);
        show(test2, 1, 2);
    }

    成员函数指针:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class A{
    public:
        int show(int x, int y){ return x*y; }
    };
    
    int(A::*p)(int, int);
    
    void main()
    {
        p = &A::show;
    
        A a1;
        (a1.*p)(1, 23); //调用
    
    }

    多态和成员函数指针:

    #include<iostream>
    #include<string>
    #include <typeinfo>  
    using namespace std;
    
    class human{
    public:
        virtual void say() = 0;
    };
    
    class father :public human{
    public:
        void say(){ cout << "father say:" << endl; }
    };
    
    class mother :public human{
    public:
        void say(){ cout << "mother say:" << endl; }
    };
    
    void main()
    {
        void (human::*fun)()=0;
        fun = &human::say;
    
        human *p;
        p = new father;
        (p->*fun)();
    
        p = new mother;
        (p->*fun)();
    
    }
  • 相关阅读:
    对用户控件(ascx)属性(property)赋值
    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
    图片淡入淡出切换效果
    在用户控件(ASCX)创建用户控件(ASCX)
    Login failed for user 'xxx'
    一些较好的书
    儿子购买的书
    怀念以前做网管的日子
    Linux下selinux简单梳理
    Rsync同步时删除多余文件 [附:删除大量文件方法的效率对比]
  • 原文地址:https://www.cnblogs.com/siqi/p/4641249.html
Copyright © 2011-2022 走看看