面试
来源:互联网 发布:mac 虚拟机 天正 编辑:程序博客网 时间:2024/05/22 08:03
大拿科技 智启物联
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 Cconstructor sub Dsup C fun()sub D fun2()Destory sup C