(8)下列程序的輸出結(jié)果為
Object id=0
Object id=1
請將程序補充完整。
#include <iostream>
using namespace std;
class Point
{
public:
Point(int xx=0, int yy=0) {X=xx; Y=yy; countP++; }
~Point( ) { countP--; }
int GetX( ) {return X;}
int GetY( ) {return Y;}
static void GetC( ) {cout<<" Object id="<<countP><<endl;}
private:
int X,Y;
static int countP;
};
【9】 //靜態(tài)數(shù)據(jù)成員的初始化
int main( )
{
Point::GetC( );
Point A(4,5);
A.GetC( );
return 0;
}
(9)非成員函數(shù)應(yīng)聲明為類的 【10】 函數(shù)才能訪問這個類的private成員。
(10)派生類中的成員不能直接訪問基類中的 【11】 成員。
(11)下列程序的輸出結(jié)果為2,請將程序補充完整。
#include <iostream>
using namespace std;
class Base
{
public:
【12】 void fun( ){ cout<<1; }
};
class Derived:public Base
{
public:
void fun( ) { cout<<2; }
};
int main( )
{
Base *p= new Derived;
p->fun( );
delete p;
return 0;
}
(12)請將下列類定義補充完整。
class Base { public: void fun( ){ cout<<"Base::fun"<<endl; } };
class Derived : public Base {
public:
void fun( ) {
【13】 //顯式調(diào)用基類的fun函數(shù)
cout<<"Derived::fun"<<endl;
}};
(13)多數(shù)運算符既能作為類的成員函數(shù)重載,也能作為類的非成員函數(shù)重載,但[ ]運算符只能作為類的 【14】 函數(shù)重載。
(14)下面是一個棧類的模板,其中push函數(shù)將元素i壓入棧頂,pop函數(shù)彈出棧頂元素。棧初始為空,top值為0,棧頂元素在stack[top-1]中,在下面橫線處填上適當語句,完成棧類模板的定義。
template <class T >
class Tstack
{
enum {size=1000};
T stack[size];
int top;
public:
Tstack( ):top(0){ }
void push(const T &i){
if(top<size)
stack[top++]=i;
}
T pop( ){
if(top == 0) exit(1); //棧空時終止運行
return 【15】 ;
}
};
北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內(nèi)蒙古 |