if parent and child classes declare the same static member variable ?
-
then what will be the following is true for that instance 1)all instances of the parent class will reference one variable and each instance of the child class would reference its own separate variable 2)all instances of the parent class would reference one variable and all instance of the child class would reference another variable. 3)All instance of the parent and the child classes would reference the same variable. 4)Each instance of the parent and the child classes would reference its own separate variable. 5)None of the above I want to know answer with some detail, if anybody could have . Thanks in advance
-
then what will be the following is true for that instance 1)all instances of the parent class will reference one variable and each instance of the child class would reference its own separate variable 2)all instances of the parent class would reference one variable and all instance of the child class would reference another variable. 3)All instance of the parent and the child classes would reference the same variable. 4)Each instance of the parent and the child classes would reference its own separate variable. 5)None of the above I want to know answer with some detail, if anybody could have . Thanks in advance
Sometimes a little test is better than a thousand words: (assuming 'parent' is base class and 'child' is derived one)
#include using namespace std;
class A
{
public:
static int alpha;};
class AA: public A
{
public:
static int alpha;
};int A::alpha = 1;
int AA::alpha = 5;void main()
{int i;
const int N=5;
A a[N];
AA aa[N];for (i=0; i<n;> {
cout << "a[" << i << "].alpha = " << a[i].alpha << endl;
cout << "aa[" << i << "].alpha = " << aa[i].alpha << endl;
}
}The output is:
a[0].alpha = 1
aa[0].alpha = 5
a[1].alpha = 1
aa[1].alpha = 5
a[2].alpha = 1
aa[2].alpha = 5
a[3].alpha = 1
aa[3].alpha = 5
a[4].alpha = 1
aa[4].alpha = 5Personally, I'll bet on point (2). ;)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Sometimes a little test is better than a thousand words: (assuming 'parent' is base class and 'child' is derived one)
#include using namespace std;
class A
{
public:
static int alpha;};
class AA: public A
{
public:
static int alpha;
};int A::alpha = 1;
int AA::alpha = 5;void main()
{int i;
const int N=5;
A a[N];
AA aa[N];for (i=0; i<n;> {
cout << "a[" << i << "].alpha = " << a[i].alpha << endl;
cout << "aa[" << i << "].alpha = " << aa[i].alpha << endl;
}
}The output is:
a[0].alpha = 1
aa[0].alpha = 5
a[1].alpha = 1
aa[1].alpha = 5
a[2].alpha = 1
aa[2].alpha = 5
a[3].alpha = 1
aa[3].alpha = 5
a[4].alpha = 1
aa[4].alpha = 5Personally, I'll bet on point (2). ;)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]What is this? Actually writing code to see how C++ works? Why, that's revolutionary.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
What is this? Actually writing code to see how C++ works? Why, that's revolutionary.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Joe Woodbury wrote:
Why, that's revolutionary.
Yes, since about
XVII
century. :-DIf the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]