大拿科技 智啟物聯(lián)
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.模板函數(shù)與仿函數(shù)
#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;
}
/*待輸出的數(shù)組*/
int iV[5] = {1,2,3,4,5};
string sV[3]={"aaa","bbb","ccc"};
- 定義一個(gè)模塊打印函數(shù),調(diào)用PrintAll并輸出上面的數(shù)組iV,sV
template<typename T>
void Print(const T value)
{
cout<< value;
}
//調(diào)用的時(shí)候使用
PrintAll(iV,5,Print<int>);
PrintAll(sV,3,Print<string>);
- 使用仿函數(shù)實(shí)現(xiàn)上面的打印函數(shù),并調(diào)用PrintAll。
仿函數(shù)!!!
/*
** 仿函數(shù):
** 《C++標(biāo)準(zhǔn)程序庫(kù)》一書中對(duì)仿函數(shù)的解釋:
** 任何東西,只要其行為像函數(shù),就可以稱之為仿函數(shù)。
*/
class MyPrint
{
public:
template<typename T>
void operator()(T value) const
{
cout<< value;
}
};
//調(diào)用
PrintAll(iV,5,myPrint);
PrintAll(sV,3,myPrint);
4.關(guān)于子類與父類,new 一個(gè)子類用父類接受。
{
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