#include
using namespace std;
class Base
{
friend void fun();
public:
Base(int data = 0)
:b(data)
{
cout << "Base()" << endl;
}
~Base()
{
cout << "~Base()" << endl;
}
static void show()
{
cout << "static show()" << endl;
}
void add()
{
a++;
}
static int a;
private:
int b;
};
int Base::a = 0;
class Derive :public Base
{
public:
Derive(int data = 0)
:d(data)
{
cout << "Derive()" << endl;
}
~Derive()
{
cout << "~Derive()" << endl;
}
void Add()
{
a++;
}
private:
int d;
};
int main()
{
Derive d;
d.show();
d.a = 2;
d.add();
d.Add();
cout << d.a << endl;
return 0;
}
Base()
Derive()
static show()
4
~Derive()
~Base()
前兩個輸出應該沒問題吧(基類的構造函數函數優先被調用,和析構正好相反)
子類不能從父類繼承的有:
1. 構造函數
2. 拷貝構造函數
3. 析構函數
子類和父類是共享一個靜態成員變量的(不是繼承,因為靜態成員是沒有this指針的,是繼承不過來的)
所以打印出4應該能理解了吧