2011年7月12日 星期二

C++ 繼承測試


#include
#include
#pragma hdrstop

using namespace std;
//---------------------------------------------------------------------------

class A
{
private:
// virtual void __fastcall virtual_private_pure()=0;
// void __fastcall private_pure()=0;
virtual void __fastcall virtual_private_A(){cout << "virtual_private_A" << endl;};
void __fastcall private_A(){cout << "private_A" << endl;};
protected:
virtual void __fastcall virtual_protected_pure()=0;
// void __fastcall protected_pure()=0;
virtual void __fastcall virtual_protected_A(){cout << "virtual_protected_A" << endl;};
void __fastcall protected_A(){cout << "protected_A" << endl;};
public:
virtual void __fastcall virtual_public_pure()=0;
// void __fastcall public_pure()=0;
virtual void __fastcall virtual_public_A(){cout << "virtual_public_A" << endl;};
void __fastcall public_A(){cout << "public_A" << endl;};


void A_get_private_A(){ private_A(); }
void A_get_virtual_private_A(){ virtual_private_A(); }
virtual void get_private_A(){ private_A(); }
virtual void get_virtual_private_A(){ virtual_private_A(); }
void get_private_A2(){ private_A(); }
void get_virtual_private_A2(){ virtual_private_A(); }
virtual void get_private_A3(){ private_A(); }
virtual void get_virtual_private_A3(){ virtual_private_A(); }
void void get_private_A4(){ private_A(); }
void void get_virtual_private_A4(){ virtual_private_A(); }
};

class B : public A
{
private:
// virtual void __fastcall virtual_private_pure()=0; //no supported
// void __fastcall private_pure()=0; //no supported

//以下2個要宣告,否則後面func呼叫會失敗,有宣告仍可以達到繼承
virtual void __fastcall virtual_private_A(){cout << "virtual_private_B" << endl;};
void __fastcall private_A(){cout << "private_B" << endl;};
protected:
virtual void __fastcall virtual_protected_pure(){cout << "virtual_protected_pure B" << endl;};
// void __fastcall protected_pure()=0; //no supported
virtual void __fastcall virtual_protected_A(){cout << "virtual_protected_B" << endl;};
void __fastcall protected_A(){cout << "protected_A" << endl;};
public:
virtual void __fastcall virtual_public_pure(){cout << "virtual_public_pure B" << endl;};
// void __fastcall public_pure()=0; //no supported
virtual void __fastcall virtual_public_A(){cout << "virtual_public_B" << endl;};
void __fastcall public_A(){cout << "public_B" << endl;};

void B_get_private_A(){ private_A(); }
void B_get_virtual_private_A(){ virtual_private_A(); }
virtual void get_private_A(){ private_A(); }
virtual void get_virtual_private_A(){ virtual_private_A(); }
void get_private_A2(){ private_A(); }
void get_virtual_private_A2(){ virtual_private_A(); }
void get_private_A3(){ private_A(); }
void get_virtual_private_A3(){ virtual_private_A(); }
virtual get_private_A4(){ private_A(); }
virtual get_virtual_private_A4(){ virtual_private_A(); }

};



#pragma argsused
int main(int argc, char* argv[])
{
B b;
A *pA = dynamic_cast(&b);


//private calling exported B-func , exported A-func , privated-func
b.B_get_private_A(); // B non-virt x non-virt
b.B_get_virtual_private_A(); // B non-virt x virt
b.get_private_A(); // B virt virt non-virt
b.get_virtual_private_A(); // B virt virt virt
b.get_private_A2(); // B non-virt non-virt non-virt
b.get_virtual_private_A2(); // B non-virt non-virt virt
b.get_private_A3(); // B non-virt virt non-virt
b.get_virtual_private_A3(); // B non-virt virt virt
b.get_private_A4(); // B virt non-virt non-virt
b.get_virtual_private_A4(); // B virt non-virt virt

pA->A_get_private_A(); // A x non-virt non-virt
pA->A_get_virtual_private_A(); // B x non-virt virt(h)
pA->get_private_A(); // B virt(x) virt(h) non-virt
pA->get_virtual_private_A(); // B virt(x) virt(h) virt
pA->get_private_A2(); // A non-virt(x) non-virt non-virt
pA->get_virtual_private_A2(); // B non-virt(x) non-virt virt(h)
pA->get_private_A3(); // B non-virt(x) virt(h) non-virt
pA->get_virtual_private_A3(); // B non-virt(x) virt(h) virt
pA->get_private_A4(); // A virt(x) non-virt non-virt
pA->get_virtual_private_A4(); // B virt(x) non-virt virt(h)
return 0;
}
//---------------------------------------------------------------------------

沒有留言:

張貼留言

文章分類