C++笔试题

来源:互联网 发布:3d max mac中文版下载 编辑:程序博客网 时间:2024/04/29 07:49

 请解释 aaa.h 中下面代码的功能

#if !defined(AFX_MYSUDU_H__9B952BEA_A051_4026_B4E5_0598A39D2DA4__INCLUDED_)
#define AFX_MYSUDU_H__9B952BEA_A051_4026_B4E5_0598A39D2DA4__INCLUDED_
... ...
#endif
预处理命令,可以保证多次包含此头文件时,只编译一次代码。 


CMemoryState 主要功能是什么
用于检测内存泄露。

 

 

请解释下面代码采用了何种 C++ 特性( C 语言不具备),作用是什么?
template<typename T>
T sum(T a, T b)
{
       return (a + b);
}
函数模板,用于将一类功能相同,参数类型和返回值不同的函数抽象为一个模板,方便模板函数调用。

 

 

 

请问下述代码中 : int operator+(… )起什么作用? this 是什么? ccc 的值最终为多少?
class Fruit
{
public:
       Fruit()
       {
              weight = 2;
       }
       Fruit(int w)
       {
              weight = w;
       }
       int operator+(Fruit f)
       {
              return this->weight * f.weight;
       }
private:
       int weight;
};
       Fruit aaa;
       Fruit bbb(4);
       int ccc = aaa + bbb;
int operator+(… )表示重载类的“+”号运算符,this表示对象本身的指针,本例中它指向类的对象aaa;ccc最终的结果为8(8 = 2 * 4)。

 

把下述代码加上异常处理。
int MyWriteFile(CString strFileName, CString strText)
{
       int nRet = 0;
      
       CFile myFile;
       myFile.Open(strFileName, CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate, NULL);
      
       int nLen = strText.GetLength();
       myFile.Write((char*)(LPCSTR)strText, nLen);
      
       myFile.Close();

       return nRet;
}
int MyWriteFile(CString strFileName, CString strText)
{
       int nRet = 0;
      
       CFile myFile;
       __try
      {
        nRet = myFile.Open(strFileName,\
               CFile::modeWrite|CFile::shareExclusive|CFile::modeCreate, NULL);
        if(!nRet)
        {
            /// 输出异常信息
            __leave;/// 结束try块,跳到__finaly代码块
        }
        int nLen = strText.GetLength();
        nRet = myFile.Write((char*)(LPCSTR)strText, nLen);
        if(!nRet)
        {
            /// 输出异常信息
            __leave;/// 结束try块,跳到__finaly代码块
        }
      }
      __finaly
     {
         myFile.Close();
     }
     return nRet;
}

 

 

原创粉丝点击