- class A
- {
- };
- class B
- {
- public:
- B();
- virtual ~B();
- };
- class C
- {
- private:
- #pragma pack(4)
- int i;
- short j;
- float k;
- char l[64];
- long m;
- char *p;
- #pragma pack()
- };
- class D
- {
- private:
- #pragma pack(1)
- int i;
- short j;
- float k;
- char l[64];
- long m;
- char *p;
- #pragma pack()
- };
- int main(void)
- {
- printf("%d\n",sizeof(A));
- printf("%d\n",sizeof(B));
- printf("%d\n",sizeof(C));
- printf("%d\n",sizeof(D));
- return 0;
- }
3、以下程序在32位机器下运行的结果是()
- #pragma pack(4)
- struct info_t
- {
- unsigned char version;
- unsigned char padding;
- unsigned char extension;
- unsigned char count;
- unsigned char marker;
- unsigned char payload;
- unsigned short sequence;
- unsigned int timestamp;
- unsigned int ssrc;
- };
- union info_u
- {
- unsigned char version;
- unsigned char padding;
- unsigned char extension;
- unsigned char count;
- unsigned char marker;
- unsigned char payload;
- unsigned short sequence;
- unsigned int timestamp;
- unsigned int ssrc;
- };
- #pragma pack()
- int main(void)
- {
- printf("%d\n",sizeof(info_t));
- printf("%d\n",sizeof(info_u));
- return 0;
- }
4、以下表达式result的值是()
- #define VAL1(a,b) a*b
- #define VAL2(a,b) a/b--
- #define VAL3(a,b) ++a%b
- int a = 1;
- int b = 2;
- int c = 3;
- int d = 3;
- int e = 5;
- int result = VAL2(a,b)/VAL1(e,b)+VAL3(c,d);
5、请写出以下程序的输出(5分)
- void swap_1(int a , int b)
- {
- int c;
- c = a;
- a = b;
- b = c;
- return ;
- }
- void swap_2(int &a , int &b)
- {
- int c;
- c = a;
- a = b;
- b = c;
- return ;
- }
- void swap_3(int *a , int *b)
- {
- int c;
- c = *a;
- *a = *b;
- *b = c;
- return ;
- }
- int main(void)
- {
- int a = 100;
- int b = 200;
- swap_1(a , b);
- printf("a = %d , b = %d\n",a , b);
- swap_2(a , b);
- printf("a = %d , b = %d\n",a , b);
- swap_3(&a , &b);
- printf("a = %d , b = %d\n",a , b);
- return 0;
- }
a = 100 , b = 200
a = 200 , b = 100
a = 100 , b = 200
6、下面的程序是否有问题,如有问题,请重构代码(5分)
- void test_type(bool b , const char *p , float f)
- {
- if(!b)
- {
- return ;
- }
- else if(!p)
- {
- return ;
- }
- else if(!f)
- {
- return ;
- }
- }
- void test_type(bool b , const char *p , float f)
- {
- if(!b)
- {
- return ;
- }
- else if(!p)
- {
- return ;
- }
- else if(f > -1e-10 && f < 1e-10)
- {
- return ;
- }
- }
- void test_mem()
- {
- char *p = new char[64];
- delete p;
- p = NULL;
- return ;
- }
回收用 new 分配的单个对象的内存空间的时候用 delete,回收用 new[] 分配的一组对象的内存空间的时候用 delete[]。
8、以下程序有什么问题,请指出。
- char* GetMem()
- {
- char p[] = "hello";
- return p;
- }
- void test_get_mem()
- {
- char *p = GetMem();
- printf(p);
- return ;
- }
- class Base
- {
- public:
- Base()
- {
- printf("I am Base()\n");
- }
- virtual ~Base()
- {
- printf("I am ~Base()\n");
- }
- public:
- virtual void SayHello()
- {
- printf("Hello Base\n");
- }
- void SayWorld()
- {
- printf("World Base\n");
- }
- };
- class Derived : public Base
- {
- public:
- Derived()
- {
- printf("I am Derived()\n");
- }
- virtual ~Derived()
- {
- printf("I am ~Derived()\n");
- }
- public:
- void SayHello();
- void SayWorld();
- };
- void Derived::SayHello()
- {
- printf("Hello Derived\n");
- }
- void Derived::SayWorld()
- {
- printf("World Derived\n");
- }
- int main(void)
- {
- Base *b1 = new Base;
- Base *b2 = new Derived;
- Derived *d = new Derived;
- b1->SayHello();
- b1->SayWorld();
- b2->SayHello();
- b2->SayWorld();
- d->SayHello();
- d->SayWorld();
- delete d;
- delete b2;
- delete b1;
- d= NULL;
- b2 = NULL;
- b1 = NULL;
- return 0;
- }
I am Base()
I am Base()
I am Derived()
I am Base()
I am Derived()
Hello Base
World Base
Hello Derived
World Base
Hello Derived
World Derived
I am ~Derived()
I am ~Base()
I am ~Derived()
I am ~Base()
I am ~Base()
11、阅读以下程序并给出执行结果
- class Bclass
- {
- public:
- Bclass(int i , int j)
- {
- x = i;
- y = j;
- }
- virtual int fun()
- {
- return 0;
- }
- protected:
- int x , y;
- };
- class lclass : public Bclass
- {
- public:
- lclass(int i , int j , int k) : Bclass(i , j)
- {
- z = k;
- }
- int fun()
- {
- return (x+y+z)/3;
- }
- private:
- int z;
- };
- int main(void)
- {
- lclass obj(2,4,10);
- Bclass p1 = obj;
- cout<<p1.fun()<<endl;
- Bclass &p2 = obj;
- cout<<p2.fun()<<endl;
- cout<<p2.Bclass::fun()<<endl;
- Bclass *p3 = &obj;
- cout<<p3->fun()<<endl;
- return 0;
- }
- const char* strchr(const char* str , char ch)
- {
- char *p = NULL;
- const char* s = str;
- for( ; *s != '\0' ; ++s)
- {
- if(*s == ch)
- {
- p = (char *)s;
- break;
- }
- }
- return p;
- }
void BubbleSort(int r[] , int n);
- void BubbleSort(int r[] , int n)
- {
- int i , j , temp;
- for(i = 0 ; i < n - 1 ; ++i)
- {
- for(j = 0 ; j < n-i-1 ; ++j)
- {
- if(r[j] > r[j + 1])
- {
- temp = r[j];
- r[j] = r[j + 1];
- r[j + 1] = temp;
- }
- }
- }
- }