4. 以下代码有什么问题?[C++易] struct Test {
Test( int ) {} Test() {} void fun() {} };
void main( void ) {
Test a(1); a.fun(); Test b(); b.fun(); }
5. 以下代码有什么问题?[C++易] cout << (true?1:\
8. 以下代码能够编译通过吗,为什么?[C++易] unsigned int const size1 = 2; char str1[ size1 ];
unsigned int temp = 0; cin >> temp;
unsigned int const size2 = temp; char str2[ size2 ];
9. 以下代码中的输出语句输出0吗,为什么?[C++易] struct CLS {
int m_i;
CLS( int I ) : m_i(i) {} CLS() {
CLS(0); } };
CLS obj;
cout << obj.m_i << endl;
10. C++中的空类,默认产生哪些类成员函数?[C++易] 答:
class Empty {
public:
Empty(); // 缺省构造函数 Empty( const Empty& ); // 拷贝构造函数 ~Empty(); // 析构函数 Empty& operator=( const Empty& ); // 赋值运算符 Empty* operator&(); // 取址运算符 const Empty* operator&() const; // 取址运算符 const };
3. 以下两条输出语句分别输出什么?[C++难] float a = 1.0f;
cout << (int)a << endl; cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么? Float b = 0.0f;
cout << (int)b << endl; cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?
2. 以下反向遍历array数组的方法有什么错误?[STL易] vector array;
array.push_back( 1 ); array.push_back( 2 ); array.push_back( 3 );
for( vector::size_type i=array.size()-1; i>=0; --I ) // 反向遍历array数组 {
cout << array[i] << endl; }
6. 以下代码有什么问题?[STL易] typedef vector IntArray; IntArray array;
array.push_back( 1 ); array.push_back( 2 ); array.push_back( 2 ); array.push_back( 3 );
// 删除array数组中所有的2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor ) {
if( 2 == *itor ) array.erase( itor ); }
11. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
答:
程序代码 程序代码
void* mymemcpy( void *dest, const void *src, size_t count ) {
char* pdest = static_cast
const char* psrc = static_cast
if( pdest>psrc && pdest for( size_t i=count-1; i!=-1; --I ) pdest[i] = psrc[i]; } else { for( size_t i=0; i return dest; } int main( void ) { char str[] = \ mymemcpy( str+1, str+0, 9 ); cout << str << endl; system( \ return 0; } C++面试题集锦 2006-7-7更新 29.编写一个函数,函数接收一个字符串,是由十六进制数组成的一组字符串,函数的功能是 把接到的这组字符串转换成十进制数字.并将十进制数字返回. 30.编写一个函数将一条字符串分成两部分,将前半部分按ASCII码升序排序,后半部分不 变 ,(如果字符串是奇数则中间的字符不变,)最后再将前后两部分交换,然后将该字符串 输 出, 测试字符串“ADZDDJKJFIEJHGI” 31.找错 Void test1() { char string[10]; char* str1=\strcpy(string, str1); } Void test2() { char string[10], str1[10]; for(I=0; I<10;I++) { str1[i] ='a'; } strcpy(string, str1); } Void test3(char* str1) { char string[10]; if(strlen(str1)<=10) { strcpy(string, str1); } } 32. 找错 #define MAX_SRM 256 DSN get_SRM_no() { static int SRM_no; int I; for(I=0;I{ SRM_no %= MAX_SRM; if(MY_SRM.state==IDLE) { break; } } if(I>=MAX_SRM) return (NULL_SRM); else return SRM_no; } 33. 写出程序运行结果 int sum(int a) { auto int c=0; static int b=3; c+=1; b+=2; return(a+b+C); } void main() { int I; int a=2; for(I=0;I<5;I++) { printf(\} } 34. int func(int a) { int b; switch(a) { case 1: 30; case 2: 20; case 3: 16; default: 0 } return b; } 则func(1)=? 35: int a[3]; a[0]=0; a[1]=1; a[2]=2;