?? 17-10.txt
字號:
/* 范例:17-10 */
#include <iostream.h>
class A
{
public:
A(int i,int j,int k):a(i),b(j),c(k){cout << "Constructor A\n";}
~A(){cout << "Destructor A\n";}
int a;
void show_mbr_a();
void show_addr();
protected:
int b;
private:
int c;
};
void A::show_mbr_a()
{
cout << "A::(value) => ";
cout << "a=" << a << " b=" << b << " c=" << c << "\n";
}
void A::show_addr()
{
cout << "A::(address) => ";
cout << "&a=" << &a << " &b=" << &b << " &c=" << &c << "\n";
}
class B
{
public:
int a;
B(int i,int j,int k):a(i),b(j),e(k){cout << "Constructor B\n";}
~B(){cout << "Destructor B\n";}
void show_mbr_b();
void show_addr();
protected:
int b;
private:
int e;
};
void B::show_mbr_b()
{
cout << "B::(value) => ";
cout << "a=" << a << " b=" << b << " e=" << e << "\n";
}
void B::show_addr()
{
cout << "B::(address) => ";
cout << "&a=" << &a << " &b=" << &b << " &e=" << &e << "\n";
}
class C:protected B, public A
{
public:
void show_mbr_c();
void show_addr();
void show_All();
C(int i,int j,int k,int l,int m,int n,int o,int p) \
:A(i,j,k),B(l,m,n),a(o),e(p) /* 構造函數初值行 */
{cout << "Constructor C\n";}
~C(){cout << "Destructor C\n";}
int a;
private:
int e;
};
void C::show_mbr_c()
{
cout << "C::(value) => ";
cout << "a=" << a << " e=" << e << "\n";
}
void C::show_addr()
{
cout << "C::(address) => ";
cout << "&a=" << &a << " &e=" << &e << "\n";
}
void C::show_All() // C的show_addr()與父類的override
{
cout << "All Address : \n";
show_mbr_b(); // 繼承后對class C而言,存取等級protected
show_mbr_a(); // 繼承后對class C而言,存取等級public
show_mbr_c();
B::show_addr(); // C中調用父類protected函數
A::show_addr(); // C中調用父類public函數
show_addr();
}
void test()
{
C obj(1,2,3,4,5,6,7,8); // A::1,2,3 B::4,5,6 C::7,8
cout << "*****C show_mbr_c*****\n";
obj.show_mbr_c();
cout << "*****C show_addr*****\n";
obj.show_addr();
cout << "*****C show_All*****\n";
obj.show_All();
//****** error *********
// cout << obj.b << "\n"; // ?::b ambiguous /* #2 */
// obj.B::show_mbr_b(); // protected 無法給外界存取 /* #3 */
}
void main()
{
test();
getchar();
}
程序執行結果:
Constructor B
Constructor A
Constructor C
*****C show_mbr_c*****
C::(value) => a=7 e=8
*****C show_addr*****
C::(address) => &a=0065FDF4 &e=0065FDF8
*****C show_All*****
All Address :
B::(value) => a=4 b=5 e=6
A::(value) => a=1 b=2 c=3
C::(value) => a=7 e=8
B::(address) => &a=0065FDDC &b=0065FDE0 &e=0065FDE4
A::(address) => &a=0065FDE8 &b=0065FDEC &c=0065FDF0
C::(address) => &a=0065FDF4 &e=0065FDF8
Destructor C
Destructor A
Destructor B
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -