一道考察類的純虛擬函式定義,靜態變數等C++題目
阿新 • • 發佈:2018-11-09
程式原題如下:
#pragma once
class mml
{
private:
static unsigned int x;
public:
mml(){ x++; }
mml(static unsigned int&){ x++; }
~mml(){ x--; }
virtual mon(){} = 0; //缺少型別, 純虛擬函式沒有{},如下即可
//virtual int mon() = 0;
static unsigned int mmc(){return x;}
};
class nnl:public mml
{
private:
public:
nnl(){ x++; } //x 是靜態變數 派生類和父類共享空間,但是訪問有許可權控制,private不能訪問
nnl(static unsigned int&){ x++; }
~nnl(){ x--; }
virtual mon(){} //缺少型別,缺少返回型別,如下即可
//virtual int mon(){ return y; }
static unsigned int nnc(){ return y; }
};
//unsigned int mml ::x =0; //static變數必須初始化
//unsigned int nnl ::y = 0; //同上
#inlcude "jxyTest.h"
int mian()
{
mml * pp = new nnl;
delete pp;
}