c++的继承布局/多态

#include <iostream>
using namespace std;
//复杂的继承


                     //钻石型继承
class B
{
public:
int ib;
char cb;
B():ib(0),cb('b'){cout<<"B()\n";}
virtual void f(){cout<<"B::f()\n";}
virtual void Bf(){cout<<"B::Bf()\n";}
};


class B1 : virtual  public B
{
public:
int ib1;
char cb1;
public:
B1():ib1(11),cb1('1') {cout<<"B1()\n";}


virtual void f() { cout << "B1::f()" << endl;}
virtual void f1() { cout << "B1::f1()" << endl;}
virtual void Bf1() { cout << "B1::Bf1()" << endl;}


};


class B2 :   virtual public B
{
public:
int ib2;
char cb2;
public:
B2():ib2(11),cb2('1') {cout<<"B2()\n";}


virtual void f() { cout << "B2::f()" << endl;}
virtual void f2() { cout << "B2::f2()" << endl;}
virtual void Bf2() { cout << "B2::Bf2()" << endl;}


};


class D : public B1, public B2
{
public:
int id;
char cd;
public:
D():id(100),cd('D') {cout<<"D()\n";}


virtual void f() { cout << "D::f()" << endl;}
virtual void f1() { cout << "D::f1()" << endl;}
virtual void f2() { cout << "D::f2()" << endl;}
virtual void Df() { cout << "D::Df()" << endl;}


};


int main()
{
B1 b1;
int **p = (int**)&b1;
cout<<p[1][0]<<"\n";
cout<<p[1][1]<<"\n";
}


#if 0
int main()
{
D d;
cout<<sizeof(d)<<"\n";
}
#endif


/*
class D size(60):
+---
| +--- (base class B1)
0      | | {vfptr}
4      | | {vbptr}
8      | | ib1
12      | | cb1
| | <alignment member> (size=3)
| +---
| +--- (base class B2)
| | +--- (base class B)
16      | | | {vfptr}
20      | | | ib
24      | | | cb
| | | <alignment member> (size=3)
| | +---
28      | | ib2
32      | | cb2
| | <alignment member> (size=3)
| +---
36      | id
40      | cd
| <alignment member> (size=3)
+---
44      | (vtordisp for vbase B)
+--- (virtual base B)
48      | {vfptr}
52      | ib
56      | cb
| <alignment member> (size=3)
+---


D::$vftable@B1@:
| &D_meta
|  0
0      | &D::f1
1      | &B1::Bf1
2      | &D::Df


D::$vftable@:
| -16
0      | &D::f
1      | &B::Bf
2      | &D::f2
3      | &B2::Bf2


D::$vbtable@:
0      | -4
1      | 44 (D d(B1+4)B)


D::$vftable@B@:
| -48
0      | &(vtordisp) thunk: this-=32; goto D::f
1      | &B::Bf
*/
#if 0
                     //重复继承
class B
{
public:
int ib;
char cb;
B():ib(0),cb('b'){}
virtual void f(){cout<<"B::f()\n";}
virtual void Bf(){cout<<"B::Bf()\n";}
};


class B1 :  public B
{
public:
int ib1;
char cb1;
public:
B1():ib1(11),cb1('1') {}


virtual void f() { cout << "B1::f()" << endl;}
virtual void f1() { cout << "B1::f1()" << endl;}
virtual void Bf1() { cout << "B1::Bf1()" << endl;}


};


class B2 :  public B
{
public:
int ib2;
char cb2;
public:
B2():ib2(11),cb2('1') {}


virtual void f() { cout << "B2::f()" << endl;}
virtual void f2() { cout << "B2::f2()" << endl;}
virtual void Bf2() { cout << "B2::Bf2()" << endl;}


};


class D : public B1, public B2
{
public:
int id;
char cd;
public:
D():id(100),cd('D') {}


virtual void f() { cout << "D::f()" << endl;}
virtual void f1() { cout << "D::f1()" << endl;}
virtual void f2() { cout << "D::f2()" << endl;}
virtual void Df() { cout << "D::Df()" << endl;}


};


int main()
{
D d;
}
/*
class D size(48):
+---
| +--- (base class B1)
| | +--- (base class B)
0      | | | {vfptr}
4      | | | ib
8      | | | cb
| | | <alignment member> (size=3)
| | +---
12      | | ib1
16      | | cb1
| | <alignment member> (size=3)
| +---
| +--- (base class B2)
| | +--- (base class B)
20      | | | {vfptr}
24      | | | ib
28      | | | cb
| | | <alignment member> (size=3)
| | +---
32      | | ib2
36      | | cb2
| | <alignment member> (size=3)
| +---
40      | id
44      | cd
| <alignment member> (size=3)
+---


D::$vftable@B1@:
| &D_meta
|  0
0      | &D::f
1      | &B::Bf
2      | &D::f1
3      | &B1::Bf1
4      | &D::Df


D::$vftable@B2@:
| -20
0      | &thunk: this-=20; goto D::f
1      | &B::Bf
2      | &D::f2
3      | &B2::Bf2
*/




#endif 






#if 0
                     //多重继承
class Base1 {
public:
int ibase1;
Base1():ibase1(10) {}
virtual void f() { cout << "Base1::f()" << endl; }
virtual void g() { cout << "Base1::g()" << endl; }
virtual void h() { cout << "Base1::h()" << endl; }


};


class Base2 {
public:
int ibase2;
Base2():ibase2(20) {}
virtual void f() { cout << "Base2::f()" << endl; }
virtual void g() { cout << "Base2::g()" << endl; }
virtual void h() { cout << "Base2::h()" << endl; }
};


class Base3 {
public:
int ibase3;
Base3():ibase3(30) {}
virtual void f() { cout << "Base3::f()" << endl; }
virtual void g() { cout << "Base3::g()" << endl; }
virtual void h() { cout << "Base3::h()" << endl; }
};




class Derive : public Base1, public Base2, public Base3 {
public:
int iderive;
Derive():iderive(100) {}
virtual void f() { cout << "Derive::f()" << endl; }
virtual void g1() { cout << "Derive::g1()" << endl; }
};


int main()
{
Derive d;
return 0;
}


/*
class Derive    size(28):
+---
| +--- (base class Base1)
0      | | {vfptr}
4      | | ibase1
| +---
| +--- (base class Base2)
8      | | {vfptr}
12      | | ibase2
| +---
| +--- (base class Base3)
16      | | {vfptr}
20      | | ibase3
| +---
24      | iderive
+---


Derive::$vftable@Base1@:
| &Derive_meta
|  0
0      | &Derive::f
1      | &Base1::g
2      | &Base1::h
3      | &Derive::g1


Derive::$vftable@Base2@:
| -8
0      | &thunk: this-=8; goto Derive::f
1      | &Base2::g
2      | &Base2::h


Derive::$vftable@Base3@:
| -16
0      | &thunk: this-=16; goto Derive::f
1      | &Base3::g
2      | &Base3::h


Derive::f this adjustor: 0


Derive::g1 this adjustor: 0
*/
#endif 


#if 0


                       //单一继承


class Parent
{
public:
int iparent;
Parent():iparent(10){}
virtual void f()
{
cout<<"Parent::f()\n";
}
virtual void g()
{
cout<<"Parent::g()\n";
}
virtual void h()
{
cout<<"Parent::h()\n";
}
};


class Child:public Parent
{
public:
int ichild;
Child():ichild(100){}
virtual void f(){ cout<<"Child::f()\n"; }
virtual void g_child() { cout << "Child::g_child()\n"; }
virtual void h_child() { cout << "Child::h_child()\n"; }
};


class GrandChild:public Child
{
public:
int igrandchild;
GrandChild():igrandchild(1000) {}
virtual void f() { cout << "GrandChild::f()" << endl; }
virtual void g_child() { cout << "GrandChild::g_child()" << endl; }
virtual void h_grandchild() { cout << "GrandChild::h_grandchild()" << endl; }
};
/*
child :           GrandChild:
&Child::f() ----->GranChiuld::f()
&Parent::g  ----->&Parent::g
        &Parent::h  ----->&Parent::h
&Child::g_child()->GrandChild::g_child()
&Child::h_child()->Child::h_child()
                ->GrandChild::h_grandchild()
iParent
ichild
iGrandChild
*/


typedef void (*FUN)();


int main()
{
FUN pFun = NULL;
GrandChild g;
int **pVtab = (int**)&g;
/*for(int i = 0; (FUN)pVtab[0][i] != NULL; ++i)
{
pFun = (FUN)pVtab[0][i];
pFun();
}*/
cout<<(int)pVtab[1]<<"\n";10
cout<<(int)pVtab[2]<<"\n";100
cout<<(int)pVtab[3]<<"\n";1000
}
#endif
//简单的继承
#if  0
class Base1 
{
public:
virtual void f() { cout << "Base1::f" << endl; }
virtual void g() { cout << "Base1::g" << endl; }
virtual void h() { cout << "Base1::h" << endl; }
};


class Base2 {
public:
virtual void f() { cout << "Base2::f" << endl; }
virtual void g() { cout << "Base2::g" << endl; }
virtual void h() { cout << "Base2::h" << endl; }
};


class Base3 {
public:
virtual void f() { cout << "Base3::f" << endl; }
virtual void g() { cout << "Base3::g" << endl; }
virtual void h() { cout << "Base3::h" << endl; }
};




class Derive : public Base1, public Base2, public Base3 {
public:
virtual void f() { cout << "Derive::f" << endl; }
virtual void g1() { cout << "Derive::g1" << endl; }
};
typedef void(*Fun)(void);




int main()
{
Fun pFun = NULL;
Derive d;
int **pVtab = (int**)&d;


pFun = (Fun)pVtab[0][0];
pFun();
pFun = (Fun)pVtab[0][1];
pFun();
pFun = (Fun)pVtab[0][2];
pFun();
pFun = (Fun)pVtab[0][3];
pFun();
pFun = (Fun)pVtab[1][0];
pFun();
pFun = (Fun)pVtab[1][1];
pFun();
pFun = (Fun)pVtab[1][2];
pFun();
pFun = (Fun)pVtab[2][0];
pFun();
pFun = (Fun)pVtab[2][1];
pFun();
pFun = (Fun)pVtab[2][2];
pFun();


}


Derive::f
Base1::g
Base1::h
Derive::g1
Derive::f
Base2::g
Base2::h
Derive::f
Base3::g
Base3::h


#endif


#if 0
class Base
{
public:
virtual void f(int i = 1)
{
cout<<"Base::f()"<<i<<"\n";
}
};


class Drive:public Base
{
public:
void f(int  i = 2)               //函数值相同,参数列表相同才能够重写,返回值不同会被认为同一函数
{
cout<<"Drive::f()"<<i<<"\n";
}


};


typedef void (*PFU)();


int main()
{
Drive d;
Base *p = &d;
p->f(3);
//Base *p = &d;  //子类私有方法也能重载
//p->f();
/*PFU pFun = (PFU)*(int*)(*(int*)(&d));
pFun();*/
}
#endif


#if 0
class Base1
{
public:
virtual void f1()
{
cout<<"Base1::f1()\n";
}
virtual void g1()
{
cout<<"Base1::g1()\n";
}
virtual void h1()
{
cout<<"Base1::h1()\n";
}


};


class Base2
{
public:
virtual void f1()
{
cout<<"Base2::f1()\n";
}
virtual void g2()
{
cout<<"Base2::g2()\n";
}
virtual void h2()
{
cout<<"Base2::h2()\n";
}


};
class Drive:public Base1,public Base2
{
public:
virtual void f1()
{
cout<<"Drive::f1()\n";
}
virtual void g2()
{
cout<<"Drive::g2()\n";
}
virtual void h3()
{
cout<<"Drive::h3()\n";
}
};




typedef void (*Fun)(void);


int main()
{
Drive d;
Base1 *b1 = &d;
Base2 *b2 = &d;
d.f1();
b1->f1();
b2->f1();


/*Drive b;
Fun pFun = NULL;
cout<<"虚函数表地址:"<<(int*)(&b)<<"\n";
cout<<"第一个函数地址:"<<(int*)*(int*)(&b)<<"\n";*/


/*pFun = (Fun)*((int*)*(int*)(&b));
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+1);
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+2);
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+3);
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+4);
pFun();
pFun = (Fun)*((int*)*(int*)(&b)+5);
pFun();*/
}
#endif


#if 0
class A
{
public:
A(int a):_a(a){cout<<"A()\n";}
private:
public:
int _a;
};


class B
{
public:
B(int b):_b(b){cout<<"B()\n";}
private:
int _b;
};


class C :public A,public B
{
public:
C(int c = 0):_c(c),A(c),B(c){cout<<"C()\n";}
private:
int _c;
};


int main()
{
C c;
}
#endif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值