C语言 c++ php mysql nginx linux lnmp lamp lanmp memcache redis 面试 笔记 ppt 设计模式 问题 远程连接

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)();

}

 

posted on 2015-07-12 17:48  思齐_  阅读(392)  评论(0编辑  收藏  举报