http://blog.csdn.net/xscarlet/article/details/1702309
1. <!--[endif]-->已知下面這些類型的聲明
template <class elemType> class Array;
enum Status {...};
typedef string* Pstring;
下面哪一個對象的定義是錯誤的:
- Array< int* &> pri( 1024 );
- Array< Array<int> > aai( 1024 );
- Array< Status > as( 1024 );
- Array< Pstring > aps( 1024 );
解釋:首先要確定Array這個模板類裏有一個構造函數,這個構造函數還必須接受一個整形或浮點形或可以轉換到整形的參數。然後可以確定a選項是不對的。分析各選項,
B中要求這個類還要有一個無參的構造函數!因爲Array<int>這樣的類型在Array< >中初始化的時候,是被視爲一個類類型的,而不是像int double 這樣的內置類型,這樣在Array<>的構造函數中就必須生成一系列Array<int>這樣的類型,如果沒有無參的構造函數,那麼是錯誤的;
C說明Array中存放的類型是Status;
D說明Array中存放的類型是string的指針;
A int型的指針的引用時不行的,但是如果改成int**是可以的
<!--[if !supportLists]-->
2. <!--[endif]-->下面哪些定義不合法:
<!--[if !supportLists]-->a. <!--[endif]-->int car = 1024, auto = 2048;
<!--[if !supportLists]-->b. <!--[endif]-->int ival = ival;
<!--[if !supportLists]-->c. <!--[endif]-->int ival(int());
<!--[if !supportLists]-->d. <!--[endif]-->double salary = wage = 999.99;
解釋:兩項有問題:D wage沒聲明;A auto是關鍵字
B是可以但是會有warning;C可以他會初始化ival爲整形的默認值
<!--[if !supportLists]-->3. <!--[endif]-->下面那些定義合法:
<!--[if !supportLists]-->a. <!--[endif]-->long* const cpl;
<!--[if !supportLists]-->b. <!--[endif]-->const long lc;
<!--[if !supportLists]-->c. <!--[endif]-->const long* const cplc;
<!--[if !supportLists]-->d. <!--[endif]-->const long* plc;
解釋:選d,abc因爲作爲一個常量,必須在聲明的時候初始化。而d是一個指針,它指向一個常長整形,可以不初始化,a是指向長整形的常量指針;b是常長整形;c是常長整形的常量指針。
<!--[if !supportLists]-->4. <!--[endif]-->已知有如下定義:
long l = 10;
const long cl = 100;
那麼下面表達式不正確的是:
<!--[if !supportLists]-->a. <!--[endif]-->const long* pl = &cl;
<!--[if !supportLists]-->b. <!--[endif]-->long* const pl = &cl;
<!--[if !supportLists]-->c. <!--[endif]-->const long* const pl = &cl;
<!--[if !supportLists]-->d. <!--[endif]-->const long* pl = &l;
B
A常長整形的指針pl指向常長整形cl
B長整形的常量指針pl指向了常長整形,pl能指向的東西是長整形,不是常長整形
C常長整形的常量指針指向常長整形
D常長整形的指針pl指向一個長整形,可以,可以從long形變化到const long形
D和B類似,但是B錯的原因是不能從const long轉化成long
<!--[if !supportLists]-->5. <!--[endif]-->已知: long lVal = 5; 下面那些定義無效: c
<!--[if !supportLists]-->a. <!--[endif]-->long& lRef = lVal;
<!--[if !supportLists]-->b. <!--[endif]-->const long& lRef = 5;
<!--[if !supportLists]-->c. <!--[endif]-->long& lRef = 5;
<!--[if !supportLists]-->d. <!--[endif]-->const long& lRef = lVal;
解釋:C
C錯是因爲5這個數字算是一個常量,但是long& 類型的lRef不能引用
原因同上題不能從一個const long 轉換爲一個long
<!--[if !supportLists]-->6. <!--[endif]-->下面的函數定義不構成重載的是:
a. long hoo( long m, long n = 0 );
long hoo( const long m );
b. long hoo( long m );
long hoo( const long m );
c. long hoo( const long m );
double hoo( const long m);
d. long hoo( long m = 0, long n = 0 );
long hoo( long m = 0 );
解釋:C
返回值類型不同是不能構成重載的!
這裏特別要注意d!如果調用的是hoo()或者hoo(100)會造成二義性!因爲有了默認參數編譯器並不知道哪個更合適!
<!--[if !supportLists]-->7. <!--[endif]-->已知模板函數的定義
template <class T1, class T2, class T3>
T1 sum( T2, T3 );
變量定義:
double dobj1, dobj2;
float fobj1, fobj2;
char cobj1, cobj2;
下面哪一個調用是錯誤的:
<!--[if !supportLists]-->a. <!--[endif]-->sum( dobj1, dobj2 ) ;
<!--[if !supportLists]-->b. <!--[endif]-->sum<double, double, double>( fobj1, fobj2 );
<!--[if !supportLists]-->c. <!--[endif]-->sum<int>( cobj1, cobj2 );
<!--[if !supportLists]-->d. <!--[endif]-->sum<double, int, char>( cobj1, cobj2 );
解釋:a
原因是編譯器無法推演出T1的類型,a選項裏沒有寫明T1的類型,而sum函數需要T1的類型去特化出一個函數能夠處理dobj1和dobj2這兩個參數的函數。其他的都可以。
<!--[if !supportLists]-->8. <!--[endif]-->下面的throw表達式中, 哪一個是錯誤的:
<!--[if !supportLists]-->a. <!--[endif]-->class exception{};
throw exception;
<!--[if !supportLists]-->b. <!--[endif]-->long excpobj;
throw excpobj;
<!--[if !supportLists]-->c. <!--[endif]-->enum MathError{ overflow, underflow, zerodivide };
throw overflow;
<!--[if !supportLists]-->d. <!--[endif]-->long excpobj = 0;
long* pl = &excpobj;
throw *pl;
解釋:
可以拋出任何類型,但是必須是某類型的對象,A中拋出的是一個類型,不對!
<!--[if !supportLists]-->9. <!--[endif]-->現有類定義如下:
class X
{
public:
X();
~X();
private:
long m_lID;
};
下面的表達式中, 沒有調用到類X的構造函數的是:
<!--[if !supportLists]-->a. <!--[endif]-->X x;
<!--[if !supportLists]-->b. <!--[endif]-->foo( X()); ( foo原型爲 void foo( X& ); )
<!--[if !supportLists]-->c. <!--[endif]-->X y = x; (x是a中的定義)
<!--[if !supportLists]-->d. <!--[endif]-->X* pax = new X[10];
解釋:c
C用的是複製構造函數。其函數原型載這個題中並沒寫出來,是默認的
如X(const X& object){…}
<!--[if !supportLists]-->10. <!--[endif]-->類X的定義同第8題, 下面的表達式中, 沒有調用到類X的析構函數的是:
<!--[if !supportLists]-->a. <!--[endif]-->{ X x; }
<!--[if !supportLists]-->b. <!--[endif]-->X* px = new X; delete px;
<!--[if !supportLists]-->c. <!--[endif]-->X* pax = new X[10]; delete pax;
<!--[if !supportLists]-->d. <!--[endif]-->{ X* px = new X; }
解釋:d在一個作用域裏用這種new方式分配內存的對象,必須手動delete纔會釋放內存,否則不會自動調用析構函數,會造成內存的泄漏
C是錯誤的應該是delete[] pax;因爲分配了很多對象,如果像c中那麼寫只能調用一次析構函數,也會造成內存泄漏
<!--[if !supportLists]-->11. <!--[endif]-->下面的表達式中, 編譯將能通過的是:
<!--[if !supportLists]-->a. <!--[endif]-->#include <vector>
vector<long> vectlCount;
<!--[if !supportLists]-->b. <!--[endif]-->#include <vector.h>
vector<long> vectlCount;
<!--[if !supportLists]-->c. <!--[endif]-->#include <vector>
std::vector<long> vectlCount;
<!--[if !supportLists]-->d. <!--[endif]-->#include <vector.h>
std::vector<long> vectlCount;
選c
在C++中,vector這樣的頭文件須寫成#include <vector>這樣,而不能加.h;而且它在std命名空間中,要使用必須加std::前綴,或者using namespace std;對於標準庫裏的所有組件都是這樣的道理。
<!--[if !supportLists]-->12. <!--[endif]-->下面哪句話是正確的:
<!--[if !supportLists]-->a. <!--[endif]-->一個class必須供應至少一個constructor.
<!--[if !supportLists]-->b. <!--[endif]-->所謂的default constructor 是指其參數列表中沒有參數.//不需要用戶輸入,參數列表裏可以定義變量並初始化等。
<!--[if !supportLists]-->c. <!--[endif]-->一個class對象被構造時, 不一定就會調用其constructor.
<!--[if !supportLists]-->d. <!--[endif]-->如果class定義中不顯示供應一個default constructor, 則編譯器會自動生成一個, 用以爲每個data member設定默認值.
解釋:
A至少有一個,沒有寫出來,編譯器會自動添加一個
b默認構造函數不是這個意思~
C可以調用複製構造函數copy constructor
D默認構造函數在有別的構造函數的時候不會在由編譯器生成
<!--[if !supportLists]-->13. <!--[endif]-->已知下面的基類和子類,
class CBase
{
public:
char foo( int );
protected:
int m_bar;
double m_foo_bar;
};
class CDerived : public CBase
{
public:
char foo( string );
bool bar( CBase* pb );
void foobar();0
protected:
string m_bar;
};
下面的代碼, 正確的是:
<!--[if !supportLists]-->a. <!--[endif]-->CDerived d; d.foo( 1024 );
<!--[if !supportLists]-->b. <!--[endif]-->void CDerived::foobar() { m_bar = 1024; }//warning, truncation from 'const int' to 'char'。
<!--[if !supportLists]-->c. <!--[endif]-->bool CDerived::bar( CBase* pb )
{ return m_foo_bar == pb->m_foo_bar; }//不能調用父類的protected對象m_foo_bar
<!--[if !supportLists]-->d. <!--[endif]-->void CDerived::foobar(){ CBase::m_foo_bar = 2048; }
解釋:D
A沒有這樣的函數!子類的同名函數會覆蓋父類的同名實現
B會出一個warning:warning,truncation from 'const int' to 'char'。但其實用的時候就會出錯,因爲編譯器是不知道m_bar是哪個的!
Cpb不能直接調用m_foo_bar 是protected
D可以這麼用,不過直接寫就可以了。
<!--[if !supportLists]-->14. <!--[endif]-->子類Derived繼承基類Base, 則下面的子類對於基類的 “虛函數override中”哪個是正確的(下面基類的函數都是virtual的):
<!--[if !supportLists]-->a. <!--[endif]-->Base* Base::copy( Base* );
Base* Derived::copy( Derived* );
<!--[if !supportLists]-->b. <!--[endif]-->Base* Base::copy( Base* );
Derived* Derived::copy( Base* );
<!--[if !supportLists]-->c. <!--[endif]-->ostream& Base::print( int, ostream& = cout );
ostream& Derived::print( int, ostream& );
<!--[if !supportLists]-->d. <!--[endif]-->void Base::eval() const;
void Derived::eval();
解釋:
d如果是同一個類型兩個函數重名一個爲const一個不是,那麼是重載,如果是兩個類型裏,應該是不算重寫的。
<!--[if !supportLists]-->15. <!--[endif]-->有如下聲明:
class CAD { }; class Vehicle { };
class CADVehicle : public CAD, Vehicle { };
請問類CADVehicle 是以什麼方式繼承類Vehicle的:
<!--[if !supportLists]-->a. <!--[endif]-->public 方式//
<!--[if !supportLists]-->b. <!--[endif]-->protected 方式
<!--[if !supportLists]-->c. <!--[endif]-->private 方式
<!--[if !supportLists]-->d. <!--[endif]-->因爲 Vehicle前面沒有指定修飾符, 所以上述寫法是錯誤的.//
解釋:路過的朋友,如果知道這個題的正確答案,麻煩給我留個言。
B這個題是我試出來的,不過是vc6下調試的,感覺是c
<!--[if !supportLists]-->16. <!--[endif]-->已知如下的類層次結構. 其中每一個類都定義了一個default constructor和一個virtual destructor:
class X { };
class A { };
class B : public A { };
class C : public B { };
class D : public X, public C { };
下面哪一個dynamic_cast 會失敗:
<!--[if !supportLists]-->a. <!--[endif]-->D* pd = new D;
A* pa = dynamic_cast<A*>( pd );
<!--[if !supportLists]-->b. <!--[endif]-->A* pa = new C;
C* pc = dynamic_cast<C*>( pa );
<!--[if !supportLists]-->c. <!--[endif]-->B* pb = new B;
D* pd = dynamic_cast<D*>( pb );
<!--[if !supportLists]-->d. <!--[endif]-->A* pa = new D;
X* px = dynamic_cast<X*>( pa );
解釋:C
因爲pb自西那個的是B類型,B類型不是D類型,所以不能轉換