重拾C++经典笔试30题(21-30)

重拾C++经典笔试30题(21-30)

1.       为什么Delete会出错?

class CBase
{
public:
         CBase() { cout <<"CBase" << endl; }
         virtual ~CBase() { cout <<"~CBase" << endl;}
};
 
classCDerived : public CBase
{
public:
         CDerived() { cout <<"CDerived" << endl; }
         ~CDerived() { cout <<"~CDerived" << endl; }
};
 
int main()
{
         CBase base;
         CBase* pBase = new CBase;
         pBase = &base;
         delete pBase; //运行时报错!
}


【分析如下】:

1.pBase指向了栈区内存,那是系统管理的空间,不能用delete释放的。

2.程序在堆区new的空间最后没有被释放,造成了内存泄露。

3.最好不要随便把申请到堆区空间的指针指向别处,至少也要有一个指针指向申请的空间。以便最后释放的是自己申请的那块内存。

【修正后做法】:

int main()
{
    CBase base;
    CBase* pBase = new CBase;
    CBase* pBase2 = pBase;   //至少也要有一个指针指向申请的空间
    pBase = &base;
    delete pBase2;  //以便最后释放的是自己申请的那块内存。
} //运行时不再报错!

【再深入点】:程序有两个问题:

1.内存泄露,new出来的没delete;

2.两次析构;base不是new出来,在生命周期结束(也就是你函数结束的时候)会自动释放,你主动调用delete将其析构,系统在函数结束时又会对其析构,所以才会报错。而且报错的地方应该是程序退出时

 

2.       类中静态常成员变量的定义?

#include<iostream>
usingnamespace std;
//可以在类的声明中对常量的类变量进行赋值
//VS2008可以,vc6.0不可以。和编译器有关。
class myclass
{
public:
         static const int i=20; //只有类的静态常量数据成员才可以在类中初始化。
};
const int myclass::i = 10;
int main()
{
         cout<<myclass::i<<endl;
         return 0;
}

3.      重载和多态的关系?

不同点

重载overload

覆盖override

1.是否支持多态?

不支持

支持

2.存在形式?

可以在类中或在C++语言中都可以体现

存在于类中父类、子类之间。

3.参数列表、返回值

参数列表或返回值不同,或二者都不同。

参数列表、返回指标必须相同。

 

 

4.      输出格式:printf用!

%a(%A)    

浮点数、十六进制数字和p-(P-)记数法(C99)

%c           

字符

%d            

有符号十进制整数

%f             

浮点数(包括floatdoulbe)

%e(%E)    

浮点数指数输出[e-(E-)记数法]

%g(%G)    

浮点数不显无意义的零"0"

%i             

有符号十进制整数(%d相同)

%u            

无符号十进制整数

%o            

八进制整数    e.g.     0123

%x(%X)     

十六进制整数0f(0F)   e.g.   0x1234

%p            

指针

%s            

字符串

     

5.      一个参数或指针可以既是const又是volatile)解读?

Volatile 以防止编译器将其优化成从寄存器中读取。一个定义为volatile的变量时说这个变量可能会被意想不到地改变,这样编译器就不会去假设这个变量的值。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。

一个参数可以既是const又是volatile,const是不让程序修改,volatile是意想不到的改变,不是程序修改。一个指针也可以是volatile,中断服务子程序修改一个指向buffer的指针。

6.      Little Endian低字节序(由低字节—>高字节存储);计算机默认的为低字节序。

High Endian高字节序(由高字节—>低字节存储);

typedef struct bitstruct
{
         int b1:5;
         int b2:2;
         int b3:2;
}bitstruct;
 
int main()
{
         bitstruct b;
         memcpy(&b,"EMCEXAMINATION",sizeof(b));
 
         cout << sizeof(b) << endl;
 
         printf("%d,%d\n",b.b1,b.b2);//5,-2
         return 0;
}

[解读]:1.sizeof(b)=4;即4个字节的大小。

2.memcpy将”EMC …”存入b中。

3.实质b中只有5+2+2,9位。即对应字符也只有”EM”.E的ASCII码为0X45,M的ASCII码为0X4D。

满足高字节—>低字节存储0X4D0X45,对应二进制位:01001101 0100 0101

4.对应的b1,满足(低字节存放于低位)取得后低地址的5位0 0101,首位为0代表正数,大小为5。

对应的b2,取10,首位为1代表负数,取补码后得到b2=-2。

27.输出结果?

int main()
{
         int a[5][2] = {0,1,2,3,4,5,6,7,8,9};
        
         int *p = a[0];
         int (*p2)[2] = &a[1];
         ++p;
         ++p2;
        
         printf("%d\n",*p); //1 p 是整型指针,初始指向元素0,加1指向1
         printf("%d\n",**p2); //4 p2是含2个元素的数组指针,初始指向元素2,该指针加1是向后移动2个数据,所以指向4
         printf("%d \n",p2[1][2]); //如何解读? 见下解读。 
         return 0;
}

 

0

1

0

0 (p)

1(++p后)

1

2  (P2指向)

3

2

4 (++p2后)

5

3

6  (p2+1后)

7

4

8  (p2[1][2])

9

 

解读:p2是一个指针,是一个指向包含两个元素数组的指针变量。和普通的指针不同的地方时它指向的长度为2。(*p2)[2]和a是等价的。

对于p2[1][2],此时p2指向4,前一个下标1就是p2指针再加1指向6,后一个下标加2移动2个元素,指向了8。


28.拷贝构造输出结果?

class A
{
    static int objectCount;
public:
    A()
    {
        objectCount++;
        cout << "A():" <<objectCount << endl;
    }
   
    A(const A& r)
    {
        objectCount++;
        cout << "A(const A&r):" << objectCount << endl;
    }
        
    ~A()
    {
        objectCount--;
        cout << "~A():"<< objectCount << endl;
    }
};
 
intA::objectCount = 0;
 
A f(A x)        
{
    cout << endl <<  "Begin: f(A x)" << endl;
    return x;   //【临时对象】调用默认拷贝构造函数A(const A& r):3
}                //~A():2
 
int main()
{
    A h;        //A():1
    A h2 = f(h); //调用默认拷贝构造函数A(constA& r):2
    cout << endl <<"End(main): f(A x)" << endl << endl;
        
    return 0;   
}               //~A():1  析构h2   
//~A():0  构函h

29.四类强制类型转换

类型

示意

举例

static_cast

1.类型转换,编译器隐式执行的任何类型都可由static_cast显示完成;2.使用类型信息执行转换,在转换执行必要的检测(越界检测、类型检查),操作相对安全;

int—>double

int ival;

double result = static_cast<double> ival

const_cast

转换掉对象的const属性

下举例

dynamic_cast

运行时类型检查,用于继承体制下的由上到下的转换downcast。

下举例

reinterpret_cast

1.仅仅重新编译了给定对象的比特模型,而没有进行二进制转换;2.为操作数提供低层次的重新解释。

下举例

举例:

//const_cast 实例.
class B
{
public:
         int m_num;
};
 
int main()
{
         B b0;
         b0.m_num = 100;
         const B b1 = b0;
         cout << b0.m_num<< " " << b1.m_num << endl;
// 以下修改const对象的值是错误的。
//       b1.m_num = 355;
//       cout << b1.m_num <<endl;  // error C2166: l-value specifiesconst object
 
//以下使用const_cast是正解.
         const_cast<B&>(b1).m_num =355;
         cout<< b1.m_num << endl;
         return 0;
}
 
//reinterpret_cast实例
int main()
{
         int n = 9;
         double dval =reinterpret_cast<double& >(n);
         double dval_new =static_cast<double>(n); //成功.
         //[仅仅复制了n的比特位到d,没有进行必要的分析]
         cout << dval << endl;//2.64214e-308
         cout << dval_new << endl;//9
         return 0;
}
//dynamic_cast实例
class B
{
public:
         B() { cout << "B()"<< endl; }
         ~B() { cout << "~B()"<< endl; }
};
class C :public B
{
public:
         C() { cout << "C()"<< endl; }
         ~C() { cout << "~C()"<< endl; }
};
class D :public C
{
public:
         D(){ cout << "D()"<< endl; }
         ~D(){ cout << "~D()"<< endl; }
};
 
void f(D* pd)
{
         C* pc =dynamic_cast<C*>(pd);   // ok: C isa direct base class
         // pc points to C subobject of pd
 
         B* pb =dynamic_cast<B*>(pd);   // ok: B isan indirect base class
         // pb points to B subobject of pd    
}
 
int main()
{
         D objd;
         f(&objd);
         return 0;
}

30. 持续更新中......

30. 如何在C/C++中显示当前程序所在的文件名及行号。

——这个当时没答上来,见过没记住。今天查了下MSDN如下:

__FILE__, //用于显示文件名的宏 %s, 格式如【F:\NeuSoftDemo\NeuSoftDemo.cpp】;__LINE__, //用于显示行号的宏 %d,格式如【12】;

扩展》》__DATE__, //用于显示当前日期,格式如【Sep 18 2012】 %s; __TIME__, //用于显示当前时间,格式如【09:45:01】 %s;

__TIMESTAMP__,//用于显示当前日期和时间,格式如【Tue Sep 18 09:48:07 2012】 %s。

31. 程序纠错题:

  1. int main(int argc,char* argv[])
  2. {
  3. char str[5][] = {"First","Second","Thrid","Four","Five"};
  4. char* p[] = {str[4],str[3],str[2],str[1],str[0]};
  5. for(int i = 0; i < 5; i++)
  6. {
  7. printf("%c\n",*(p+i));
  8. }
  9. return 0;
  10. }
int main(int argc, char* argv[])
{
	char str[5][] = {"First","Second","Thrid","Four","Five"};
	char* p[] = {str[4],str[3],str[2],str[1],str[0]};
	
	for(int i = 0; i < 5; i++)
	{
		printf("%c\n",*(p+i));
	}

	return 0;
}
个人感觉如下:

错误1: char str[5][] 定义出错,需要指定第一维的个数,改为char str[][5]吗?但后面的字符串如"Second"6个字符,还有'\0'。改为:char* str[5]比较稳妥;

错误2: printf("%c\n",*(p+i)); 显然*(p+i) 等价于p[i]存储的是字符串,所以%c应该改为%s。(%c打印的是单个字符,%s打印的是字符串)。

修正后如下:

[cpp] 
  1. int main(int argc,char* argv[])
  2. {
  3. char *str[5] = {"First","Second","Thrid","Four","Five"};
  4. char* p[] = {str[4],str[3],str[2],str[1],str[0]};
  5. for(int i = 0; i < 5; i++)
  6. {
  7. printf("%s\n",*(p+i));
  8. }
  9. return 0;
  10. }

33. 递归与非递归实现二分查找。

//非递归实现-

  1. void binarySearchUncycle(int nArr[],int nSize,int nSearchVal)
  2. {
  3. int nLow = 0;
  4. int nHigh = nSize-1;
  5. int nMid = 0;
  6. bool bFound = false;
  7. while(nLow <= nHigh)
  8. {
  9. nMid = (nLow + nHigh)/2;
  10. cout << "nMid =" << nMid << endl;
  11. if(nArr[nMid] == nSearchVal)
  12. {
  13. bFound = true;
  14. break;
  15. }
  16. else if(nArr[nMid] > nSearchVal)
  17. {
  18. nHigh = nMid-1;
  19. }
  20. else
  21. {
  22. nLow = nMid+1;
  23. }
  24. }//end while
  25. if(bFound)
  26. {
  27. cout << "The Elem " << nSearchVal << " is Exist in the array!" << endl;
  28. }
  29. else
  30. {
  31. cout << "The Elem " << nSearchVal << " is Not Exist in the array!" << endl;
  32. }
  33. }
 
[cpp]
  1. //递归实现二分查找
  2. void binarySearchCycle(int nArr[],int low, int high, int nSearchVal)
  3. {
  4. int nLow = low;
  5. int nHigh = high;
  6. int nMid = 0;
  7. bool bFound = false;
  8. nMid = (nLow + nHigh)/2;
  9. if(nArr[nMid] == nSearchVal)
  10. {
  11. bFound = true;
  12. cout << "The Elem " << nSearchVal << " is Exist in the array!" << endl;
  13. return;
  14. }
  15. else if(nArr[nMid] > nSearchVal)
  16. {
  17. nHigh = nMid-1;
  18. binarySearchCycle(nArr,nLow,nHigh,nSearchVal);
  19. }
  20. else
  21. {
  22. nLow = nMid+1;
  23. binarySearchCycle(nArr,nLow,nHigh,nSearchVal);
  24. }
  25. }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铭毅天下

和你一起,死磕Elastic!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值