面试

大拿科技 智启物联

2016-9-6

1.下面程序的输入是:16 12

参见链接

struct A
{
    int a;
    short b;
    int c;
    char d;
} ;
struct B
{
    int a;
    short b;
    char d;
    int c;
};
void sizeofstruct()
{
    cout<<sizeof(A)<<endl<<sizeof(B)<<endl;
} 

2.下面程序的输出: 000000f7,fffffff7

void point()
{
    unsigned int a = 0xFFFFFFF7;
    unsigned char i = (unsigned char) a;
    char *b=(char*)&a;
    printf("%08x,%08x\n",i,*b);
}

3.模板函数与仿函数

#include<iostream>
#include<string>

using namespace std;
template<typename T,typename _PrintFunc>
void PrintAll(const T *values,int size ,_PrintFunc _print)
{
    for(int i=0;i<size;i++) _print(values[i]);
    cout << endl;
}
/*待输出的数组*/
int iV[5] = {1,2,3,4,5};
string sV[3]={"aaa","bbb","ccc"};
  • 定义一个模块打印函数,调用PrintAll并输出上面的数组iV,sV
template<typename T>
void Print(const T value)
{
    cout<< value;
}
//调用的时候使用
PrintAll(iV,5,Print<int>);
PrintAll(sV,3,Print<string>); 
  • 使用仿函数实现上面的打印函数,并调用PrintAll。

仿函数!!!

/*
** 仿函数: 
** 《C++标准程序库》一书中对仿函数的解释:
**  任何东西,只要其行为像函数,就可以称之为仿函数。
*/
class MyPrint    
{    
public:  
    template<typename T> 
    void operator()(T value) const    
    {    
        cout<< value;
    }    
};  
//调用
PrintAll(iV,5,myPrint);
PrintAll(sV,3,myPrint);

4.关于子类与父类,new 一个子类用父类接受。

{
    C *c = new D();
    c->fun();
    c->fun2();
    delete c;   
}

类的定义如下:

class C
{
public:
    C()
    {
        cout<<"constructor sup C"<<endl;
    }
    ~C()
    {
        cout<<"Destory sup C"<<endl;
    }
    void fun()
    {
        cout<< "sup C fun()" << endl;   
    } 
    virtual  void fun2() 
    {
        cout << "sup C fun2()" << endl;
    }
};
class D:public C
{
public:
    D()
    {
        cout<<"constructor sub D"<<endl;
    }
    ~D()
    {
        cout<<"Destory sub D"<<endl;
    }
    
    void fun()
    {
        cout<< "sub D fun()" << endl;   
    } 
    virtual  void fun2() 
    {
        cout << "sub D fun2()" << endl;
    }
};

输出:

constructor sup C
constructor sub D
sup C fun()
sub D fun2()
Destory sup C
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,754评论 18 399
  • 1.OC里用到集合类是什么? 基本类型为:NSArray,NSSet以及NSDictionary 可变类型为:NS...
    轻皱眉头浅忧思阅读 1,393评论 0 3
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,205评论 30 471
  • 小编费力收集:给你想要的面试集合 1.C++或Java中的异常处理机制的简单原理和应用。 当JAVA程序违反了JA...
    八爷君阅读 4,650评论 1 114
  • http://python.jobbole.com/85231/ 关于专业技能写完项目接着写写一名3年工作经验的J...
    燕京博士阅读 7,616评论 1 118