次のようなクラスがあります。
class base
{
protected:
int x;
int y;
int z;
public:
base(int x, int y, int z)
{
x = x;
y = y;
z = z;
}
virtual void show();
};
上記からクラスを次のように派生させます。
class derived : protected base
{
public:
int a;
int b;
int c;
derived(int a, int b, int x, int y, int z) : base(x, y, z) //initialising the base class members as well
{
cout<<a<<b<<x<<y<<z; //works fine
a = a;
b = b;
}
void show()
{
cout<<a<<b<<x<<y<<z; //show junk values
}
//some data members and member functions
};
main() では、次を使用します。
derived d(1, 2, 3, 4, 5);
d.show();
データ メンバーは、コンストラクター内で有効な値を持っているようです。ただし、同様の機能を使用すると、つまり同じ可視性モードでジャンク値が表示されるようです。