????2. ??й???й???????????
????(1)?κ????????(virtual)??????????????????е??????
????(2)?κη????????(non-virtual)??????????????????е??????
????(3)?κγ??????(data member)????????????????????????????
????(4)????????????(self)??
??????????
/*
version: 1.0
author: hellogiser
blog: http://www.cnblogs.com/hellogiser
date: 2014/9/27
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class OBJ1
{
public:
OBJ1()
{
cout << "OBJ1 ";
}
};
class OBJ2
{
public:
OBJ2()
{
cout << "OBJ2 ";
}
};
class Base1
{
public:
Base1()
{
cout << "Base1 ";
}
};
class Base2
{
public:
Base2()
{
cout << "Base2 ";
}
};
class Base3
{
public:
Base3()
{
cout << "Base3 ";
}
};
class Base4
{
public:
Base4()
{
cout << "Base4 ";
}
};
class Derived : public Base1?? virtual public Base2??
public Base3?? virtual public Base4
{
public:
Derived() : Base4()?? Base3()?? Base2()??
Base1()?? obj2()?? obj1()
{
cout << "Derived. ";
}
protected:
OBJ1 obj1;
OBJ2 obj2;
};
void test()
{
Derived aa;
cout << "This is ok. ";
}
int main()
{
test();
return 0;
}
/*
Base2
Base4
Base1
Base3
OBJ1
OBJ2
Derived.
This is ok.
*/
??????????2??
/*
version: 1.0
author: hellogiser
blog: http://www.cnblogs.com/hellogiser
date: 2014/9/27
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base1
{
public:
Base1(int i)
{
cout << "Base1 " << i << endl;
}
};
class Base2
{
public:
Base2(int i)
{
cout << "Base2 " << i << endl;
}
};
class Base3
{
public:
Base3()
{
cout << "Base3 *" << endl;
}
};
class Derived : public Base2??  public Base1?? virtual public Base3
{
public:
Derived(int a?? int b?? int c?? int d?? int e)
: Base1(a)?? b2(d)?? b1(c)?? Base2(b)
{
m = e;
cout << "Derived. ";
}
protected:
Base1 b1;
Base2 b2;
Base3 b3;
int m;
};
void test()
{
Derived aa(1?? 2?? 3?? 4?? 5);
cout << "This is ok. ";
}
int main()
{
test();
return 0;
}
/*
Base3 *
Base2 2
Base1 1
Base1 3
Base2 4
Base3 *
Derived.
This is ok.
*/
??????????
????(1) virtual
?????????????Base3
???????????????Base3?????????б????????Base3()?? ?????Base3???????????Base3()?????"Base3  *"
????(2)non-virtual
?????????????Base2??Base1
??????????????Base2?????????б?????Base2(b)??????Base2???????Base2(2)?????"Base2 2"
???????????????Base1?????????б?????Base1(a)??????Base1???????Base1(1)?????"Base1 1"
????(3)data member
???????????????b1??b2??b3
???????????????b1?????????б?????b1(c)??????Base1???????Base1(3)?????"Base1 3"
???????崠??????b2?????????б?????b2(d)??????Base2???????Base1(4)?????"Base2 4"
????????????????b3?????????б????????b3()??????Base3???????Base3()?????"Base3 *"
????(4)self
??????7??????????????????壬???"Derived."