Thursday, August 14, 2008

c/c++

#include
using namespace std;

class B
{
public:
B() { Vfunc(); }
virtual ~B() { cout<<"base ~B\r\n"; }
virtual void Vfunc() { cout<<"base Vfunc\r\n"; }
virtual void Vfunc() const { cout<<"const base Vfunc\r\n"; }
void func() { cout<<"base func\r\n"; }
};

class D : public B
{
public:
D() { Vfunc(); }
~D() { cout<<"derive ~D\r\n"; }
virtual void Vfunc() { cout<<"derive Vfunc\r\n"; }
void func() { cout<<"derive func\r\n"; }
};

int main()
{
cout<<"D *pD = new D; -->\r\n";
D *pD = new D;

cout<<"\r\npD->func();-->\r\n";
pD->func();

cout<<"\r\nB &rB1(*pD);-->\r\n";
B &rB1(*pD);
cout<<"\r\nrB1.Vfunc();-->\r\n";
rB1.Vfunc();

cout<<"\r\nconst B &rB2(*pD);-->\r\n";
const B &rB2(*pD);
cout<<"\r\nrB2.Vfunc();-->\r\n";
rB2.Vfunc();


cout<<"\r\nB const &rB3(*pD);-->\r\n";
B const &rB3(*pD);
cout<<"\r\nrB3.Vfunc();-->\r\n";
rB3.Vfunc();

cout<<"\r\n B *pB1(pD);-->\r\n";
B *pB1(pD);
cout<<"\r\npB1->Vfunc();-->\r\n";
pB1->Vfunc();

cout<<"\r\n const B *pB2(pD);-->\r\n";
const B *pB2(pD);
cout<<"\r\n pB2->Vfunc();-->\r\n";
pB2->Vfunc();

cout<<"\r\n B const *pB3(pD);-->\r\n";
B const *pB3(pD);
cout<<"\r\n pB3->Vfunc();-->\r\n";
pB3->Vfunc();


cout<<"\r\ndelete pD;-->\r\n";
delete pD;

return 0;
}


output----------------------------------->>>>>>>

D *pD = new D; -->
base Vfunc
derive Vfunc

pD->func();-->
derive func

B &rB1(*pD);-->

rB1.Vfunc();-->
derive Vfunc

const B &rB2(*pD);-->

rB2.Vfunc();-->
const base Vfunc

B const &rB3(*pD);-->

rB3.Vfunc();-->
const base Vfunc

B *pB1(pD);-->

pB1->Vfunc();-->
derive Vfunc

const B *pB2(pD);-->

pB2->Vfunc();-->
const base Vfunc

B const *pB3(pD);-->

pB3->Vfunc();-->
const base Vfunc

delete pD;-->
derive ~D
base ~B

No comments: