1. int?? i = 3, j = 4;
???i++; ++j;
???printf("%d ?%d",++i ,j++);
輸出結(jié)果?
答案
? 5?? 5

2 寫一個“標(biāo)準(zhǔn)”宏MIN,這個宏輸入2個參數(shù)返回較小的一個
答案
(謝謝? theanswerzju??指出之前形參未寫)
#define MIN(a,b)? ((a)>(b))?(b):(a)

3.簡述const的用途

答案


4.class和struct的區(qū)別
答案
唯一的區(qū)別就是:
?訪問權(quán)限不一樣??
? struct ?? 默認(rèn)情況下數(shù)據(jù)是公有的(public); ?
? class ? ? 默認(rèn)情況下數(shù)據(jù)是私有的(private);

(謝謝? theanswerzju??指出)
補充,還有 繼承權(quán)限不一樣
struct ?? 默認(rèn)情況下數(shù)據(jù)是公有繼承的(public); ?
? class ? ? 默認(rèn)情況下數(shù)據(jù)是私有繼承的(private);

再補充:
struct不支持泛型
還有如果你不使用虛函數(shù) 不需要構(gòu)造 析構(gòu)等等,使用struct要比class高效
編譯器會優(yōu)化



5.看代碼,寫輸出結(jié)果
#include <string.h>
#include "stdafx.h"
#include <iostream>
using namespace std;

struct?
{
?short a1;
?short a2;
?short a3;
}A;

struct
{
?long a1;
?short a2;
}B;
int _tmain(int argc, _TCHAR* argv[])
{
?char* ss1 = "0123456789";
?char ss2[] = "0123456789";
?char ss3[100] = "0123456789";
?int ss4[100];
?char q1[] = "abc";
?char q2[] = "a\n";
?char* q3 = "a\n";
?char? *str1 = (char*)malloc(100);
?void? *str2 = (void*)malloc(100);
?cout<<sizeof(ss1)<< " ";
?cout<<sizeof(ss2)<< " ";
?cout<<sizeof(ss3)<< " ";
?cout<<sizeof(ss4)<< " ";
?cout<<sizeof(q1)<< " ";
?cout<<sizeof(q2)<< " ";
?cout<<sizeof(q3)<< " ";
?cout<<sizeof(A)<< " ";
?cout<<sizeof(B)<< " ";
?cout<<sizeof(str1)<< " ";
?cout<<sizeof(str2)<< " ";
?getchar();
?return 0;
}

結(jié)果 4? 11? 100? 400 4 3 4 6 8 4 4

void foo(void)
{
?? unsigned int a = 6;
?? int b = -20;
? (a+b>6)?puts(">6"):puts("<=6");
}

結(jié)果?? >6

?做錯了,這題
b會轉(zhuǎn)換為 unsigned int ,那么最高位是1,所以b的數(shù)字就很大了,肯定大于6


6.#define和typede的區(qū)別,你更喜歡哪種?Why?
答案:
1) ? #define是預(yù)處理指令,在編譯預(yù)處理時進(jìn)行簡單的替換,不作正確性檢查,不關(guān)含義是否正確照樣帶入,只有在編譯已被展開的源程序時才會發(fā)現(xiàn)可能的錯誤并報錯。例如: ?
? ? #define ? PI ? 3.1415926 ?
? ? 程序中的:area=PI*r*r ? ? 會替換為3.1415926*r*r ?
? 如果你把#define語句中的數(shù)字9 ? 寫成字母g ? 預(yù)處理也照樣帶入。 ?
? ?
? 2)typedef是在編譯時處理的。它在自己的作用域內(nèi)給一個已經(jīng)存在的類型一個別名,但是You ? cannot ? use ? the ? typedef ? specifier ? inside ? a ? function ? definition。 ?
? ?
? 3)typedef ? ? int ? * ? int_ptr; ?
? 與 ?
? ? ? ? ? ? #define ? int_ptr ? int ? * ? ?
? ? 作用都是用int_ptr代表 ? int ? * ? ,但是二者不同,正如前面所說 ? ,#define在預(yù)處理 ? 時進(jìn)行簡單的替換,而typedef不是簡單替換 ? ,而是采用如同定義變量的方法那樣來聲明一種類型。也就是說; ?
? ?
? //refer ? to ? ? ? ? (xzgyb(老達(dá)摩)) ?
? #define ? int_ptr ? int ? * ?
? int_ptr ? a, ? b; ? ? //相當(dāng)于int ? * ? ? a, ? b; ? 只是簡單的宏替換 ?
? ?
? typedef ? int* ? int_ptr; ?
? int_ptr ? a, ? b; ? ? //a, ? b ? 都為指向int的指針,typedef為int* ? 引入了一個新的助記符 ?
? ?
? ?
? 這也說明了為什么下面觀點成立 ?
? //QunKangLi(維護(hù)成本與程序員的創(chuàng)造力的平方成正比) ?
? typedef ? int ? * ? pint ? ; ?
? #define ? PINT ? int ? * ?
? ?
? 那么: ?
? const ? pint ? p ? ;//p不可更改,但p指向的內(nèi)容可更改 ?
? const ? PINT ? p ? ;//p可更改,但是p指向的內(nèi)容不可更改。 ?
? ?
? pint是一種指針類型 ? ? const ? pint ? p ? 就是把指針給鎖住了 ? ? p不可更改 ?
? 而const ? PINT ? p ? ? ? 是const ? int ? * ? ? p ? ? 鎖的是指針p所指的對象。 ?
? ?
? 3)也許您已經(jīng)注意到#define ? 不是語句 ? ? 不要在行末加分號,否則 ? ? 會連分號一塊置換。???
???

7.非C++內(nèi)建型別A和B,在哪幾種情況下B能隱式轉(zhuǎn)化為A?

顯式轉(zhuǎn)換和隱式轉(zhuǎn)換的區(qū)別:
static_cast用于將派生類指針轉(zhuǎn)換成基類指針。發(fā)生于編譯時刻。
dynamic_cast用于將基類指針轉(zhuǎn)換成派生類指針。發(fā)生于運行時刻。

static_cast還可用于:
- 轉(zhuǎn)換不同內(nèi)建數(shù)據(jù)類型(char->int,flot->double等)
- 轉(zhuǎn)換整數(shù)類型到enum
- 轉(zhuǎn)換空指針為任何其它類型的空指針

reinterpret_cast可用于:
- 轉(zhuǎn)換任何相關(guān)或不相關(guān)的指針
- 轉(zhuǎn)換指針為整數(shù)類型 (反過來也行嗎?)
- 轉(zhuǎn)換空指針為任何其它類型的空指針


8.C++中的空類,默認(rèn)產(chǎn)出哪些類的成員函數(shù)?
答案:

class Empty

{

? public:

? Empty(); // 缺省構(gòu)造函數(shù)

? Empty( const Empty& ); // 拷貝構(gòu)造函數(shù)

? ~Empty(); // 析構(gòu)函數(shù)

? Empty& operator=( const Empty& ); // 賦值運算符

? Empty* operator&(); // 取址運算符

? const Empty* operator&() const; // 取址運算符 const

};


10.寫一個函數(shù),完成內(nèi)存之間的拷貝


5. 用變量a給出下面的定義
a) 一個整型數(shù)(An integer)
b) 一個指向整型數(shù)的指針(A pointer to an integer)
c) 一個指向指針的的指針,它指向的指針是指向一個整型數(shù)(A pointer to a pointer to an integer)
d) 一個有10個整型數(shù)的數(shù)組(An array of 10 integers)
e) 一個有10個指針的數(shù)組,該指針是指向一個整型數(shù)的(An array of 10 pointers to integers)
f) 一個指向有10個整型數(shù)數(shù)組的指針(A pointer to an array of 10 integers)
g) 一個指向函數(shù)的指針,該函數(shù)有一個整型參數(shù)并返回一個整型數(shù)(A pointer to a function that takes an integer as an argument and returns an integer)
h) 一個有10個指針的數(shù)組,該指針指向一個函數(shù),該函數(shù)有一個整型參數(shù)并返回一個整型數(shù)( An array of ten pointers to functions that take an integer argument and return an integer )

答案是:
a) int a; // An integer
b) int *a; // A pointer to an integer
c) int **a; // A pointer to a pointer to an integer
d) int a[10]; // An array of 10 integers
e) int *a[10]; // An array of 10 pointers to integers
f) int (*a)[10]; // A pointer to an array of 10 integers
g) int (*a)(int); // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int); // An array of 10 pointers to functions that take an integer argument and return an integer