自考新教材-p183

源程序:

#include<iostream>
using namespace std;

class Base //基类
{
private:
float x;
public:
static int staV;
Base()
{
staV++;
}
};

int Base::staV = 0;

class Derived :public Base //派生类
{
private:
float y;
public:
Derived()
{
staV++;
}
};

int main()
{
Base a;
cout << a.staV << endl; //输出1
Derived d;
cout << d.staV << endl; //输出3
system("pause");
return 0;
}

运行结果:

 

posted @ 2020-02-04 15:57  bobo哥  阅读(118)  评论(0)    收藏  举报