1、先說說宏吧。好多公司的筆試題會在宏上出題,最常見的有以下三條語句。
1.1、2個數中取最小值,注意括號。
#define MIN( A, B ) ( (A) <= (B) ? (A) : (B))
1.2、這個題在選擇題中經常出現,考察的是計算數的展開式
#define SQR(X) ( (x) * (x) )
1.3、考察定義值的長度
#define SEC_PER_YEAR( 60* 60 * 24 * 365) UL
重點舉例說明第二條:
通常程序員粗心大意容易寫成這樣#define SQR(X) ( x * x ) 那麼下面用例子測試下這個結果。
SQR(6+4); 這樣的輸入得出的結果是6+4×6+4 = 6+24+4 = 34 結果是錯誤的。所以這個地方是要注意的。
2、2個數字比較大小,求較大的值。要求不用判斷語句(if , switch...)來實現。
int iMax = ( (a + b) + abs(a-b) ) / 2;
3、交換2個值。
3.1、書上的答案是這樣,我也經常這樣去寫。
int iNum = a;
a = b;
b = iNum;
3.2、資料上的最優答案是。
a = a ^ b;
b = a ^ b;
a = a ^ b;
4、const的用法。
4.1、修飾常量
4.2、可以修飾函數參數、返回值、函數體、防止程序被意外修改。曾強程序的健壯性。
5、const 與 define 的區別。
const 定義常量時有數據類型 define 沒有數據類型
const 可以進行調試 define 不可以
6、sizeof 與 strlen 的區別。
sizeof是運算符 strlen是庫函數
sizeof可以用類型做參數 strlen只能用char*做參數
sizeof可以用函數做參數
strlen計算字符的長度不是類型佔內存的大小
7、list刪除時的操作
for ( it = IntArray.begin(); it != IntArray.end() ; )
{
if ( ... )
{
it = IntArray.erase(it);
}
else
{
++it;
}
}
8、拷貝構造函數與賦值函數的區別
class Test;
8.1、 Test a;
a = b;
8.2、 Test a = b;
Test a(b);
兩者調用時間不同:
8.1、a構造完成,已有資源,然後等號進行賦值。
8.2、a此時還沒有分配資源,賦值時不需要分配資源。
9、什麼是“多態”?
在基類函數前面添加virtual關鍵字後,在派生類中重新該函數,運行時將會根據對象的實際類型調用相應的函數。
10、什麼是虛函數?
用virtual關鍵字申明的函數叫虛函數。
11、析構函數爲VIRTUAL是什麼目的?
這樣做是爲了當用一個基類的指針刪除一個派生類的對象時,派生類的析構函數會被調用。
12、 多線程的方法?
互斥對象、事件對象、關鍵代碼段。
互斥對象與事件對象都屬於內核對象,利用內核對象實現線程同步時,速度較慢。但可以在多進程中的各個線程間進行同步。
關鍵代碼段速度較快,但在等待進入關鍵代碼段時由於沒有辦法設置等待超時值。會造成線程的死鎖。
13、WINDOWS內存原理?
Windows爲每一個進程分配了4GB的地址空間。2GB的地址空間,用於存代碼、數據、堆棧。2GB用於共享系統使用。
14、實現strcmp函數.
char* strcmp(char* strDest, const char* strSrc)
{
assert( (strDest != NULL) && (strSrc != NULL) );
char* address = strDest;
while( (*strDest++ = *strSrc++) != '/0' )
NULL;
return address;
}
15、鏈表倒置
node* ReverseList(node* head)
{
node* p = NULL;
node* q = head;
while( head->next != 0)
{
p = head->next;
head->next = p->next;
p->next = q;
q = p;
}
return q;
}
16、實現String類
class String
{
private:
char* m_data;
public:
String(const char* pStr);
~String();
String(const String& other);
String& operator=(const String& other);
}
String::String(const char* pStr)
{
if ( pStr == NULL )
{
m_data = new char[1];
*m_data = '/0';
}
else
{
int iSize = strlen(pStr);
m_data = new char[iSize+1];
strcpy(m_data, str);
}
}
String::~String(void)
{
delete []m_data;
}
String::String(const String& other)
{
int iSize = strlen(other.m_data);
m_data = new char[iSize+1];
strcpy(m_data, other.m_data);
}
String& String::operator=(const String& other)
{
if ( this != &other )
{
int iSize = strlen(other.m_data);
char* pCh = new char[iSize+1];
delete [] m_data;
m_data = pCh;
}
return *this;
}