I
Immortal Nephi
First class is the base class. It has two data: m_Base1 and m_Base2.
Second class and third class are derived classes and they are derived
from first class. m_Base1 and m_Base2 are inherited into two derived
classes.
Second class has its own m_Base1 and m_Base2 and third class does the
same. I am curious. How can second class and third class share the
same m_Base1 and m_Base2?
You define second class first and enter data into m_Base1 and
m_Base2. Then, you define third class and how can you modify a
pointer to second class' m_Base1 and m_Base2.
For example:
class Base
{
Base() {}
~Base() {}
int m_Base1;
int m_Base2;
};
class Derive1 : public Base
{
Derive1() {}
~Derive1() {}
void Set()
{
m_Base1 = 20;
m_Base2 = 30;
}
};
class Derive2 : public Base
{
Derive2() {}
~Derive2() {}
void Print()
{
// How can you do this below?
cout << "Derive1::m_Base1 -> " << Derive1::m_Base1 << endl;
cout << "Derive1::m_Base2 -> " << Derive1::m_Base2 << endl;
}
};
int main(void)
{
Derive1 d1;
Derive2 d2;
d1.Set();
d2.Print();
return 0;
}
Nephi
Second class and third class are derived classes and they are derived
from first class. m_Base1 and m_Base2 are inherited into two derived
classes.
Second class has its own m_Base1 and m_Base2 and third class does the
same. I am curious. How can second class and third class share the
same m_Base1 and m_Base2?
You define second class first and enter data into m_Base1 and
m_Base2. Then, you define third class and how can you modify a
pointer to second class' m_Base1 and m_Base2.
For example:
class Base
{
Base() {}
~Base() {}
int m_Base1;
int m_Base2;
};
class Derive1 : public Base
{
Derive1() {}
~Derive1() {}
void Set()
{
m_Base1 = 20;
m_Base2 = 30;
}
};
class Derive2 : public Base
{
Derive2() {}
~Derive2() {}
void Print()
{
// How can you do this below?
cout << "Derive1::m_Base1 -> " << Derive1::m_Base1 << endl;
cout << "Derive1::m_Base2 -> " << Derive1::m_Base2 << endl;
}
};
int main(void)
{
Derive1 d1;
Derive2 d2;
d1.Set();
d2.Print();
return 0;
}
Nephi